Advertisement
a53

Triangle_DP

a53
Apr 17th, 2022
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n,arr[251];
  4. long long dp[251][251];
  5.  
  6. int main()
  7. {
  8. ios_base::sync_with_stdio(0);
  9. cin.tie(0);
  10. cout.tie(0);
  11. cin>>n;
  12. for(int i=0;i<n;++i)
  13. cin>>arr[i];
  14. for(int m=0;m<n;++m)
  15. {
  16. for(int i=0,j=m;j<n;++i,++j)
  17. {
  18. if(j<i+2)
  19. dp[i][j]=0;
  20. else
  21. {
  22. dp[i][j]=0;
  23. for(int k=i+1;k<j;++k)
  24. {
  25. long long val=dp[i][k]+dp[k][j]+1LL*arr[i]*arr[j]*arr[k];
  26. if(dp[i][j]<val)
  27. dp[i][j]=val;
  28. }
  29. }
  30. }
  31. }
  32. cout<<dp[0][n-1];
  33. return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement