有编号1-n的n个格子,机器人从1号格子顺序向后走,一直走到n号格子,并需要从n号格子走出去。机器人有一个初始能量,每个格子对应一个整数A
i
i
,表示这个格子的能量值。如果A
i
i
> 0,机器人走到这个格子能够获取A
i
i
个能量,如果A
i
i
< 0,走到这个格子需要消耗相应的能量,如果机器人的能量 < 0,就无法继续前进了。问机器人最少需要有多少初始能量,才能完成整个旅程。
例如:n = 5。{1,-2,-1,3,4} 最少需要2个初始能量,才能从1号走到5号格子。途中的能量变化如下3 1 0 3 7。
Input
第1行:1个数n,表示格子的数量。(1 <= n <= 50000)
第2 - n + 1行:每行1个数A
i
i
,表示格子里的能量值(-1000000000 <= A
i
i
<= 1000000000)
Output
输出1个数,对应从1走到n最少需要多少初始能量。
Sample Input
5
1
-2
-1
3
4
Sample Output
2
Input
第1行:1个数n,表示格子的数量。(1 <= n <= 50000)
第2 - n + 1行:每行1个数A
i
i
,表示格子里的能量值(-1000000000 <= A
i
i
<= 1000000000)
Output
输出1个数,对应从1走到n最少需要多少初始能量。
Sample Input
5
1
-2
-1
3
4
Sample Output
2
分析:总的来看,这道题不就是对序列逐个求和,当遇到和负数就进行处理,正数不用管,处理的方式就是遇到负数就将和置零,然后开始新的累加,并将答案累加,上代码吧,很简单的思路
import java.util.*;
public class Main {
static Scanner in = new Scanner(System.in);
static int maxn = 50005;
static int[] a = new int[maxn];
public static void main(String[] args) {
int n = in.nextInt();
for(int i = 0;i < n;i++)
a[i] = in.nextInt();
long sum = a[0],ans = 0;
for(int i = 1;i < n;i++) {
if(sum+a[i]<0) {
ans+=Math.abs(sum+a[i]);
sum = 0;
}
else
sum+=a[i];
}
System.out.println(ans);
}
}