113642024-08-26 18:36:34GervidBányász RPG (40 pont)cpp17Elfogadva 40/4029ms1768 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>

using namespace std;

int main()
{
	iostream::sync_with_stdio(0);
	cin.tie(0);

	int n, i;
	cin >> n;

	vector<pair<long long, long long>> resources(n); // {xp treshold, needed amount}
	for (i = 0; i < n; i++)
	{
		cin >> resources[i].first;
	}
	for (i = 0; i < n; i++)
	{
		cin >> resources[i].second;
	}

	sort(resources.begin(), resources.end()); //sort by the xp tresholds

	long long xp = 0, ans = 0, j = n - 1;

	for (i = 0; i < n && i <= j; i++)
	{
		while (xp < resources[i].first && j > i) //while not enough xp for next easiest, do the hardest
		{
			long long twice = min(resources[j].second, resources[i].first - xp); //get as much as (there is from the material/needed for the next xp treshold)
			resources[j].second -= twice; //document the changes

			xp += twice;
			ans += 2 * twice;

			if (resources[j].second == 0) j--; //get the hardest not completed material
		}

		long long twice = min(resources[i].second, max((long long)0, (resources[i].first - xp))); //get as much as (needed for the xp treshold/there is from the material)
		long long once = resources[i].second - twice; //if there is a remainder, we get it twice as fast

		xp += once + twice;
		ans += once + 2 * twice;
	}

	cout << ans;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
base40/40
1Elfogadva0/03ms540 KiB
2Elfogadva0/07ms680 KiB
3Elfogadva2/23ms504 KiB
4Elfogadva2/23ms416 KiB
5Elfogadva2/26ms616 KiB
6Elfogadva2/212ms744 KiB
7Elfogadva2/24ms504 KiB
8Elfogadva2/24ms504 KiB
9Elfogadva3/32ms376 KiB
10Elfogadva3/33ms504 KiB
11Elfogadva3/32ms232 KiB
12Elfogadva3/33ms496 KiB
13Elfogadva4/43ms512 KiB
14Elfogadva4/42ms232 KiB
15Elfogadva2/217ms1396 KiB
16Elfogadva2/224ms1500 KiB
17Elfogadva2/218ms1256 KiB
18Elfogadva2/229ms1768 KiB