#include #include using namespace std; using namespace atcoder; #define rep(i, n) for (int i = 0; i < (int)(n); i++) template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } int main() { int N; cin >> N; stack> sta; sta.push({0,1000000001}); long long res = 0; rep(i,N){ int h; cin >> h; int index = 1; if(i % 2 == 0){ int index = 0; } while(true){ auto p = sta.top(); if(p.second < h){ if(index % 2){ if(i % 2 == 0){ res += p.second - p.first; }else{ res -= p.second - p.first; } } sta.pop(); index++; }else{ // cout << index <<" " <