YEZAELP

SMMR-099: MTrap

Jun 8th, 2020
121
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. const long long INF=2e18;
  4. long long dp[410][410];
  5. int main(){
  6.     int n;
  7.     scanf("%d",&n);
  8.     vector <long long> v;
  9.     for(int i=1;i<=n;i++){
  10.         long long x,y;
  11.         scanf("%lld %lld",&x,&y);
  12.         if(i==1) v.push_back(x);
  13.         v.push_back(y);
  14.     }
  15.  
  16.     for(int i=v.size()-1;i>=1;i--){
  17.             for(int j=1;j<=v.size()-1;j++){
  18.                 if(i==j){
  19.                     dp[i][j]=0;
  20.                     continue;
  21.                 }
  22.                 dp[i][j]=INF;
  23.                 for(int k=i;k<j;k++){
  24.                     long long x;
  25.                     x = dp[i][k] + dp[k+1][j] + v[i-1]*v[k]*v[j];
  26.                     dp[i][j] = min(dp[i][j] , x);
  27.                 }
  28.             }
  29.         }
  30.     printf("%lld\n",dp[1][v.size()-1]);
  31.     return 0;
  32. }
Add Comment
Please, Sign In to add comment