fork download
  1. // Abhay Singh Bhadoria ( _Asb_ ) //
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. const int N = 2e3 + 5;
  5. int a[N], dp[N][N];
  6. int main() {
  7. ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  8. int tc;
  9. cin >> tc;
  10. while(tc--) {
  11. int n;
  12. cin >> n;
  13. for(int i = 1; i <= n; ++i) cin >> a[i];
  14. for(int i = 0; i <= n; ++i) {
  15. for(int j = 1; i + j <= n; ++j) {
  16. int k = j + i;
  17. dp[j][k] = max(dp[j + 1][k] + (n - i) * a[j], dp[j][k - 1] + (n - i) * a[k]);
  18. }
  19. }
  20. cout << dp[1][n] << endl;
  21. }
  22. }
Success #stdin #stdout 0s 31768KB
stdin
1
5
1 2 3 4 5
stdout
55