[cpp] Twomeals Brute Force

Viewer

copydownloadembedprintName: Twomeals Brute Force
  1. #include<iostream>
  2. using namespace std;
  3. int main(){
  4.     int n;
  5.     cin>>n;
  6.     int arr[n];
  7.     for(int i=0;i<n;i++){
  8.         cin>>arr[i];
  9.     }
  10.     int ans=0;
  11.     for(int l1=0;l1<n;l1++){
  12.         for(int r1=l1;r1<n;r1++){
  13.             for(int l2=r1+1;l2<n;l2++){
  14.                 for(int r2=l2;r2<n;r2++){
  15.                     int sum=0;
  16.                     for(int i=l1;i<=r1;i++){
  17.                         sum+=arr[i];
  18.                     }
  19.                     for(int i=l2;i<=r2;i++){
  20.                         sum+=arr[i];
  21.                     }
  22.                     ans=max(ans,sum);
  23.                 }
  24.             }
  25.         }
  26.     }
  27.     cout<<ans;
  28. }

Editor

You can edit this paste and save as new:


File Description
  • Twomeals Brute Force
  • Paste Code
  • 18 Apr-2024
  • 672 Bytes
You can Share it: