fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main(){
  6. int n ; cin>>n;
  7. vector<int>arr(n);
  8. for(int i = 0 ; i <n ; i++){
  9. cin>>arr[i];
  10. }
  11. int b; cin>>b ;
  12. int max_sum =INT_MIN;
  13. for(int k=0;k<=b;k++){
  14. int sum = 0 ;
  15. for(int j=0 ; j<k;j++) {
  16. sum+=arr[j];
  17. }
  18. int remaining = b-k;
  19. for(int j = n-1; j>= n-remaining ; j--){
  20. sum+=arr[j];
  21. }
  22. max_sum=max(sum , max_sum);
  23. //k elements from the start , we need to select b-k elements from the end
  24. }
  25. cout<<max_sum;
  26. return 0 ;
  27. }
  28.  
  29.  
Success #stdin #stdout 0s 5308KB
stdin
5
5 -2 3 1 2 
5

stdout
9