116572024-11-03 17:36:46MagyarKendeSZLGMaximális szorzat (50 pont)cpp17Accepted 50/5081ms2732 KiB
#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;

const ll MOD = 1e9 + 7;

int main() {
    ll N, K, B;
    cin >> N >> K >> B;

    vector<ll> neg;
    priority_queue<ll, vector<ll>, greater<ll>> pq;

    for (int i = 0; i < N; i++) {
        ll x;
        cin >> x;
        if (x < 0) {
            neg.push_back(x);
        } else {
            pq.push(x);
        }
    }

    if (B > neg.size()) {
        cout << "-1\n";
        exit(0);
    }

    // This fucking edge case: only B negatives (you have to
    // use all K increments)
    if (B == N) {
        for (int i = 0; i < B; i++) { pq.push(neg[i]); }

        while (K--) {
            ll mn = pq.top();
            pq.pop();
            if (mn + 1 >= 0) {
                cout << "-1\n";
                exit(0);
            }
            pq.push(mn + 1);
        }

        ll result = 1;
        while (!pq.empty()) {
            ll mn = pq.top();
            pq.pop();
            result *= -mn;
            result %= MOD;
        }

        cout << result << "\n";
        exit(0);
    }

    sort(neg.begin(), neg.end());
    ll result = 1;

    for (int i = 0; i < B; i++) {
        result *= -neg[i];
        result %= MOD;
    }

    for (int i = B; i < neg.size(); i++) {
        K += neg[i];
        pq.push(0);
    }

    if (K < 0) {
        cout << "-1\n";
        exit(0);
    }

    while (K--) {
        ll mn = pq.top();
        pq.pop();
        pq.push((mn + 1) % MOD);
    }

    while (!pq.empty()) {
        ll mn = pq.top();
        pq.pop();
        result *= mn;
        result %= MOD;
    }

    cout << result << "\n";
}

SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms320 KiB
2Accepted0/01ms320 KiB
3Accepted0/01ms508 KiB
4Accepted0/01ms320 KiB
5Accepted0/04ms568 KiB
6Accepted2/21ms320 KiB
7Accepted2/21ms320 KiB
8Accepted2/21ms320 KiB
9Accepted2/21ms320 KiB
10Accepted2/27ms568 KiB
11Accepted2/272ms1432 KiB
12Accepted1/178ms1564 KiB
13Accepted1/11ms320 KiB
14Accepted1/18ms580 KiB
15Accepted1/119ms904 KiB
16Accepted1/141ms1072 KiB
17Accepted1/121ms1024 KiB
18Accepted1/112ms948 KiB
19Accepted1/172ms2528 KiB
20Accepted1/159ms2732 KiB
21Accepted1/181ms2556 KiB
22Accepted1/128ms2732 KiB
23Accepted1/176ms1296 KiB
24Accepted1/178ms1500 KiB
25Accepted2/22ms380 KiB
26Accepted2/26ms568 KiB
27Accepted2/245ms1488 KiB
28Accepted1/145ms1456 KiB
29Accepted2/219ms940 KiB
30Accepted1/163ms1456 KiB
31Accepted1/137ms1524 KiB
32Accepted2/23ms320 KiB
33Accepted2/265ms1452 KiB
34Accepted1/165ms1476 KiB
35Accepted2/268ms1368 KiB
36Accepted2/263ms1416 KiB
37Accepted2/263ms1456 KiB
38Accepted2/263ms1512 KiB
39Accepted1/12ms320 KiB