fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int n,q; // size of array n and no of queries q.
  7. cin>>n>>q;
  8. vector<int> arr(n+1,0); //using 1 based indexing
  9. vector<int> dp(n+1); // creating a dp array to store sum till i from 1
  10. for(int i=1;i<=n;i++)
  11. {
  12. cin>>arr[i];
  13. dp[i]=dp[i-1]+arr[i];
  14. }
  15. for(int i=0;i<q;i++)
  16. {
  17. int query;
  18. cin>>query;
  19. cout<<dp[query]<<endl;
  20. }
  21.  
  22.  
  23. return 0;
  24. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty