题目:https://ac.nowcoder.com/acm/contest/75341/E
代码:
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
const int N = 2e6 + 5;
ll a[N], b[N];
int main() {
int n, m;
cin >> n >> m;
a[0] = b[0] = 0;
// 先算前缀和
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] += a[i - 1];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
b[i] += b[i - 1];
}
ll j = 0, res = 1e9, ans = -1e9;
for (int i = 1; i <= n; i++) {
while (a[i] - a[j] >= m) {
// 找最小的前缀和
res = min(res, b[j]);
// j++的目的其实就是看能不能去掉最前面可口度为负的蛋糕
j++;
}
ans = max(ans, b[i] - res);
}
cout << ans << endl;
system("pause");
return 0;
}