题目
有一个长度为 序列
(序列下标从 1 开始),每次可以从任意位置
花费
的代价来把
删除。
注意,删除后 后面的数会依次向前补上(下标 -1 ) 。
求把整个序列删完的最小代价。
解题思路
当 是正数时,在下标 1 的位置删除代价最小,即
。
当 是负数时,在其当前所在下标的位置删除代价最小,即
,因为下标不能更大了。
C++代码
#include<cstdio>
using namespace std;
int main(){
int n;
scanf("%d", &n);
long long ans = 0;
int a;
for(int i=0; i<n; ++i){
scanf("%d", &a);
if(a<0)
ans += 1LL*a*(i+1);
else
ans += a;
}
printf("%lld", ans);
return 0;
} 
京公网安备 11010502036488号