提交时间:2025-02-07 16:14:40

运行 ID: 36118

#include<bits/stdc++.h> using namespace std; #define ll long long const int N = 1e4 + 5, pre = 1e4; ll n, a[N], cnt[2][N + N],ans; int main(){ //freopen("book10.in","r",stdin); //freopen("book10.out","w",stdout); ios::sync_with_stdio(0); cin>>n; for (int i = 1; i <= n; i++) { cin>>a[i]; } for (int i = 1; i <= n; i++) { memset(cnt, 0, sizeof(cnt)); int x = 0; for (int j = i; j >= 1; j--) { x += a[j] < a[i] ? -1 : (a[j] > a[i] ? 1 : 0); cnt[j & 1][pre + x] += j; } x = 0; for (int j = i; j <= n; j++) { x += a[j] < a[i] ? 1 : (a[j] > a[i] ? -1 : 0); ans += 1ll * cnt[j & 1][pre + x] * j * a[i]; } } cout<<ans; return 0; }