Advertisement
Guest User

Hdhdhddhdhd

a guest
Jan 27th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define int long long
  5. long long t[1000000 + 5];
  6.  
  7. stack<pair<int,int> > stos;
  8.  
  9. main(){
  10.     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  11.    
  12.     int n;
  13.     cin >> n;
  14.     for(int i = 1; i <= n; i++)
  15.         cin >> t[i];
  16.     long long ans = 0;
  17.     long long suma = 0;
  18.  
  19.     for(int i = 1; i <= n; i++){
  20.         int ile = 0;
  21.         //~ cout << "i :" << i << " suma :" << suma << endl;
  22.         while(stos.empty() == 0 && stos.top().first >= t[i]){
  23.             ile += stos.top().second;
  24.             //~ cout << " zwiekszone ile o :" << stos.top().second << endl;
  25.             suma -= stos.top().second * stos.top().first;
  26.             //~ cout << " suma po zwiekszeniu : "<< suma << endl;
  27.             stos.pop();
  28.         }
  29.         ile++;
  30.         //~ cout <<"i :" << i << "ile po :" << ile << endl;
  31.         stos.push({t[i],ile});
  32.         suma += ile * t[i];
  33.         //~ cout << "i :" <<i<<" o : " << suma << endl;
  34.         ans += suma;
  35.     }
  36.    
  37.     cout << ans;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement