winemom

PODM

Nov 12th, 2025
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int nMax = 505;
  5. typedef long long ll;
  6. ll best[nMax][nMax], dim[nMax];
  7.  
  8. int main()
  9. {
  10.     int n;
  11.     cin >> n;
  12.     for(int i=0; i<=n; i++)
  13.         cin >> dim[i];
  14.     for(int i=1; i<n; i++)
  15.         best[i][i+1] = dim[i-1] * dim[i] * dim[i+1];
  16.  
  17.     for(int d=2; d<=n-1; d++)
  18.     {
  19.         for(int i=1; i<=n-d; i++)
  20.         {
  21.             best[i][i + d] = LONG_LONG_MAX;
  22.             for(int k=i; k< i+ d; k++)
  23.             {
  24.                 best[i][i + d] = min(best[i][i + d], best[i][k] + best[k +1][i + d] + dim[i-1] * dim[k] * dim[i + d]);
  25.             }
  26.         }
  27.     }
  28.     cout << best[1][n];
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment