for(int i = 1; i<=n; i++){ for(int j = 0; j<i; j++){ d[i][j] = max(d[i][j], d[i-1][j] + (i * cookie[n - i + j])); d[i][j+1] = max(d[i][j+1], d[i-1][j] + (i * cookie[j])); } }
#include<iostream> using namespace std; const int size = 2020; int n; int d[size][size], cookie[size]; int main(void) { cin>>n; for(int i = 0; i<n; i++){ cin>>cookie[i]; } for(int i = 1; i<=n; i++){ for(int j = 0; j<i; j++){ d[i][j] = max(d[i][j], d[i-1][j] + (i * cookie[n - i + j])); d[i][j+1] = max(d[i][j+1], d[i-1][j] + (i * cookie[j])); } } int ans = 0; for(int i = 0; i<n; i++){ ans = max(ans, d[n][i]); } cout<<ans; return 0; }