Mohammad_Dipu_Sultan

Burst Balloons Optimally_SRBD

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