更换位置的花费: 假如是从第
p
r
e
pre
pre个王国占领到第
i
i
i个王国,
w
1
=
a
b
s
(
c
[
i
]
−
c
[
p
r
e
]
)
∗
a
w1 = abs(c[i] - c[pre]) *a
w1=abs(c[i]−c[pre])∗a
假如更换以后可以减少占领后续王国的花费:
w
2
=
(
n
−
i
)
∗
b
∗
(
c
[
i
]
−
c
[
p
r
e
]
)
w2 = (n - i) * b * (c[i] - c[pre])
w2=(n−i)∗b∗(c[i]−c[pre])
只要比较这两个的花费然后选择方案即可。
LL w[N], sum[N];voidsolve(){
cin >> n >> a >> b;for(int i =0; i <= n; i ++) sum[i]=0;int pre =0;for(int i =1; i <= n; i ++){
cin >> w[i];
sum[i]= sum[i -1]+ w[i];}
LL ans =0;for(int i =1; i <= n; i ++){
ans +=abs(w[i]- w[pre])* b;if((w[i]- w[pre])* b *1ll*(n - i)> a *abs(w[i]- w[pre])){
ans += a *abs(w[i]- w[pre]);
pre = i;}}
cout << ans << endl;}