Sample Input
5
5 -4 1 -3 1
6
-1000 -1000 -1000 1000 1000 1000
0
Sample Output
9
9000
解法:貪心法 事實上運到隔壁即可
import java.util.Scanner;
public class UVA11054 {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int number=0,i=1;
long count = 0;
while(sc.hasNext()){
number = sc.nextInt();
if(number == 0)break;
sc.nextLine();
count =0;
int box[] = new int[number+1];
String num[] = sc.nextLine().split(" ");
for(i=0;i<number;i++){
box[i] = Integer.parseInt(num[i]);
}
for(i=1;i<number;i++){
count+=Math.abs(box[i-1]);
box[i] += box[i-1];
}
System.out.println(count);
}
sc.close();
}
}