//EVIEBOT #include #define inf 0x3f3f3f3f #define INF 1000111000111000111LL #define DEBUG(x) cout << '>' << #x << ':' << x << endl; #define fastIO ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL); #define endl '\n' #define pb push_back #define pf push_front #define fi first #define se second #define MP make_pair #define MT make_tuple #define clr(xxx) memset(xxx, 0, sizeof(xxx)); typedef long long int lli; using namespace std; ofstream outfile; ifstream infile; vector a, sa, ssa; int n; void transform(vector & f, vector & s){ int nn = f.size(), i, j, k; int arr[nn+1][nn+1]; for(i = 0; i < nn; ++i){ int maxi = 0, ind; for(j = i; j < nn; ++j){ maxi = max(maxi, f[j]); arr[i][j] = maxi; } } for(k= 0; k < nn; ++k){ for(i = 0; i <= nn-k-1; ++i){ j = i+k; //cout << i << ' '<< j << ' '<< arr[i][j] << "\n"; s.pb(arr[i][j]); } } } int main() { infile.open("input.txt"); outfile.open("output.txt"); fastIO; int i, x; cin >> n; for(i =1; i <= n; ++i){ cin >> x; a.pb(x); } transform(a, sa); transform(sa, ssa); lli sum = 0; for(i = 0; i < ssa.size(); ++i) sum += (lli)ssa[i]; cout << sum << "\n"; }