fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc=new Scanner(System.in);
  14. int n=sc.nextInt();
  15. int a[]=new int[n+1];
  16.  
  17. for(int i=1;i<=n;i++)
  18. a[i]=sc.nextInt();
  19.  
  20. int prefix[]=new int[n+1];
  21.  
  22. for(int i=1;i<=n;i++)
  23. prefix[i]=prefix[i-1]+a[i];
  24.  
  25.  
  26. int q=sc.nextInt();
  27. while(q-->0)
  28. {
  29. int l=sc.nextInt();
  30. int r=sc.nextInt();
  31.  
  32. System.out.println(prefix[r]-prefix[l-1]);
  33. }
  34. }
  35. }
Success #stdin #stdout 0.14s 56520KB
stdin
5
1 5 6 1 3
1
3 
5
stdout
10