fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. int n = 5 ; //size of the array
  7. int a[5] = {6,7,3,2,2};
  8.  
  9. int dp[n+1]={0}; //dp array
  10. int i = 0 ;
  11. while(i<=n-1){
  12. if(i==0){
  13. dp[i] = a[i] ;
  14. }
  15. else{
  16. dp[i] = a[i] + dp[i-1];
  17. }
  18. i++;
  19. }
  20. int q = 4 ; //number of queries
  21. int w[4] = {0,3,4,2}; //query array
  22. i = 0 ;
  23. while(i<=q-1){
  24. int query;query = w[i];
  25. cout<<dp[query]; //answering each query in O(1)
  26. cout<<endl;
  27. i++;
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
6
18
20
16