96812024-02-24 17:36:22szilAz IKPC legerősebb csapatacpp17Accepted 100/100151ms19988 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted19ms12808 KiB
2Accepted19ms13056 KiB
subtask29/9
3Accepted18ms13264 KiB
4Accepted4ms7232 KiB
5Accepted4ms7440 KiB
6Accepted18ms13720 KiB
7Accepted18ms13856 KiB
8Accepted4ms7884 KiB
9Accepted4ms8180 KiB
10Accepted4ms8312 KiB
11Accepted4ms8248 KiB
12Accepted4ms8532 KiB
subtask37/7
13Accepted21ms15192 KiB
14Accepted23ms15344 KiB
15Accepted27ms15344 KiB
16Accepted27ms15560 KiB
17Accepted27ms15536 KiB
18Accepted25ms15648 KiB
19Accepted26ms15756 KiB
20Accepted27ms15664 KiB
21Accepted28ms15764 KiB
subtask411/11
22Accepted126ms19392 KiB
23Accepted123ms19524 KiB
24Accepted125ms19660 KiB
25Accepted125ms19596 KiB
26Accepted133ms19484 KiB
27Accepted130ms19484 KiB
subtask522/22
28Accepted27ms16016 KiB
29Accepted27ms16156 KiB
30Accepted27ms16252 KiB
31Accepted4ms9860 KiB
32Accepted27ms16320 KiB
33Accepted4ms9868 KiB
34Accepted4ms9876 KiB
subtask651/51
35Accepted140ms19712 KiB
36Accepted137ms19620 KiB
37Accepted20ms11820 KiB
38Accepted144ms19728 KiB
39Accepted143ms19988 KiB
40Accepted151ms19880 KiB
41Accepted24ms12028 KiB
42Accepted125ms19920 KiB
43Accepted129ms19832 KiB
44Accepted126ms19820 KiB
45Accepted126ms19820 KiB