Advertisement
Misbah_Uddin_Tareq

dp Soluion by istiak(toph)

May 17th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4.  
  5. const int N=2050;
  6. ll n,dp[N][N],arr[N];
  7. ll solve(int i,int j,ll moves)
  8. {
  9.     if(i>j)
  10.         return 0;
  11.     if(!dp[i][j])
  12.     {
  13.         ll x=arr[i]*moves+solve(i+1,j,moves+1);
  14.         ll y=arr[j]*moves+solve(i,j-1,moves+1);
  15.         dp[i][j]=min(x,y);
  16.     }
  17.     return dp[i][j];
  18. }
  19.  
  20. int main()
  21. {
  22.     cin>>n;
  23.     for(int i=0; i<n; i++)
  24.         cin>>arr[i];
  25.     cout<<solve(0,n-1,1);
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement