250132026-02-17 12:36:45999Maximális szorzat (50 pont)cpp17Runtime error 37/50103ms7368 KiB
// Source: https://usaco.guide/general/io

#include <bits/stdc++.h>
using namespace std;
#define int long long
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

int pow(int a, int b){
    if(b==0)return 1;
    if(b%2)return (pow(a,b-1)*a)%(int)(1e9+7);
    int x=pow(a,b/2);
    return (x*x)%(int)(1e9+7);
}

signed main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    int n,k,b;cin>>n>>k>>b;
    vector<int> neg,poz,null;
    for(int i = 0;i<n;i++){
        int a;cin>>a;
        if(a==0)null.push_back(a);
        else if(a>0)poz.push_back(a);
        else neg.push_back(a);
    }
    sort(neg.begin(),neg.end());
    sort(poz.rbegin(),poz.rend());
    int sum=0,ans=1;
    for(int i = neg.size()-1;i>=0;i--){
        if(i>=b)sum+=-neg[i];
        else ans=(ans*neg[i])%(int)(1e9+7);
    }
    if(sum>k||neg.size()<b){
        cout<<-1<<endl;
        return -1;
    }
    k-=sum;
    if(null.size()+neg.size()-b>k){
        cout<<0<<endl;
        return -1;
    }
    k-=(null.size()+neg.size()-b);
    for(int i = 0;i<null.size()+neg.size()-b;i++){
        poz.push_back(1);
    }
    int currn=0,currdb=0;
    if(poz.size()>0)currn=poz[0],currdb=1;
    set<pair<int,int>> s;
    for(int i = 1;i<poz.size();i++){
        if(currn!=poz[i]){
            s.insert({currn,currdb});
            currn=poz[i];
            currdb=1;
        }
        else currdb++;
    }
    if(poz.size()>0)s.insert({currn,currdb});
    //for(auto[a,b]:s)cout<<a<<' '<<b<<endl;
    while(k>0){
        auto [A,B]=*s.begin();
        if(B<=k){
            s.erase({A,B});
            auto it=s.lower_bound({A+1,0});
            pair<int,int> i=*it;
            if(it!=s.end()&&i.first==A+1){
                s.erase({i});
                s.insert({A+1,B+i.second});
            }
            else if(it==s.end()||it!=s.end()&&i.first!=A+1){
                s.insert({A+1,B});
            }
            k-=B;
        }
        else{
            s.erase({A,B});
            auto it=s.lower_bound({A+1,0});
            pair<int,int> i=*it;
            if(it!=s.end()&&i.first==A+1){
                s.erase({i});
                s.insert({A+1,k+i.second});
                s.insert({A,B-k});
            }
            else if(it==s.end()||it!=s.end()&&i.first!=A+1){
                s.insert({A+1,k});
                s.insert({A,B-k});  
            }
            break;
        }
    }
    //for(auto [a,b]:s)cout<<a<<' '<<b<<endl;
    for(auto i : s)ans=(ans*pow(i.first,i.second))%(int)(1e9+7);
    cout<<ans<<endl;
}
SubtaskSumTestVerdictTimeMemory
base37/50
1Accepted0/01ms508 KiB
2Accepted0/01ms500 KiB
3Accepted0/01ms508 KiB
4Accepted0/01ms316 KiB
5Accepted0/03ms564 KiB
6Accepted2/21ms316 KiB
7Accepted2/21ms316 KiB
8Accepted2/21ms316 KiB
9Accepted2/22ms316 KiB
10Accepted2/27ms1128 KiB
11Accepted2/278ms7368 KiB
12Accepted1/1103ms7232 KiB
13Accepted1/11ms316 KiB
14Accepted1/14ms684 KiB
15Runtime error0/110ms960 KiB
16Accepted1/114ms1196 KiB
17Runtime error0/110ms948 KiB
18Runtime error0/14ms948 KiB
19Wrong answer0/120ms1516 KiB
20Wrong answer0/116ms1348 KiB
21Wrong answer0/126ms1360 KiB
22Wrong answer0/112ms1456 KiB
23Accepted1/179ms4300 KiB
24Accepted1/197ms6884 KiB
25Accepted2/21ms316 KiB
26Accepted2/23ms564 KiB
27Wrong answer0/213ms856 KiB
28Wrong answer0/113ms1108 KiB
29Runtime error0/212ms1012 KiB
30Accepted1/135ms1456 KiB
31Runtime error0/121ms1536 KiB
32Accepted2/29ms316 KiB
33Accepted2/230ms1460 KiB
34Accepted1/129ms1456 KiB
35Accepted2/235ms1452 KiB
36Accepted2/235ms1456 KiB
37Accepted2/235ms1456 KiB
38Accepted2/235ms1456 KiB
39Accepted1/19ms316 KiB