public class NegSum { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] h = new int[n]; int sum = 0; for(int i = 0; i < n; i++) { h[i] = in.nextInt(); sum += h[i] < 0 ? i+1 : 0; } System.out.println(sum); } }
Answers & Comments
public class NegSum {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] h = new int[n];
int sum = 0;
for(int i = 0; i < n; i++) {
h[i] = in.nextInt();
sum += h[i] < 0 ? i+1 : 0;
}
System.out.println(sum);
}
}