Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- long long t[1000000 + 5];
- stack<pair<int,int> > stos;
- main(){
- ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- int n;
- cin >> n;
- for(int i = 1; i <= n; i++)
- cin >> t[i];
- long long ans = 0;
- long long suma = 0;
- for(int i = 1; i <= n; i++){
- int ile = 0;
- //~ cout << "i :" << i << " suma :" << suma << endl;
- while(stos.empty() == 0 && stos.top().first >= t[i]){
- ile += stos.top().second;
- //~ cout << " zwiekszone ile o :" << stos.top().second << endl;
- suma -= stos.top().second * stos.top().first;
- //~ cout << " suma po zwiekszeniu : "<< suma << endl;
- stos.pop();
- }
- ile++;
- //~ cout <<"i :" << i << "ile po :" << ile << endl;
- stos.push({t[i],ile});
- suma += ile * t[i];
- //~ cout << "i :" <<i<<" o : " << suma << endl;
- ans += suma;
- }
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement