Advertisement
Mohammad_Dipu_Sultan

Burst Balloons Optimally SRBD

Aug 26th, 2023
1,311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int a[15], n;
  5. vector<int>seen;
  6.  
  7. int sum(int ind){
  8.     int store = 1;
  9.     bool f=false;
  10.  
  11.     for(int i = ind+1; i<n; i++){
  12.         if(seen[i] == 0){
  13.             store = store*a[i];
  14.             f=true;
  15.             break;
  16.         }
  17.     }
  18.  
  19.     for(int i=ind-1; i>=0; i--){
  20.         if(seen[i]==0){
  21.             store = store * a[i];
  22.             f=true;
  23.             break;
  24.         }
  25.     }
  26.  
  27.     if(f==true){
  28.         return store;
  29.     }
  30.     else{
  31.         return a[ind];
  32.     }
  33. }
  34.  
  35. int Brust(int ind){
  36.  
  37.     if(ind==n){
  38.         return 0;
  39.     }
  40.  
  41.     int ans = INT_MIN;
  42.     for(int i = 0; i < n; i++){
  43.         if(seen[i]==0){
  44.             seen[i]=1;
  45.             ans = max(ans, Brust(ind+1)+sum(i));
  46.             seen[i]=0;
  47.         }
  48.     }
  49.  
  50.     return ans;
  51. }
  52.  
  53.  
  54. int main(){
  55.  
  56.     cin >> n;
  57.     for( int i = 0; i < n; i++){
  58.         cin >> a[i];
  59.     }
  60.  
  61.     seen.assign(n, 0);
  62.     cout << Brust(0) << endl;
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement