253212026-02-19 09:57:38PappMatyasMaximális szorzat (50 pont)cpp17Accepted 50/5076ms2348 KiB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int n, k, m;
vector<long long> v;
long long modder = 1000000000 + 7;

struct int2
{
    int x;
    int y;
};

static void Increase(int index)
{
    v[index]++;
    k--;
    if(index < n - 1 && k > 0)
    {
        if(v[index + 1] < v[index])
        {
            Increase(index + 1);
        }
    }
}

static bool Compare(int2 a, int2 b)
{
    return a.x > b.x;
}

int main()
{
    cin >> n >> k >> m;
    v.resize(n);
    vector<int2> neg;

    int nCount = 0;

    for(int i = 0; i < n; i++)
    {
        cin >> v[i];

        if(v[i] < 0)
        {
            nCount++;
            int2 negative;
            negative.x = v[i];
            negative.y = i;
            neg.push_back(negative);
        }
    }
    if(nCount < m)
    {
        cout << -1;
        return 0;
    }

    sort(neg.begin(), neg.end(), Compare);

    int sz = neg.size();
    for(int i = 0; i < sz; i++)
    {
        if(nCount == m) break;
        int index = neg[i].y;
        v[index] = 0;
        k += neg[i].x;
        nCount--;
    }

    if(k < 0)
    {
        cout << -1;
        return 0;
    }

    sort(v.begin(), v.end());

    int fnn = 0;
    for(int i = 0; i < n; i++)
    {
        if(v[i] < 0) continue;
        fnn = i; break;
    }

    while(k > 0)
    {
        Increase(fnn);
    }

    nCount = 0;

    for(int i = 0; i < n; i++)
    {
        if(v[i] < 0)
        {
            nCount++;
        }
    }

    if(nCount != m)
    {
        cout << -1;
        return 0;
    }
    long long ans = 1;
    for(int i = 0; i < n; i++)
    {
        ans = (ans * abs(v[i])) % modder;
    }
    cout << ans;
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/01ms316 KiB
2Accepted0/01ms316 KiB
3Accepted0/01ms316 KiB
4Accepted0/01ms316 KiB
5Accepted0/04ms316 KiB
6Accepted2/21ms368 KiB
7Accepted2/21ms316 KiB
8Accepted2/21ms316 KiB
9Accepted2/21ms500 KiB
10Accepted2/26ms472 KiB
11Accepted2/256ms1076 KiB
12Accepted1/159ms1192 KiB
13Accepted1/11ms316 KiB
14Accepted1/16ms580 KiB
15Accepted1/120ms1116 KiB
16Accepted1/128ms1328 KiB
17Accepted1/123ms1432 KiB
18Accepted1/112ms808 KiB
19Accepted1/157ms2248 KiB
20Accepted1/143ms2308 KiB
21Accepted1/176ms2116 KiB
22Accepted1/130ms2348 KiB
23Accepted1/170ms1712 KiB
24Accepted1/161ms1308 KiB
25Accepted2/21ms316 KiB
26Accepted2/27ms796 KiB
27Accepted2/237ms1200 KiB
28Accepted1/137ms1376 KiB
29Accepted2/219ms1200 KiB
30Accepted1/176ms2220 KiB
31Accepted1/132ms1076 KiB
32Accepted2/22ms508 KiB
33Accepted2/276ms2328 KiB
34Accepted1/175ms2272 KiB
35Accepted2/276ms2288 KiB
36Accepted2/276ms2304 KiB
37Accepted2/276ms2220 KiB
38Accepted2/275ms2220 KiB
39Accepted1/11ms316 KiB