Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #define N 100005
- using namespace std;
- ///2650
- ifstream fin(".in");
- ofstream fout(".out");
- int st[N], dr[N], a[N],s[N];
- int n ,top,i,x;
- long long sum=0;
- int main()
- {
- cin>>n;
- for(i=1;i<=n;i++)cin>>a[i];
- /// 2 santinele
- a[0]=a[n+1]=N;
- ///construim vector la stg
- s[1]=0;
- for(i=1;i<=n;++i)
- {
- x=a[i];
- while(top>0 && a[s[top]]<x)top--;
- st[i]=i-s[top];
- s[++top]=i;
- }
- ///cosntruim vector la dr
- s[1]=n+1;
- top=1;
- for(i=n;i>=1;--i)
- {
- x=a[i];
- while(top>0 && a[s[top]]<x)top--;
- dr[i]=s[top]-i;
- s[++top]=i;
- }
- ///suma
- for(int i=1;i<=n;i++)
- sum+=a[i]*st[i]*dr[i];
- cout<<sum;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment