155622025-02-20 12:53:53xxxMaximális szorzat (50 pont)cpp17Elfogadva 50/5054ms3544 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int INF = 3000000;
const int MOD = 1e9+7;


signed main() {
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    int n, k, b;
    cin >> n >> k >> b;

    vector<int> a(n+1);
    vector<int> negv;
    priority_queue<int, vector<int>, greater<int>> pq;
    int negc = 0, negmax = -INF;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        if (a[i] < 0) {
            negc++;
            negv.push_back((-1)*a[i]);
        } else {
            pq.push(a[i]);
        }
    }

    if (negc < b) {
        cout << -1 << endl;
        return 0;
    }
    sort(negv.begin(), negv.end());

    int bc = negc-b, kc = k;
    for(int i = 0; i < negc; i++) {
        if (bc > 0) {
            if (negv[i] > kc) {
                cout << -1 << endl;
                return 0;
            } else {
                kc -= negv[i];
                bc--;
                negv[i] = 0;
                pq.push(0);
            }
        }
    }

    if (pq.empty()) {
        priority_queue<int, vector<int>, greater<int> > pq2;
        for(int i = 0; i < negc; i++) {
            pq2.push(negv[i]*(-1));
        }

        while(!pq2.empty() && kc > 0) {
            int X = pq2.top();
            //cout << X << ' ';
            pq2.pop();
            if (X == -1) {
                cout << -1 << endl;
                return 0;
            }
            pq2.push(X+1);
            kc--;
        }
        /*4 4 4
-3 -2 -1 -1*/

        int ans = 1;

        while(!pq2.empty()) {
            ans *= pq2.top();
            pq2.pop();
            ans %= MOD;
        }
        cout << ans << endl;

        return 0;

    }

    while (kc > 0) {
        int X = pq.top();
        pq.pop();
        pq.push(X+1);
        kc--;
    }

    int ans = 1;

    for(int i = 0; i < negc; i++) {
        if (negv[i] != 0) {
            ans *= negv[i];
            ans %= MOD;
        }
    }
    //cout << ans << "n\n";

    while(!pq.empty()) {
        int X = pq.top();
        ans *= X;
        ans %= MOD;
        pq.pop();
    }
    cout << ans << endl;

}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base50/50
1Elfogadva0/01ms316 KiB
2Elfogadva0/01ms500 KiB
3Elfogadva0/01ms332 KiB
4Elfogadva0/01ms316 KiB
5Elfogadva0/04ms608 KiB
6Elfogadva2/21ms316 KiB
7Elfogadva2/21ms316 KiB
8Elfogadva2/21ms316 KiB
9Elfogadva2/21ms316 KiB
10Elfogadva2/24ms564 KiB
11Elfogadva2/243ms2200 KiB
12Elfogadva1/146ms2268 KiB
13Elfogadva1/11ms316 KiB
14Elfogadva1/14ms756 KiB
15Elfogadva1/110ms1272 KiB
16Elfogadva1/129ms1436 KiB
17Elfogadva1/112ms1460 KiB
18Elfogadva1/18ms1456 KiB
19Elfogadva1/150ms3340 KiB
20Elfogadva1/143ms3312 KiB
21Elfogadva1/154ms3412 KiB
22Elfogadva1/119ms3544 KiB
23Elfogadva1/143ms2116 KiB
24Elfogadva1/146ms2252 KiB
25Elfogadva2/21ms316 KiB
26Elfogadva2/24ms568 KiB
27Elfogadva2/232ms1840 KiB
28Elfogadva1/132ms1888 KiB
29Elfogadva2/28ms1332 KiB
30Elfogadva1/129ms2336 KiB
31Elfogadva1/117ms2224 KiB
32Elfogadva2/22ms316 KiB
33Elfogadva2/230ms2224 KiB
34Elfogadva1/132ms2224 KiB
35Elfogadva2/234ms2180 KiB
36Elfogadva2/229ms2224 KiB
37Elfogadva2/230ms2224 KiB
38Elfogadva2/229ms2220 KiB
39Elfogadva1/12ms316 KiB