Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- * problem link: https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&category=0&problem=2730&mosmsg=Submission+received+with+ID+27781398
- *
- * */
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define endl '\n'
- #define sz(x) int(x.size())
- #define all(x) x.begin(), x.end()
- int main() {
- ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
- int h, w;
- while (scanf("%d", &h), h) {
- scanf("%d", &w);
- int high[w + 1];
- high[0] = h;
- stack<int> stk;
- stk.push(0);
- int cnt = 0;
- for (int i = 1; i <= w; i++) {
- scanf("%d", &high[i]);
- while (stk.size() && high[stk.top()] <= high[i]) stk.pop();
- if (stk.size() && high[stk.top()] > high[i]) {
- cnt += high[stk.top()] - high[i];
- stk.pop();
- }
- stk.push(i);
- }
- cout << cnt << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment