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.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# 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 ) ;
}