201392026-01-02 20:28:23szabelrBányász RPG (40 pont)cpp17Accepted 40/4028ms1080 KiB
#include <vector>
#include <iostream>
#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)
	{
		while (i <= j and exp >= anyag[i].second)
		{
			exp += anyag[i].first;
			ido += anyag[i].first;
			i++;
		}
		if (i > j) break;
		long long kell = anyag[i].second - exp;
		if (kell >= anyag[j].first)
		{
			exp += anyag[j].first;
			ido += 2 * anyag[j].first;
			j--;
		}
		else
		{
			exp += kell;
			ido += 2 * kell;
			anyag[j].first -= kell;
		}
	}
	cout << ido;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/01ms316 KiB
2Accepted0/06ms316 KiB
3Accepted2/21ms508 KiB
4Accepted2/21ms316 KiB
5Accepted2/24ms316 KiB
6Accepted2/27ms564 KiB
7Accepted2/22ms316 KiB
8Accepted2/22ms316 KiB
9Accepted3/31ms500 KiB
10Accepted3/31ms316 KiB
11Accepted3/31ms316 KiB
12Accepted3/31ms496 KiB
13Accepted4/41ms316 KiB
14Accepted4/41ms316 KiB
15Accepted2/216ms644 KiB
16Accepted2/220ms820 KiB
17Accepted2/217ms820 KiB
18Accepted2/228ms1080 KiB