fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6.  
  7. int n = sc.nextInt();
  8. int[] a = new int[n];
  9.  
  10. for (int i = 0; i < n; i++) {
  11. a[i] = sc.nextInt();
  12. }
  13.  
  14. int[] dp = new int[n];
  15. for (int i = 0; i < n; i++) {
  16. if (i == 0) {
  17. dp[i] = a[i];
  18. } else {
  19. dp[i] = a[i] + dp[i - 1];
  20. }
  21. }
  22.  
  23. int q = sc.nextInt();
  24. int[] w = new int[q];
  25.  
  26. for (int i = 0; i < q; i++) {
  27. w[i] = sc.nextInt();
  28. }
  29.  
  30. for (int i = 0; i < q; i++) {
  31. int query = w[i];
  32. System.out.println(dp[query]);
  33. }
  34.  
  35. sc.close(); // if i dont close this the memory will leak and it also tells the program that im doen woth tking input
  36. }
  37. }
  38.  
Success #stdin #stdout 0.17s 56564KB
stdin
5
1 2 3 4 5
2
3 4
stdout
10
15