输入的第
行包含一个整数
(
),表示能量发射站的个数。
对于输入的第
到
行,第
行有两个整数
(
)、
(
),表示第
个发射站的高度和发射的能量值。
输出仅一行,表示接收最多能量的发射站接收到的能量值。
3 4 2 3 5 6 10
7
#include <iostream>
#include <vector>
#include <stack>
int main()
{
int n = 0;
std::cin>>n;
std::vector<std::pair<int,int>> stations(n);
for(int i = 0;i < n;++i)
{
auto&[a,b] = stations[i];
std::cin>>a>>b;
}
std::stack<int> st;
std::vector<int> nums(n);
for(int i = 0;i < n;++i)
{
while(!st.empty() && stations[st.top()].first < stations[i].first)
{
nums[i] += stations[st.top()].second;
st.pop();
}
st.push(i);
}
while(!st.empty()) st.pop();
for(int i = n-1;i >= 0;--i)
{
while(!st.empty() && stations[st.top()].first < stations[i].first)
{
nums[i] += stations[st.top()].second;
st.pop();
}
st.push(i);
}
int ans = 0;
for(int&num:nums)
{
ans = std::max(ans,num);
}
std::cout<<ans<<'\n';
}