116572024-11-03 17:36:46MagyarKendeSZLGMaximális szorzat (50 pont)cpp17Elfogadva 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";
}

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