ann8497

burst the balloon to get the max coins

Aug 8th, 2019
1,339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int dp[100][100];
  5. int solve(int *a , int n){
  6.    
  7.    for(int len = 1; len<=n; len++){
  8.        for(int left = 1; left<=n-len+1; left++){
  9.        int right = left + len - 1;
  10.        for(int k = left; k<=right; k++){
  11.            dp[left][right] = max(dp[left][right],(dp[left][k-1]
  12.                                   + dp[k+1][right] + a[left-1]*a[k]*a[right+1]));
  13.        }
  14.        }
  15.    }
  16.    return dp[1][n];
  17. }
  18.  
  19. int main(){
  20.    
  21.     int n; cin>>n;
  22.     int a[n+2];
  23.     a[0] = 1;
  24.     a[n+1] = 1;
  25.     for(int i =1; i<=n; i++)cin>>a[i];
  26.     a[n+1] = 1;
  27.     memset(dp, 0, sizeof(dp));
  28.     cout<<solve(a,n)<<endl;
  29.     return 0;
  30. }
Add Comment
Please, Sign In to add comment