Problem p03111 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
const ll MOD = 1000000007;
const int nmax = 8;
ll N, A, B, C;
std::vector<long long> l(N + 1);
bool graph[nmax][nmax];
ll ans = LONG_MAX;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

ll mulMod(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); }

ll calc(vector<ll> v) {
  ll cost = 0;
  ll a = 0, b = 0, c = 0;
  for (auto &&i : v) {
    switch (i) {
    case 1:
      if (a != 0)
        cost += 10;
      a += i;
      break;
    case 2:
      if (b != 0)
        cost += 10;
      b += i;
      break;
    case 3:
      if (c != 0)
        cost += 10;
      c += i;
      break;
    default:
      break;
    }
  }
  cost += abs(A - a);
  cost += abs(B - b);
  cost += abs(C - c);
  return cost;
}

int dfs(int n, int na, int a, int nb, int b, int nc, int c) {
  if (n == N) {
    if (na == 0 || nb == 0 || nc == 0)
      return 1000000000;

    return abs(A - a) + abs(B - b) + abs(C - c) + 10 * (na + nb + nc - 3);
  }

  int tmp1, tmp2, tmp3, tmp4;
  tmp1 = dfs(n + 1, na + 1, a + l[n], nb, b, nc, c);
  tmp2 = dfs(n + 1, na, a, nb + 1, b + l[n], nc, c);
  tmp3 = dfs(n + 1, na, a, nb, b, nc + 1, c + l[n]);
  tmp4 = dfs(n + 1, na, a, nb, b, nc, c);
  int ans;
  ans = min(min(min(tmp1, tmp2), tmp3), tmp4);
  return ans;
}

int main() {
  scanf("%lld", &N);
  scanf("%lld", &A);
  scanf("%lld", &B);
  scanf("%lld", &C);

  for (int i = 0; i < N; i++) {
    scanf("%lld", &l[i]);
  }
  cout << dfs(0, 0, 0, 0, 0, 0, 0) << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
const ll MOD = 1000000007;
const int nmax = 8;
ll N, A, B, C;
std::vector<long long> l(N + 100);
bool graph[nmax][nmax];
ll ans = LONG_MAX;
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}

ll mulMod(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); }

ll calc(vector<ll> v) {
  ll cost = 0;
  ll a = 0, b = 0, c = 0;
  for (auto &&i : v) {
    switch (i) {
    case 1:
      if (a != 0)
        cost += 10;
      a += i;
      break;
    case 2:
      if (b != 0)
        cost += 10;
      b += i;
      break;
    case 3:
      if (c != 0)
        cost += 10;
      c += i;
      break;
    default:
      break;
    }
  }
  cost += abs(A - a);
  cost += abs(B - b);
  cost += abs(C - c);
  return cost;
}

int dfs(int n, int na, int a, int nb, int b, int nc, int c) {
  if (n == N) {
    if (na == 0 || nb == 0 || nc == 0)
      return 1000000000;

    return abs(A - a) + abs(B - b) + abs(C - c) + 10 * (na + nb + nc - 3);
  }

  int tmp1, tmp2, tmp3, tmp4;
  tmp1 = dfs(n + 1, na + 1, a + l[n], nb, b, nc, c);
  tmp2 = dfs(n + 1, na, a, nb + 1, b + l[n], nc, c);
  tmp3 = dfs(n + 1, na, a, nb, b, nc + 1, c + l[n]);
  tmp4 = dfs(n + 1, na, a, nb, b, nc, c);
  int ans;
  ans = min(min(min(tmp1, tmp2), tmp3), tmp4);
  return ans;
}

int main() {
  scanf("%lld", &N);
  scanf("%lld", &A);
  scanf("%lld", &B);
  scanf("%lld", &C);

  for (int i = 0; i < N; i++) {
    scanf("%lld", &l[i]);
  }
  cout << dfs(0, 0, 0, 0, 0, 0, 0) << endl;
  return 0;
}