fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector<int>ps(vector<int>&arr){
  4. vector<int>pref(arr.size()+1,0);
  5. for(int i=1;i<=arr.size();i++){
  6. pref[i]=pref[i-1]+arr[i-1];
  7. }
  8. return pref;
  9. }
  10. int main(){
  11. vector<int>arr={2,4,6,8};
  12. vector<int>pref=ps(arr);
  13. cout<<pref[3]-pref[0]<<"\n"; // sum[1,3] = 12
  14. return 0;
  15. }
  16.  
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
12