1799F - Halve or Subtract - CodeForces Solution


greedy

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define pii pair<int, int>

const long long inf = 1e18;

const int maxN = 1e6 + 10;
int a[maxN];
string s;
int n, b;
int k1, k2;

void solve() {
    cin >> n >> b >> k1 >> k2;

    for (int i = 1; i<=n; i++) {
        cin >> a[i];
    }

    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);
    
    long long ans = inf;
    for (int i = 0; i<=min(k1, k2); i++) {

        long long curAns = 0;
        vector<int> difs;

        for (int j = 1; j <= i; j++) {
            curAns += max(0, (a[j] + 1) / 2 - b);
        }
        
        int ostalo = min(n - i, k1 + k2 - 2*i);

        for (int j = i + 1; j<= i + ostalo; j++) {
            curAns+= (a[j] + 1) / 2;
            difs.pb(a[j] - (a[j] + 1)/2 - min(b, a[j]));
        }

        sort(difs.begin(), difs.end());
        for (int l = 0; l < k2 - i; l++) {
            if (difs[l] > 0 && ostalo - l <= k1 - i) break;
            curAns+=difs[l];
        }
        for (int j = i + ostalo + 1;  j<= n; j++) curAns += a[j];
        ans = min(ans, curAns);
    }

    cout << ans << endl;
}

int main() {

    int tc;

    cin >> tc;

    while(tc--) {
        solve();
    }
    
    return 0;
}


Comments

Submit
0 Comments
More Questions

1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions
1077B - Disturbed People
653A - Bear and Three Balls
794A - Bank Robbery
157A - Game Outcome
3B - Lorry
1392A - Omkar and Password
489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut