solution :
对于f[i] = min{f[k] + max(g[k+1~i]}方程的优化(k的取值不能超过一左端点)
注意到函数max的单调性,,事实上在合法的k的取值区间内,真正有影响的转移只有使max函数值改变的那几个点,维护一个单调双端队列,在i右推时保证队列中g数组的单调减,加个配对堆辅助添加删除,注意需要修改队头元素在堆中的值
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 2E4 + 20;
typedef long long LL;
typedef __gnu_pbds::priority_queue<int,greater<int>,__gnu_pbds::pairing_heap_tag> Heap;
int n,lim,cnt,h[maxn],g[maxn],f[maxn],a[maxn],inq[maxn];
LL sum[maxn];
Heap Q;
Heap::point_iterator id[maxn];
bool Judge(int now)
{
int head = 1,tail = 0,Left = 0;
Q.clear();
for (int i = 1; i <= n; i++) {
while (sum[i] - sum[Left] > now) ++Left;
while (head <= tail && a[head] <= Left) Q.erase(id[a[head++]]);
while (head <= tail && h[a[tail]] < h[i]) Q.erase(id[a[tail--]]);
a[++tail] = i;
a[head-1] = Left;
id[i] = Q.push(f[a[tail-1]] + h[i]);
if (head < tail)
Q.modify(id[a[head]],f[Left] + h[a[head]]);
f[i] = Q.top();
if (f[i] > lim) return 0;
}
return 1;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#else
freopen("training.in","r",stdin);
freopen("training.out","w",stdout);
#endif
cin >> n >> lim;
int L,R; L = R = 0;
for (int i = 1; i <= n; i++) {
scanf("%d%d",&h[i],&g[i]);
L = max(L,g[i]);
R += g[i];
sum[i] = sum[i-1] + 1LL*g[i];
}
while (R - L > 1) {
int mid = (L + R) >> 1;
if (Judge(mid)) R = mid;
else L = mid;
}
if (Judge(L)) cout << L;
else cout << R;
return 0;
}