201382026-01-02 20:26:49szabelrBányász RPG (40 pont)cpp17Accepted 40/4027ms1076 KiB
// Bányász RPG.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<pair<int, int>> anyag(n);
    for (int i = 0; i < n; i++)
    {
        cin >> anyag[i].second;
    }
    for (int i = 0; i < n; i++)
    {
        cin >> anyag[i].first;
    }
    sort(anyag.begin(), anyag.end(),[](auto s1, auto s2){
        return s1.second < s2.second;
    });
    int i = 0;
    int j = n-1;
    long long ido = 0;
    long long exp = 0;
    while (i <= j)
    {
        if ( exp >= anyag[i].second)
        {
            exp += anyag[i].first;
            ido += anyag[i].first;
                i++;
        }
        else {
            long long kell = anyag[i].second - exp;
            if (kell >= anyag[j].first)
            {
                exp += anyag[j].first;
                ido += 2LL * anyag[j].first;
                j--;
            }
            else
            {
                exp += kell;
                ido += 2LL * kell;
                anyag[j].first -= kell;
            }
        }
        
    }
    cout << ido;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/01ms316 KiB
2Accepted0/06ms316 KiB
3Accepted2/21ms500 KiB
4Accepted2/21ms316 KiB
5Accepted2/24ms316 KiB
6Accepted2/27ms564 KiB
7Accepted2/22ms316 KiB
8Accepted2/22ms316 KiB
9Accepted3/31ms316 KiB
10Accepted3/31ms548 KiB
11Accepted3/31ms316 KiB
12Accepted3/31ms316 KiB
13Accepted4/41ms316 KiB
14Accepted4/41ms508 KiB
15Accepted2/216ms592 KiB
16Accepted2/221ms820 KiB
17Accepted2/217ms820 KiB
18Accepted2/227ms1076 KiB