9681 | 2024-02-24 17:36:22 | szil | Az IKPC legerősebb csapata | cpp17 | Elfogadva 100/100 | 151ms | 19988 KiB |
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const int MAXN = 100'001;
ll a[MAXN], b[MAXN], nxt[MAXN];
pair<ll, int> dp[MAXN][2]; // any - with team
vector<int> roots;
vector<int> g[MAXN];
void clear() {
for (int i = 0; i < MAXN; i++) {
dp[i][0] = {0, 0};
for (int j : {0, 1}) {
dp[i][j] = {0, 0};
}
}
}
void dfs(int u, ll lambda) {
for (int v : g[u]) {
dfs(v, lambda);
dp[u][0].first += dp[v][0].first;
dp[u][0].second += dp[v][0].second;
}
dp[u][1] = {dp[u][0].first + b[u] - lambda, dp[u][0].second - 1};
for (int v : g[u]) {
pair<ll, int> corrected = {dp[u][0].first - dp[v][0].first, dp[u][0].second - dp[v][0].second};
pair<ll, int> opt = {corrected.first + dp[v][1].first + b[u], corrected.second + dp[v][1].second};
dp[u][1] = max(dp[u][1], opt);
}
dp[u][0] = max(dp[u][0], dp[u][1]);
}
pair<ll, int> calc(ll lambda) {
pair<ll, int> res = {0, 0};
clear();
for (int root : roots) {
dfs(root, lambda);
auto best = max(dp[root][0], dp[root][1]);
res.first += best.first;
res.second += best.second;
}
res.second *= -1;
return res;
}
void solve() {
int n, k; cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
vector<ll> vals(b+1, b+n+1);
sort(vals.begin(), vals.end());
int cnt = count_if(vals.begin(), vals.end(), [](ll x){return x >= 0;});
if (cnt <= k) {
ll ans = 0;
for (int i = n-1; i >= n-k; i--) {
ans += vals[i];
}
cout << ans << "\n";
return;
}
stack<int> st;
int maxe = 0;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] <= a[i]) st.pop();
if (!st.empty()) {
g[st.top()].emplace_back(i);
}
st.push(i);
if (maxe <= a[i]) {
roots.emplace_back(i);
maxe = a[i];
}
}
ll lo = 0, hi = 1e16;
while (lo < hi) {
ll mid = (lo + hi) / 2;
if (calc(mid).second > k) {
lo = mid + 1;
} else {
hi = mid;
}
}
auto ans = calc(lo);
ll val = ans.first + ans.second * lo;
if (ans.second < k) {
int need = k - ans.second;
val += need * lo;
}
cout << val << "\n";
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
Részfeladat | Összpont | Teszt | Verdikt | Idő | Memória | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Elfogadva | 19ms | 12808 KiB | ||||
2 | Elfogadva | 19ms | 13056 KiB | ||||
subtask2 | 9/9 | ||||||
3 | Elfogadva | 18ms | 13264 KiB | ||||
4 | Elfogadva | 4ms | 7232 KiB | ||||
5 | Elfogadva | 4ms | 7440 KiB | ||||
6 | Elfogadva | 18ms | 13720 KiB | ||||
7 | Elfogadva | 18ms | 13856 KiB | ||||
8 | Elfogadva | 4ms | 7884 KiB | ||||
9 | Elfogadva | 4ms | 8180 KiB | ||||
10 | Elfogadva | 4ms | 8312 KiB | ||||
11 | Elfogadva | 4ms | 8248 KiB | ||||
12 | Elfogadva | 4ms | 8532 KiB | ||||
subtask3 | 7/7 | ||||||
13 | Elfogadva | 21ms | 15192 KiB | ||||
14 | Elfogadva | 23ms | 15344 KiB | ||||
15 | Elfogadva | 27ms | 15344 KiB | ||||
16 | Elfogadva | 27ms | 15560 KiB | ||||
17 | Elfogadva | 27ms | 15536 KiB | ||||
18 | Elfogadva | 25ms | 15648 KiB | ||||
19 | Elfogadva | 26ms | 15756 KiB | ||||
20 | Elfogadva | 27ms | 15664 KiB | ||||
21 | Elfogadva | 28ms | 15764 KiB | ||||
subtask4 | 11/11 | ||||||
22 | Elfogadva | 126ms | 19392 KiB | ||||
23 | Elfogadva | 123ms | 19524 KiB | ||||
24 | Elfogadva | 125ms | 19660 KiB | ||||
25 | Elfogadva | 125ms | 19596 KiB | ||||
26 | Elfogadva | 133ms | 19484 KiB | ||||
27 | Elfogadva | 130ms | 19484 KiB | ||||
subtask5 | 22/22 | ||||||
28 | Elfogadva | 27ms | 16016 KiB | ||||
29 | Elfogadva | 27ms | 16156 KiB | ||||
30 | Elfogadva | 27ms | 16252 KiB | ||||
31 | Elfogadva | 4ms | 9860 KiB | ||||
32 | Elfogadva | 27ms | 16320 KiB | ||||
33 | Elfogadva | 4ms | 9868 KiB | ||||
34 | Elfogadva | 4ms | 9876 KiB | ||||
subtask6 | 51/51 | ||||||
35 | Elfogadva | 140ms | 19712 KiB | ||||
36 | Elfogadva | 137ms | 19620 KiB | ||||
37 | Elfogadva | 20ms | 11820 KiB | ||||
38 | Elfogadva | 144ms | 19728 KiB | ||||
39 | Elfogadva | 143ms | 19988 KiB | ||||
40 | Elfogadva | 151ms | 19880 KiB | ||||
41 | Elfogadva | 24ms | 12028 KiB | ||||
42 | Elfogadva | 125ms | 19920 KiB | ||||
43 | Elfogadva | 129ms | 19832 KiB | ||||
44 | Elfogadva | 126ms | 19820 KiB | ||||
45 | Elfogadva | 126ms | 19820 KiB |