Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created September 13, 2019 13:31
Show Gist options
  • Save surinoel/ee617277031f9c274efbf8c945cc0ab7 to your computer and use it in GitHub Desktop.
Save surinoel/ee617277031f9c274efbf8c945cc0ab7 to your computer and use it in GitHub Desktop.
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int b[1001];
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int maxh = -1, idx;
for (int i = 0; i < n; i++) {
int l, h;
cin >> l >> h;
b[l] = h;
if (maxh == -1 || h > maxh) {
maxh = h;
idx = l;
}
}
int sum = maxh;
int pidx = idx;
for (;;) {
int ppidx = -1;
int tmaxh = 0;
for (int i = pidx - 1; i >= 0; i--) {
if (b[i] > tmaxh) {
tmaxh = b[i];
ppidx = i;
}
}
if (ppidx == -1) {
break;
}
else {
sum += abs(pidx - ppidx) * tmaxh;
pidx = ppidx;
}
}
pidx = idx;
for (;;) {
int ppidx = -1;
int tmaxh = 0;
for (int i = pidx + 1; i <= 1000; i++) {
if (b[i] > tmaxh) {
tmaxh = b[i];
ppidx = i;
}
}
if (ppidx == -1) {
break;
}
else {
sum += abs(pidx - ppidx) * tmaxh;
pidx = ppidx;
}
}
cout << sum << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment