提交时间:2025-02-07 16:24:30

运行 ID: 36126

/*B baoli*/ #include<bits/stdc++.h> #define int long long using namespace std; int n,a[10005],b[20005],c[20005],ans=0; signed main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<=n;i++){ int x; x=0; for(int j=i-1;j>=1;j--){ if(a[j]<a[i])x--; else x++; b[n+x]+=j; } x=0; for(int j=i+1;j<=n;j++){ if(a[j]<a[i])x--; else x++; c[n+x]+=j; } x=0; for(int j=i-1;j>=1;j--){ if(a[j]<a[i])x--; else x++; ans+=j*c[n-x]*a[i]; } ans+=i*b[n]*a[i]; ans+=i*c[n]*a[i]; ans+=i*i*a[i]; for(int j=1;j<=2*n-1;j++)b[j]=c[j]=0; } cout<<ans; return 0; }