Submission #675982

#TimeUsernameProblemLanguageResultExecution timeMemory
675982as111Kas (COCI17_kas)C++14
100 / 100
352 ms392452 KiB
#include <iostream>

#define MAXN 500
#define MAX 100000

using namespace std;
int N;
int notes[MAXN + 5];
int dp[MAXN + 5][2*MAX + 5]; // for first i banknotes, difference between 2 sums + MAX, diff<MAX means negative
int main() {
	cin >> N;
	int total = 0;
	for (int i = 1; i <= N; i++) {
		cin >> notes[i];
		total += notes[i];
	}
	for (int i = 0; i <= N; i++) fill(dp[i], dp[i] + 2*MAX+5, MAX + 1);
	dp[0][MAX] = 0;
	for (int i = 0; i < N; i++) {
		for (int k = -total; k <= total; k++) {
			int j = k + MAX;
			dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + notes[i + 1]); // add to remaining
			dp[i + 1][j + notes[i + 1]] = min(dp[i + 1][j + notes[i + 1]], dp[i][j]); // add to first person
			dp[i + 1][j - notes[i + 1]] = min(dp[i + 1][j - notes[i + 1]], dp[i][j]); // add to second
		}
	}
	cout << (dp[N][MAX]+(total-dp[N][MAX])/2);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...