You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.2 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
stack<int> stk; //栈
int a[N]; //高度数组
int b[N]; //能量数组
int s[N]; //每个发射塔的结果数组
int n; //发射塔个数
int res; //最大结果
/**
* i
* @param i
*/
void add(int i) {
//把栈顶高度比自己小的出栈这样的是不能享受i个发射塔带来的能量滴~
while (!stk.empty() && a[i] > a[stk.top()]) stk.pop();
//如果找着了一个高度比自己大的发射塔那么这个位置的发射塔将享受i个发射塔带来的能量。
if (!stk.empty()) s[stk.top()] += b[i];
//把自己入栈等待其它人给自己带来能量enjoy!
stk.push(i);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
//从左向右扫一遍
for (int i = 1; i <= n; i++) add(i);
//清空栈
while (!stk.empty()) stk.pop();
//从右向左扫一遍
for (int i = n; i >= 1; i--) add(i);
//求最大值
for (int i = 1; i <= n; i++) res = max(s[i], res);
//输出
printf("%d", res);
}