提交时间:2025-02-07 14:24:55
运行 ID: 36024
#include <bits/stdc++.h> #define int long long using namespace std; int n,p[10004]; int ans; signed main () { cin >> n; for (int i = 1;i <= n;i++) { cin >> p[i]; } for (int l = 1;l <= n;l++) { int m = p[l]; ans += l * l * m; priority_queue <int> pq,pq2; pq.push(p[l]); for (int r = l + 2;r <= n;r += 2) { if (p[r - 1] < m) { pq.push(p[r - 1]); } else { pq2.push(p[r - 1]); } if (p[r] < m) { pq.push(p[r]); } else { pq2.push(p[r]); } if (pq2.size() < (r - l + 1) / 2) { pq2.push(-pq.top()),pq.pop(); } if (pq2.size() > (r - l + 1) / 2) { pq.push(-pq2.top()),pq2.pop(); } m = pq.top(); ans += l * r * pq.top(); } } cout << ans << endl; return 0; }