166432025-05-07 17:55:15sztomiMaximális szorzat (50 pont)cpp17Time limit exceeded 43/50600ms5488 KiB
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n, k, b;
    cin >> n >> k >> b;
    vector<int> poz;
    vector<int> neg;
    int temp;
    for(int i = 0; i < n; i++){
        cin >> temp;
        if(temp >= 0){
            poz.push_back(temp);
        }
        else if(temp < 0){
            neg.push_back(temp);
        }
    }
    sort(neg.begin(), neg.end(), greater<int>());


    if(neg.size() < b){
        cout << "-1\n";
        return 0;
    }

    int kell = neg.size()-b;
    int elhasznal = 0;
    for(int i = 0; i < kell; i++){
        elhasznal += -neg[i];
        poz.push_back(0);
    }

    if(elhasznal > k){
        cout << "-1\n";
        return 0;
    }
    k -= elhasznal;

    sort(poz.begin(), poz.end());
    map<int, int> dbok;
    for(int i = 0; i < poz.size(); i++){
        dbok[poz[i]]++;
    }

    int uj = 0;
    int kov;
    int also=dbok.begin()->first, also_db=dbok.begin()->second;
    //cout << "also " << also << "\n";
    auto it = dbok.begin();
    auto nxt = it;
    nxt++;
    for(; nxt != dbok.end(); it++, nxt++){
        kov = nxt->first;
        uj = (kov-also)*also_db;
        if(uj >= k){
            break;
        }
        k -= uj;
        also = nxt->first;
        also_db += nxt->second;
        //cout << uj << " " << kov << " feltolt " << also << " ig " << also_db << " db\n";
    }

    //cout << "also_db " << also_db << " k " << k << "\n";
    int osszes_novel = k / also_db;
    also += osszes_novel;
    k -= osszes_novel*also_db;
    //cout << "osszes_novel " << osszes_novel << "\n";

    also_db -= k;
    long long ret = 1;
    long long mod = 1e9 + 7;
    //cout << "vegso alsodb " << also_db << "\n";
    for(int i = 0; i < also_db; i++){
        ret *= also;
        ret %= mod;
    }
    for(int i = 0; i < k; i++){
        ret *= (also + 1);
        ret %= mod;
    }

    for(int i = also_db+k; i < poz.size(); i++){
        ret *= poz[i];
        ret %= mod;
    }

    for(int i = neg.size()-b; i < neg.size(); i++){
        ret *= abs(neg[i]);
        ret %= mod;
    }
    cout << ret << "\n";
}
SubtaskSumTestVerdictTimeMemory
base43/50
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Accepted0/01ms316 KiB
4Accepted0/01ms316 KiB
5Accepted0/04ms428 KiB
6Accepted2/21ms316 KiB
7Accepted2/21ms316 KiB
8Accepted2/21ms588 KiB
9Accepted2/21ms316 KiB
10Accepted2/27ms904 KiB
11Accepted2/282ms5244 KiB
12Accepted1/182ms5488 KiB
13Accepted1/11ms508 KiB
14Accepted1/14ms504 KiB
15Accepted1/117ms704 KiB
16Accepted1/124ms944 KiB
17Accepted1/120ms688 KiB
18Accepted1/110ms688 KiB
19Time limit exceeded0/1600ms1048 KiB
20Time limit exceeded0/1600ms1040 KiB
21Time limit exceeded0/1600ms1048 KiB
22Time limit exceeded0/1600ms984 KiB
23Accepted1/171ms3064 KiB
24Accepted1/182ms5104 KiB
25Accepted2/21ms316 KiB
26Accepted2/24ms500 KiB
27Time limit exceeded0/2600ms684 KiB
28Time limit exceeded0/1600ms636 KiB
29Accepted2/221ms688 KiB
30Accepted1/159ms944 KiB
31Accepted1/132ms944 KiB
32Accepted2/21ms316 KiB
33Accepted2/259ms944 KiB
34Accepted1/159ms892 KiB
35Accepted2/259ms944 KiB
36Accepted2/259ms944 KiB
37Accepted2/259ms944 KiB
38Accepted2/259ms944 KiB
39Accepted1/11ms316 KiB