fork download
  1.  
  2.  
  3. /* Name of the class has to be "Main" only if the class is public. */
  4. class Ideone
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. int[] a = {0, 4, 8, 1, 3, 5, 9};
  9. int n = a.length-1;
  10.  
  11. int[] queries = new int[]{2,3,5,1};
  12.  
  13. long[] dp = new long[n+1];
  14.  
  15. dp[0] = 0;
  16.  
  17. for(int i = 1; i <= n; i++){
  18. dp[i] = dp[i-1] + a[i];
  19. }
  20.  
  21. for (int q : queries) {
  22. if (q >= 0 && q <= n) {
  23. System.out.println("Sum of first " + q + " numbers: " + dp[q]);
  24. }
  25. }
  26. }
  27. }
Success #stdin #stdout 0.11s 55424KB
stdin
Standard input is empty
stdout
Sum of first 2 numbers: 12
Sum of first 3 numbers: 13
Sum of first 5 numbers: 21
Sum of first 1 numbers: 4