Cod sursa(job #2138451)

Utilizator andreigasparoviciAndrei Gasparovici andreigasparovici Data 21 februarie 2018 17:35:52
Problema Diamant Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <bits/stdc++.h>
using namespace std;

const int LMAX = 50005;
const int NMAX = 1005;
const int INF = 0x3f3f3f3f;

int n, a[NMAX], dp[LMAX], half, s;

void read() {
  scanf("%d ", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d ", &a[i]);
    s += a[i];
  }
  half = s / 2;
}

void knapsack() {
  for (int i = 1; i <= n; i++) {
    for (int j = half; j >= a[i]; j--) {
      dp[j] = max(dp[j], dp[j - a[i]] + a[i]);
    }
  }
  printf("%d %d\n", dp[half], s - dp[half]);
}

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

  read();
  knapsack();

  return 0;
}