Cod sursa(job #2935949)

Utilizator lucaxsofLuca Sofronie lucaxsof Data 7 noiembrie 2022 18:45:27
Problema Energii Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <math.h>
#include <vector>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <bits/stdc++.h>
using namespace std;

ifstream fin("energii.in");
ofstream fout("energii.out");

const int maxx = 1003;

int n, g;

struct oop
{
	int w, p;
}Q[maxx];

long long dp[5003];

long long cost[5003];

void read()
{
	fin >> n >> g;
	for (int i = 1; i <= n; ++i)
	{
		fin >> Q[i].w >> Q[i].p;
	}
}

//Q[i].w = putere max;
//Q[i].p = cost minim

void fn()
{
	for (int i = 1; i <= n; ++i)
		for (int j = g - Q[i].w; j >= 0; --j)
		{
			if (dp[j + Q[i].w] < Q[i].w + dp[j])
			{
				dp[j + Q[i].w] = Q[i].w + dp[j];
				cost[j + Q[i].w] = cost[j] + Q[i].p;
			}
			else if (dp[j + Q[i].w] == Q[i].p + dp[j] && cost[j + Q[i].w] > cost[j] + Q[i].p)	cost[j + Q[i].w] = cost[j] + Q[i].p;

		}
	if (dp[g])
		fout << cost[g];
else
fout << -1;
}

int main()
{
	read();
	fn();
}