fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int f(const vector<int> &v, vector<vector<int>> &dp, int i, int j){
  5. if(dp[i][j]!=-1) return dp[i][j];
  6. int l = 1;
  7. int r = 1;
  8. if(i) l = v[i-1];
  9. if(j<v.size()) r = v[j];
  10. int ans = 0; // ans = 0 when i==j
  11. for(int k=i; k<j; k++){
  12. int x = f(v,dp,i,k) + l*v[k]*r + f(v,dp,k+1,j);
  13. if(ans<x) ans = x;
  14. }
  15. return dp[i][j] = ans;
  16. }
  17. int f(const vector<int> &v){
  18. int n = v.size();
  19. vector<vector<int>> dp(n+1,vector<int>(n+1,-1));
  20. return f(v,dp,0,n);
  21. }
  22. int main() {
  23. int t,n,x;
  24. vector<int> v;
  25. cin>>t;
  26. while(t--){
  27. cin>>n;
  28. v.clear();
  29. while(n--&&cin>>x) v.push_back(x);
  30. cout<<f(v)<<endl;
  31. }
  32. return 0;
  33. }
Success #stdin #stdout 0s 4456KB
stdin
2
2 5 10
5 1 2 3 4 5
stdout
60
110