249462026-02-17 01:11:03999Maximális szorzat (50 pont)cpp17Runtime error 43/50101ms7480 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 0;
    }
    k-=sum;
    if(null.size()+neg.size()-b>k){
        cout<<0<<endl;
        return 0;
    }
    k-=(null.size()+neg.size()-b);
    for(int i = 0;i<null.size()+neg.size()-b;i++){
        poz.push_back(1);
    }
    int 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++;
    }
    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
base43/50
1Accepted0/01ms316 KiB
2Accepted0/01ms500 KiB
3Accepted0/01ms316 KiB
4Accepted0/01ms316 KiB
5Accepted0/03ms564 KiB
6Accepted2/21ms316 KiB
7Accepted2/21ms316 KiB
8Accepted2/21ms316 KiB
9Accepted2/21ms364 KiB
10Accepted2/26ms1076 KiB
11Accepted2/282ms7340 KiB
12Accepted1/1101ms7480 KiB
13Accepted1/11ms316 KiB
14Accepted1/14ms596 KiB
15Accepted1/110ms884 KiB
16Accepted1/114ms1288 KiB
17Accepted1/110ms948 KiB
18Accepted1/14ms948 KiB
19Runtime error0/120ms1576 KiB
20Runtime error0/116ms1580 KiB
21Runtime error0/126ms1456 KiB
22Runtime error0/112ms1436 KiB
23Accepted1/176ms4156 KiB
24Accepted1/197ms6860 KiB
25Accepted2/21ms316 KiB
26Accepted2/23ms564 KiB
27Runtime error0/214ms864 KiB
28Runtime error0/114ms1048 KiB
29Accepted2/212ms1116 KiB
30Accepted1/135ms1472 KiB
31Accepted1/123ms1456 KiB
32Accepted2/29ms508 KiB
33Accepted2/230ms1376 KiB
34Accepted1/129ms1504 KiB
35Accepted2/237ms1636 KiB
36Accepted2/235ms1472 KiB
37Accepted2/234ms1536 KiB
38Accepted2/235ms1572 KiB
39Accepted1/19ms316 KiB