Cod sursa(job #2447346)

Utilizator ShayTeodor Matei Shay Data 12 august 2019 22:53:45
Problema Problema rucsacului Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <bits/stdc++.h>

#define BUFFER_SIZE 1 << 17
#define NMAX 5005
#define GMAX 10005

char BUFFER[BUFFER_SIZE];
int pos = BUFFER_SIZE;
int dp[GMAX];

inline char next() {
	if (pos == BUFFER_SIZE) {
		fread(BUFFER, 1, BUFFER_SIZE, stdin);
		pos = 0;
	}

	return BUFFER[pos++];
}

inline int read() {
	int n = 0;
	char c = next();

	while (!('0' <= c && c <= '9')) {
		c = next();
	}

	while ('0' <= c && c <= '9') {
		n = (n << 3) + (n << 1) + (c - '0');
		c = next();
	}

	return n;
}

int main() {
	freopen("rucsac.in", "r", stdin);
	freopen("rucsac.out", "w", stdout);

	int n = read(), g = read();
	int w, p;
	for (int i = 1 ; i <= n ; ++i) {
		w = read(), p = read();
		for (int j = g - w ; j >= 0 ; --j) {
			dp[j + w] = std::max(dp[j + w], dp[j] + p);
		}
	}

	printf("%d\n", dp[g]);
	return 0;
}