#include <bits/stdc++.h>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maxi(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)250000 + 9;
const int mod = (int)1e9 + 7;
void prepare(); void main_code();
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0; prepare();
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); }
}
void prepare() {};
int n, k, c[N];
long long s[N];
int ver[N], vl[N];
const long long INF = (long long)-1e15;
struct persis {
struct node {
int l, r;
int cnt = 0;
long long sum = 0;
node () {
l = r = -1;
}
void cp(node &x) {
l = x.l, r = x.r;
cnt = x.cnt;
sum = x.sum;
}
};
int N;
vector<node> t;
int build(int l, int r) {
int cur = sz(t);
t.push_back(node());
if (l == r) return cur;
int m = (l + r) >> 1;
int L = build(l, m);
int R = build(m + 1, r);
t[cur].l = L, t[cur].r = R;
// cerr << l << ' ' << r << ' ' << cur << ' ' << t[cur].l << ' ' << t[cur].r << nl;
return cur;
}
void refine(int id){
t[id].cnt=t[t[id].l].cnt + t[t[id].r].cnt;
t[id].sum=t[t[id].l].sum + t[t[id].r].sum;
}
int upd(int id, int l, int r, int p) {
int cur = sz(t);
t.push_back(node());
t.back().cp(t[id]);
if (l == r) {
++t[cur].cnt;
t[cur].sum += vl[l];
return cur;
}
int m = (l + r) >> 1;
if (p <= m) {
int x=upd(t[id].l, l, m, p);
t[cur].l=x;
}else{
int x=upd(t[id].r, m+1, r, p);
t[cur].r=x;
}
refine(cur);
// cerr << l << ' ' << r << ' ' << p << ' ' << id << ' ' << t[cur].l << ' ' << t[cur].r << ' ' << t[id].sum << nl;
return cur;
}
int upd(int id, int v) {
return upd(id, 1, N, v);
}
void init() {
vec<int> cc;
FOR(i, 1, n) cc.push_back(c[i]);
unq(cc);
N = sz(cc);
FOR(i, 1, N) vl[i] = cc[i - 1];
ver[0] = build(1, N);
FOR(i, 1, n) {
int x = lower_bound(all(cc), c[i]) - cc.begin() + 1;
ver[i] = upd(ver[i - 1], x);
}
}
#define rc(id) (t[id].r)
#define lc(id) (t[id].l)
pair<long long, int> get(int id1, int id2, int l, int r, int k) {
if (l == r)
return _mp(t[id1].sum - t[id2].sum, t[id1].cnt - t[id2].cnt);
int m = (l + r) >> 1;
if (t[rc(id1)].cnt - t[rc(id2)].cnt >= k)
return get(rc(id1), rc(id2), m + 1, r, k);
auto A = _mp(t[rc(id1)].sum - t[rc(id2)].sum, t[rc(id1)].cnt - t[rc(id2)].cnt);
auto B = get(lc(id1), lc(id2), l, m, k - A.second);
return _mp(A.fi + B.fi, A.se + B.se);
}
pair<long long, int> get(int l, int r) {
if (r - l + 1 < k) return _mp(INF, 0);
return get(ver[r], ver[l - 1], 1, N, k);
}
};
persis st;
long long ans = INF;
int opt[N];
long long dp[N];
void solve(int l, int r, int lb, int rb) {
if (l > r) return ;
int mid = (l + r) / 2;
pair<long long, int> mx = {2LL * INF, 0};
FOR(i, max(mid + k - 1, lb), rb) {
mx = max(mx, _mp(st.get(mid, i).first - (s[i] - s[mid - 1]), i));
}
dp[mid] = mx.first;
opt[mid] = mx.second;
maxi(ans, dp[mid]);
solve(l, mid - 1, lb, opt[mid]);
solve(mid + 1, r, opt[mid], rb);
}
void main_code() {
cin >> n >> k;
FOR(i, 1, n) cin >> s[i], s[i] += s[i - 1];
FOR(i, 1, n) cin >> c[i];
st.init();
solve(1, n, 1, n);
cout << ans;
}
/* Let the river flows naturally */
컴파일 시 표준 에러 (stderr) 메시지
trade.cpp: In function 'int main()':
trade.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
trade.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |