prithivMLmods/Coder-Stat huggingface dataset:
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;
using ll = long long int;

int main(void) {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  int mx = 0, score;
  vector<int> s(n, 1);
  do {
    score = 0;
    // for (int i = 0; i < n; i++) cout << s[i];
    // cout << " ";
    for (int i = 0; i < q; i++) {
      if (s[b[i] - 1] - s[a[i] - 1] == c[i])
        score += d[i];
    }
    // cout << score << endl;
    mx = max(mx, score);
    for (int i = 0; i < n; i++) {
      if (i == 0)
        s[n - 1 - i]++;
      if (s[n - 1 - i] > m) {
        s[n - 2 - i]++;
        s[n - 1 - i] = s[n - 2 - i];
      }
    }
  } while (s[0] != m);
  cout << mx << endl;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;
using ll = long long int;

int main(void) {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  int mx = 0, score;
  vector<int> s(n, 1);
  do {
    score = 0;
    // for (int i = 0; i < n; i++) cout << s[i];
    // cout << " ";
    for (int i = 0; i < q; i++) {
      if (s[b[i] - 1] - s[a[i] - 1] == c[i])
        score += d[i];
    }
    // cout << score << endl;
    mx = max(mx, score);
    s[n - 1]++;
    if (m != 1) {
      for (int i = 0; i < n; i++) {
        if (s[n - 1 - i] > m) {
          s[n - 2 - i]++;
          for (int j = 0; j <= i; j++) {
            s[n - 1 - i + j] = s[n - 2 - i];
          }
        }
      }
    }
  } while (s[0] != m);
  cout << mx << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

#ifndef ONLINE_JUDGE
#include <fstream>
#define cin fin
#define cout fout
ifstream fin("test.in");
ofstream fout("test.out");
#endif

int p[10];
int n, m, q;
int a[60], b[60], c[60], d[60];

int f(int i, int fn) {
  if (i == n + 1) {
    int r = 0;
    /*for (int j = 1; j <= n; j++)
      cout << p[j] << ' ';*/
    for (int j = 1; j <= q; j++) {
      if (p[b[j]] - p[a[j]] == c[j])
        r += d[j];
    }
    // cout << r << endl;
    return r;
  }
  int r = 0;
  for (int j = fn; j <= m;
       j++) { // intram cu toate permutarile de lungime n cu valori de la 1 la m
    p[i] = j;
    r = max(r, f(i + 1, j));
  }
  return r;
}

int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  /*for (int i = 0; i < q; i++) {
    a[i]--; b[i]--;
  }*/
  cout << f(1, 1) << endl;
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

#ifndef ONLINE_JUDGE
#include <fstream>
#define cin fin
#define cout fout
ifstream fin("test.in");
ofstream fout("test.out");
#endif

int p[11];
int n, m, q;
int a[60], b[60], c[60], d[60];

int f(int i, int fn) {
  if (i == n + 1) {
    int r = 0;
    /*for (int j = 1; j <= n; j++)
      cout << p[j] << ' ';*/
    for (int j = 1; j <= q; j++) {
      if (p[b[j]] - p[a[j]] == c[j])
        r += d[j];
    }
    // cout << r << endl;
    return r;
  }
  int r = 0;
  for (int j = fn; j <= m;
       j++) { // intram cu toate permutarile de lungime n cu valori de la 1 la m
    p[i] = j;
    r = max(r, f(i + 1, j));
  }
  return r;
}

int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  /*for (int i = 0; i < q; i++) {
    a[i]--; b[i]--;
  }*/
  cout << f(1, 1) << endl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int MOD = 1e9 + 7;
using ll = long long;

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<ll> a(n), b(n), c(n), d(n);
  rep(i, q) {
    int A, B, C, D;
    cin >> A >> B >> C >> D;
    A--;
    B--;
    a[i] = A;
    b[i] = B;
    c[i] = C;
    d[i] = D;
  }
  vector<vector<ll>> memo;
  for (int A = 1; A <= m; A++) {
    for (int B = A; B <= m; B++) {
      for (int C = B; C <= m; C++) {
        for (int D = C; D <= m; D++) {
          for (int E = D; E <= m; E++) {
            for (int F = E; F <= m; F++) {
              for (int G = F; G <= m; G++) {
                for (int H = G; H <= m; H++) {
                  for (int I = H; I <= m; I++) {
                    for (int J = I; J <= m; J++) {
                      memo.push_back({A, B, C, D, E, F, G, H, I, J});
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  ll ans = 0;
  ll l = memo.size();
  for (ll i = 0; i < l; i++) {
    const vector<ll> test = memo[i];
    if (test.size() == 0)
      continue;
    ll res = 0;
    rep(j, q) {
      if (test[b[j]] - test[a[j]] == c[j])
        res += d[j];
    }
    ans = max(ans, res);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int MOD = 1e9 + 7;
using ll = long long;

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<ll> a(q), b(q), c(q), d(q);
  rep(i, q) {
    int A, B, C, D;
    cin >> A >> B >> C >> D;
    A--;
    B--;
    a[i] = A;
    b[i] = B;
    c[i] = C;
    d[i] = D;
  }
  vector<vector<ll>> memo;
  for (int A = 1; A <= m; A++) {
    for (int B = A; B <= m; B++) {
      for (int C = B; C <= m; C++) {
        for (int D = C; D <= m; D++) {
          for (int E = D; E <= m; E++) {
            for (int F = E; F <= m; F++) {
              for (int G = F; G <= m; G++) {
                for (int H = G; H <= m; H++) {
                  for (int I = H; I <= m; I++) {
                    for (int J = I; J <= m; J++) {
                      memo.push_back({A, B, C, D, E, F, G, H, I, J});
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  ll ans = 0;
  ll l = memo.size();
  for (ll i = 0; i < l; i++) {
    const vector<ll> test = memo[i];
    if (test.size() == 0)
      continue;
    ll res = 0;
    rep(j, q) {
      if (test[b[j]] - test[a[j]] == c[j])
        res += d[j];
    }
    ans = max(ans, res);
  }
  cout << ans << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>

// などの必要なインクルードファイルすべて（3章の各標準ライブラリの説明でそれぞれ書いています）
using namespace std;

int N, M, Q;

int a[50][4];

int A[10];

int t, c;

int tokuten(int k) {

  int i, j;

  if (k == N) {

    c = 0;

    for (j = 0; j < Q; j++) {

      if ((A[a[j][1] - 1] - A[a[j][0] - 1]) == a[j][2]) {

        c = c + a[j][3];
      }
    }

    if ((t < c)) {
      t = c;
    } // 最高点dしたら入れ替え

  } else {

    if (k == 0) {

      for (i = 1; i < M + 1; i++) {

        A[k] = i;

        tokuten(k + 1);
      }

    } else {

      for (i = A[k - 1]; i < M + 1; i++) {

        A[k] = i;

        tokuten(k + 1);
      }
    }
  }
}

int main(void) {

  int i, j;

  t = 0;

  scanf("%d%d%d", &N, &M, &Q);

  for (i = 0; i < Q; i++) {

    scanf("%d%d%d%d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
  }

  for (i = 0; i < N; i++) {

    A[i] = 1;
  }

  tokuten(0);

  printf("%d", t);
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <set>
#include <stdio.h>
#include <stdlib.h>

// などの必要なインクルードファイルすべて（3章の各標準ライブラリの説明でそれぞれ書いています）
using namespace std;

int N, M, Q;

int a[50][4];

int A[10];

int t, c;

void tokuten(int k) {

  int i, j;

  if (k == N) {

    c = 0;

    for (j = 0; j < Q; j++) {

      if ((A[a[j][1] - 1] - A[a[j][0] - 1]) == a[j][2]) {

        c = c + a[j][3];
      }
    }

    if ((t < c)) {
      t = c;
    } // 最高点dしたら入れ替え

  } else {

    if (k == 0) {

      for (i = 1; i < M + 1; i++) {

        A[k] = i;

        tokuten(k + 1);
      }

    } else {

      for (i = A[k - 1]; i < M + 1; i++) {

        A[k] = i;

        tokuten(k + 1);
      }
    }
  }
}

int main(void) {

  int i, j;

  t = 0;

  scanf("%d%d%d", &N, &M, &Q);

  for (i = 0; i < Q; i++) {

    scanf("%d%d%d%d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
  }

  for (i = 0; i < N; i++) {

    A[i] = 1;
  }

  tokuten(0);

  printf("%d", t);
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, m, q;
vector<int> a, b, c, d;
int ans = 0;
void dfs(vector<int> k) {
  if ((int)k.size() == m + 1) {
    int sco = 0;
    for (int i = 0; i < q; i++) {
      if (k.at(b.at(i)) - k.at(a.at(i)) == c.at(i))
        sco += d.at(i);
    }
    ans = max(ans, sco);
    return;
  }

  k.push_back(k.back());
  while (k.back() <= m) {
    dfs(k);
    k.back()++;
  }
}

int main() {
  cin >> n >> m >> q;
  a = b = c = d = vector<int>(q);
  for (int i = 0; i < q; i++)
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);

  vector<int> sam(1, 1);
  dfs(sam);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n, m, q;
vector<int> a, b, c, d;
int ans = 0;
void dfs(vector<int> k) {
  if ((int)k.size() == n + 1) {
    int sco = 0;
    for (int i = 0; i < q; i++) {
      if (k.at(b.at(i)) - k.at(a.at(i)) == c.at(i))
        sco += d.at(i);
    }
    ans = max(ans, sco);
    return;
  }

  k.push_back(k.back());
  while (k.back() <= m) {
    dfs(k);
    k.back()++;
  }
}

int main() {
  cin >> n >> m >> q;
  a = b = c = d = vector<int>(q);
  for (int i = 0; i < q; i++)
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);

  vector<int> sam(1, 1);
  dfs(sam);
  cout << ans << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
/*input
10 10 1
1 10 9 1
*/
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define mod 998244353
#define int long long
int n, m, q;
struct node {
  int a, b, c, d;
  node(int a1, int b1, int c1, int d1) : a(a1), b(b1), c(c1), d(d1) {}
  node() {}
};

vector<node> Q;
int ans = 0;

int cal(vector<int> &v) {
  int ans = 0;
  for (auto it : Q) {
    if (v[it.b] - v[it.a] == it.c)
      ans += it.d;
  }
  return ans;
}

void rec(int index, int val, vector<int> &v) {
  if (index == n) {
    ans = max(ans, cal(v));
    return;
  }
  v[index] = val;
  for (int i = val; i <= m; i++) {
    rec(index + 1, i, v);
  }
}

signed main() {

  cin >> n >> m >> q;
  Q.resize(q);
  for (int i = 0; i <= q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    b--;
    a--;
    Q[i] = node(a, b, c, d);
  }
  vector<int> v(n);
  rec(0, 1, v);
  cout << ans << endl;
}

Fixed submission:
/*input
10 10 1
1 10 9 1
*/
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define mod 998244353
#define int long long
int n, m, q;
struct node {
  int a, b, c, d;
  node(int a1, int b1, int c1, int d1) : a(a1), b(b1), c(c1), d(d1) {}
  node() {}
};

vector<node> Q;
int ans = 0;

int cal(vector<int> &v) {
  int ans = 0;
  for (auto it : Q) {
    if (v[it.b] - v[it.a] == it.c)
      ans += it.d;
  }
  return ans;
}

void rec(int index, int val, vector<int> &v) {
  if (index == n) {
    ans = max(ans, cal(v));
    return;
  }
  v[index] = val;
  for (int i = val; i <= m; i++) {
    rec(index + 1, i, v);
  }
}

signed main() {

  cin >> n >> m >> q;
  Q.resize(q);
  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    b--;
    a--;
    Q[i] = node(a, b, c, d);
  }
  vector<int> v(n);
  rec(0, 1, v);
  cout << ans << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const ll INF = 1LL << 60;
long long N;
long long M;
long long Q;
vector<ll> a, b, c, d;
ll ans = 0;

// i: 何回めか
// v: 配列
void dfs(int cnt, int i, vector<int> v) {
  if (cnt >= N) {
    // 操作
    // rep(k, v.size()) {
    //     cout << v[k] << " ";
    // }
    // cout << endl;
    ll tmp = 0;
    rep(k, Q) {
      ll diff = v[b[k]] - v[a[k]];
      if (diff == c[k]) {
        tmp += d[k];
      }
    }
    ans = max(ans, tmp);
    return;
  }
  for (int m = i; m <= M; m++) {
    dfs(cnt + 1, m, v);
  }
}

int main() {
  scanf("%lld", &N);
  scanf("%lld", &M);
  scanf("%lld", &Q);
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);

  for (int i = 0; i < Q; i++) {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
    scanf("%lld", &c[i]);
    scanf("%lld", &d[i]);
    a[i]--;
    b[i]--;
  }

  // vector<int> tmp;
  // dfs(0, 1, tmp);
  // return 0;

  for (int i = 1; i <= M; i++) {
    vector<int> tmp;
    dfs(0, i, tmp);
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const ll INF = 1LL << 60;
long long N;
long long M;
long long Q;
vector<ll> a, b, c, d;
ll ans = 0;

// i: 何回めか
// v: 配列
void dfs(int cnt, int i, vector<int> v) {
  if (cnt >= N) {
    // 操作
    // rep(k, v.size()) {
    //     cout << v[k] << " ";
    // }
    // cout << endl;
    ll tmp = 0;
    rep(k, Q) {
      ll diff = v[b[k]] - v[a[k]];
      if (diff == c[k]) {
        tmp += d[k];
      }
    }
    ans = max(ans, tmp);
    return;
  }
  v.push_back(i);
  for (int m = i; m <= M; m++) {
    dfs(cnt + 1, m, v);
  }
}

int main() {
  scanf("%lld", &N);
  scanf("%lld", &M);
  scanf("%lld", &Q);
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);

  for (int i = 0; i < Q; i++) {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
    scanf("%lld", &c[i]);
    scanf("%lld", &d[i]);
    a[i]--;
    b[i]--;
  }

  // vector<int> tmp;
  // dfs(0, 1, tmp);
  // return 0;

  for (int i = 1; i <= M; i++) {
    vector<int> tmp;
    dfs(0, i, tmp);
  }

  cout << ans << endl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int check(vector<int> A, int Q, int r[11][4]) {
  int score = 0;
  for (int i = 0; i < Q; i++) {
    if (A[r[i][1]] - A[r[i][0]] == r[i][2]) {
      score += r[i][3];
    }
  }
  return score;
}
int main(void) {
  int N, M, Q;
  std::cin >> N >> M >> Q;
  int r[11][4];
  for (int i = 0; i < Q; i++) {
    cin >> r[i][0] >> r[i][1] >> r[i][2] >> r[i][3];
  }
  vector<int> A(N + 10);
  for (int i = 0; i <= N; i++) {
    A[i] = 1;
  }
  A[N + 1] = M;
  int digit = N, score = 0;
  while (true) {
    score = max(score, check(A, Q, r));
    if (digit < 1) {
      digit = 1;
    }
    while (A[digit] == A[digit + 1]) {
      A[digit] = 1;
      ++digit;
      if (digit > N) {
        break;
      }
    }
    if (digit > N) {
      break;
    }
    ++A[digit];
    --digit;
  }
  printf("%d\n", score);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int check(vector<int> A, int Q, int r[11][4]) {
  int score = 0;
  for (int i = 0; i < Q; i++) {
    if (A[r[i][1]] - A[r[i][0]] == r[i][2]) {
      score += r[i][3];
    }
  }
  return score;
}
int main(void) {
  int N, M, Q;
  std::cin >> N >> M >> Q;
  int r[55][4];
  for (int i = 0; i < Q; i++) {
    cin >> r[i][0] >> r[i][1] >> r[i][2] >> r[i][3];
  }
  vector<int> A(N + 10);
  for (int i = 0; i <= N; i++) {
    A[i] = 1;
  }
  A[N + 1] = M;
  int digit = N, score = 0;
  while (true) {
    score = max(score, check(A, Q, r));
    if (digit < 1) {
      digit = 1;
    }
    while (A[digit] == A[digit + 1]) {
      A[digit] = 1;
      ++digit;
      if (digit > N) {
        break;
      }
    }
    if (digit > N) {
      break;
    }
    ++A[digit];
    --digit;
  }
  printf("%d\n", score);

  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
const int N = 100005;
using namespace std;
int A[15], n, m, q, a[55], b[55], c[55], d[55], ans;
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  for (int i = 1; i <= n; i++)
    A[i] = 1;
  while (!a[0]) {
    int res = 0;
    for (int i = 1; i <= q; i++)
      if (A[b[i]] - A[a[i]] == c[i])
        res += d[i];
    ans = max(ans, res);
    for (int i = n; i >= 0; i--) {
      if (A[i] < m) {
        A[i]++;
        for (int j = i + 1; j <= n; j++)
          A[j] = A[i];
        break;
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
const int N = 100005;
using namespace std;
int A[15], n, m, q, a[55], b[55], c[55], d[55], ans;
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  for (int i = 1; i <= n; i++)
    A[i] = 1;
  while (!A[0]) {
    int res = 0;
    for (int i = 1; i <= q; i++)
      if (A[b[i]] - A[a[i]] == c[i])
        res += d[i];
    ans = max(ans, res);
    for (int i = n; i >= 0; i--) {
      if (A[i] < m) {
        A[i]++;
        for (int j = i + 1; j <= n; j++)
          A[j] = A[i];
        break;
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;
int maxv = 0;
struct node {
  int a, b, c, d;
};
node tmp[20];
int arr[20];
int n, m, q;
void dfs(int u, int last) {
  if (u == n + 1) {
    int sum = 0;
    for (int i = 1; i <= q; i++) {
      if (arr[tmp[i].b] - arr[tmp[i].a] == tmp[i].c) {
        sum += tmp[i].d;
      }
    }
    maxv = max(maxv, sum);
    return;
  }
  for (int i = last; i <= m; i++) {
    arr[u] = i;
    dfs(u + 1, i);
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    tmp[i] = {a, b, c, d};
  }
  dfs(1, 1);
  cout << maxv << endl;
}

Fixed submission:
#include <iostream>

using namespace std;
int maxv = 0;
struct node {
  int a, b, c, d;
};
node tmp[100];
int arr[20];
int n, m, q;
void dfs(int u, int last) {
  if (u == n + 1) {
    int sum = 0;
    for (int i = 1; i <= q; i++) {
      if (arr[tmp[i].b] - arr[tmp[i].a] == tmp[i].c) {
        sum += tmp[i].d;
      }
    }
    maxv = max(maxv, sum);
    return;
  }
  for (int i = last; i <= m; i++) {
    arr[u] = i;
    dfs(u + 1, i);
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    tmp[i] = {a, b, c, d};
  }
  dfs(1, 1);
  cout << maxv << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

const int max_n = 13;
int A[max_n];
int B[max_n];
int C[max_n];
int D[max_n];
int N, M, Q;
int G[max_n];

Int rec(int cp, int num) {
  Int ans = 0;
  if (cp == N) {
    for (int i = 0; i < Q; i++) {
      if (G[B[i]] - G[A[i]] == C[i])
        ans += D[i];
    }
  } else {
    for (int j = num; j <= M; j++) {
      G[cp] = j;
      ans = max(ans, rec(cp + 1, j));
    }
  }

  return ans;
}

Int solve(void) {
  Int ans = 0;
  for (int i = 1; i <= M; i++) {
    G[0] = i;
    ans = max(ans, rec(1, i));
  }

  return ans;
}

int main(void) {
  cin >> N >> M >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    A[i]--;
    B[i]--;
  }

  cout << solve() << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

const int max_n = 100;
int A[max_n];
int B[max_n];
int C[max_n];
int D[max_n];
int N, M, Q;
int G[max_n];

Int rec(int cp, int num) {
  Int ans = 0;
  if (cp == N) {
    for (int i = 0; i < Q; i++) {
      if (G[B[i]] - G[A[i]] == C[i])
        ans += D[i];
    }
  } else {
    for (int j = num; j <= M; j++) {
      G[cp] = j;
      ans = max(ans, rec(cp + 1, j));
    }
  }

  return ans;
}

Int solve(void) {
  Int ans = 0;
  for (int i = 1; i <= M; i++) {
    G[0] = i;
    ans = max(ans, rec(1, i));
  }

  return ans;
}

int main(void) {
  cin >> N >> M >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    A[i]--;
    B[i]--;
  }

  cout << solve() << endl;

  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
using namespace std;

int main() {
  int N, M, Q;
  int ans = 0;
  cin >> N >> M >> Q;
  vector<int> A(N + 2);
  vector<int> v(4 + 2, 0);
  vector<vector<int>> S(Q + 2, v);
  for (int i = 0; i < Q; i++) {
    cin >> S[i][0] >> S[i][1] >> S[i][2] >> S[i][3];
  }
  for (A[0] = 1; A[0] <= M; A[0]++) {
    for (A[1] = A[0]; A[1] <= M; A[1]++) {
      for (A[2] = A[1]; A[2] <= M; A[2]++) {
        for (A[3] = A[2]; A[3] <= M; A[3]++) {
          for (A[4] = A[3]; A[4] <= M; A[4]++) {
            for (A[5] = A[4]; A[5] <= M; A[5]++) {
              for (A[6] = A[5]; A[6] <= M; A[6]++) {
                for (A[7] = A[6]; A[7] <= M; A[7]++) {
                  for (A[8] = A[7]; A[8] <= M; A[8]++) {
                    for (A[9] = A[8]; A[9] <= M; A[9]++) {
                      int tmp = 0;
                      for (int i = 0; i < Q; i++) {
                        int e = S[i][0];
                        int f = S[i][1];
                        int g = S[i][2];
                        int h = S[i][3];
                        if (A[f - 1] - A[e - 1] == g) {
                          tmp += h;
                        }
                      }
                      ans = max(ans, tmp);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
using namespace std;

int main() {
  int N, M, Q;
  int ans = 0;
  cin >> N >> M >> Q;
  vector<int> A(N + 5);
  vector<int> v(4 + 5, 0);
  vector<vector<int>> S(Q + 5, v);
  for (int i = 0; i < Q; i++) {
    cin >> S[i][0] >> S[i][1] >> S[i][2] >> S[i][3];
  }
  for (A[0] = 1; A[0] <= M; A[0]++) {
    for (A[1] = A[0]; A[1] <= M; A[1]++) {
      for (A[2] = A[1]; A[2] <= M; A[2]++) {
        for (A[3] = A[2]; A[3] <= M; A[3]++) {
          for (A[4] = A[3]; A[4] <= M; A[4]++) {
            for (A[5] = A[4]; A[5] <= M; A[5]++) {
              for (A[6] = A[5]; A[6] <= M; A[6]++) {
                for (A[7] = A[6]; A[7] <= M; A[7]++) {
                  for (A[8] = A[7]; A[8] <= M; A[8]++) {
                    for (A[9] = A[8]; A[9] <= M; A[9]++) {
                      int tmp = 0;
                      for (int i = 0; i < Q; i++) {
                        int e = S[i][0];
                        int f = S[i][1];
                        int g = S[i][2];
                        int h = S[i][3];
                        if (A[f - 1] - A[e - 1] == g) {
                          tmp += h;
                        }
                      }
                      ans = max(ans, tmp);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<long long> factorial;
long long fact(long long n) {
  if (factorial[n] == -1)
    factorial[n] = n * fact(n - 1);
  return factorial[n];
}
long long binom(long long n, long long r) {
  return fact(n) / fact(r) / fact(n - r);
}
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  for (int i = 0; i < q; ++i)
    cin >> a[i] >> b[i] >> c[i] >> d[i];

  vector<int> B(n + m, 0);
  for (int i = 1; i <= n; ++i)
    B[i] = 1;

  factorial.assign(n + m, -1);
  factorial[0] = 1;
  int ans = 0;
  long long t = binom(n + m - 1, n);
  for (long long j = 0; j < t; ++j) {
    int cnt = 1;
    vector<int> A(n + 1, 0);
    for (int i = 0; i < n + m; ++i) {
      if (B[i] == 0) {
        ++A[cnt];
      } else {
        A[cnt + 1] = A[cnt];
        ++cnt;
      }
    }

    int tmp = 0;
    for (int i = 0; i < q; ++i) {
      if (A[b[i]] - A[a[i]] == c[i])
        tmp += d[i];
    }
    ans = max(ans, tmp);
    next_permutation(B.begin() + 1, B.end());
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<long long> factorial;
long long fact(long long n) {
  if (factorial[n] == -1)
    factorial[n] = n * fact(n - 1);
  return factorial[n];
}
long long binom(long long n, long long r) {
  return fact(n) / fact(r) / fact(n - r);
}
int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  for (int i = 0; i < q; ++i)
    cin >> a[i] >> b[i] >> c[i] >> d[i];

  vector<int> B(n + m, 0);
  for (int i = 1; i <= n; ++i)
    B[i] = 1;

  factorial.assign(n + m, -1);
  factorial[0] = 1;
  int ans = 0;
  long long t = binom(n + m - 1, n);
  for (long long j = 0; j < t; ++j) {
    int cnt = 1;
    vector<int> A(n + 1, 0);
    for (int i = 0; i < n + m; ++i) {
      if (B[i] == 0) {
        ++A[cnt];
      } else {
        if (cnt == n)
          break;
        A[cnt + 1] = A[cnt];
        ++cnt;
      }
    }

    int tmp = 0;
    for (int i = 0; i < q; ++i) {
      if (A[b[i]] - A[a[i]] == c[i])
        tmp += d[i];
    }
    ans = max(ans, tmp);
    next_permutation(B.begin() + 1, B.end());
  }

  cout << ans << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int N, M, Q;
vector<int> a, b, c, d;
ll ans;
// 参考:解説放送
void dfs(vector<int> A) {

  if (A.size() == M + 1) //([0]を1にしてi-indexedにしているため)
  {
    // calc ans
    ll ans2 = 0;
    rep(j, Q) // それぞれのクエリについて
    {
      if (A.at(b[j] - 1) - A.at(a[j] - 1) == c[j])
        ans2 += d[j];
    }
    ans = max(ans, ans2);
    return;
  }
  A.push_back(A.back());
  while (A.back() <= M) {
    dfs(A);
    A.back()++;
  }
}
int main() {
  cin >> N >> M >> Q;
  a = b = c = d = vector<int>(Q);

  rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }

  dfs(vector<int>(1, 1));

  cout << ans << endl;
}
/*vector <int> a(3);
for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }*/

Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int N, M, Q;
vector<int> a, b, c, d;
ll ans;
// 参考:解説放送
void dfs(vector<int> A) {

  if (A.size() == N + 1) //([0]を1にしてi-indexedにしているため)
  {
    // calc ans
    ll ans2 = 0;
    rep(j, Q) // それぞれのクエリについて
    {
      if (A.at(b[j] - 1) - A.at(a[j] - 1) == c[j])
        ans2 += d[j];
    }
    ans = max(ans, ans2);
    return;
  }
  A.push_back(A.back());
  while (A.back() <= M) {
    dfs(A);
    A.back()++;
  }
}
int main() {
  cin >> N >> M >> Q;
  a = b = c = d = vector<int>(Q);

  rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }

  dfs(vector<int>(1, 1));

  cout << ans << endl;
}
/*vector <int> a(3);
for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }*/
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
using vi = vector<int>;

// 特別に1を最初の要素とする
int64_t f(vector<int> &A, int i, int m, vi const &a, vi const &b, vi const &c,
          vi const &d) {
  int64_t high = 0;
  if (i < A.size()) {
    for (A[i + 1] = A[i]; A[i + 1] <= m; A[i + 1]++) {
      auto ret = f(A, i + 1, m, a, b, c, d);
      if (high < ret)
        high = ret;
    }
    return high;
  } else {
    // スコアを求める
    int q = a.size();
    int64_t score = 0;
    for (int j = 0; j < q; j++) {
      if ((A[b[j]] - A[a[j]]) == c[j])
        score += d[j];
    }
    return score;
  }
}

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  int64_t high = 0;
  vi A(n + 1); // 特別に1を最初の要素とする
  A[0] = 1;
  high = f(A, 0, m, a, b, c, d);
  cout << high;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
using vi = vector<int>;

// 特別に1を最初の要素とする
int64_t f(vector<int> &A, int i, int m, vi const &a, vi const &b, vi const &c,
          vi const &d) {
  int64_t high = 0;
  if ((i + 1) < A.size()) {
    for (A[i + 1] = A[i]; A[i + 1] <= m; A[i + 1]++) {
      auto ret = f(A, i + 1, m, a, b, c, d);
      if (high < ret)
        high = ret;
    }
    return high;
  } else {
    // スコアを求める
    int q = a.size();
    int64_t score = 0;
    for (int j = 0; j < q; j++) {
      if ((A[b[j]] - A[a[j]]) == c[j])
        score += d[j];
    }
    return score;
  }
}

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  int64_t high = 0;
  vi A(n + 1); // 特別に1を最初の要素とする
  A[0] = 1;
  high = f(A, 0, m, a, b, c, d);
  cout << high;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef tuple<int, int, int, int> T;
#define PI 3.14159265359
#define MOD 1000000007
const int MAX = 510000;
int maxd = 0;
vector<int> A(11);
int N, M, Q;
vector<int> a(51);
vector<int> b(51);
vector<int> c(51);
vector<int> d(51);
int dfs(int n, int m, int x) {
  int dn = N - n;
  A[dn] = x;
  if (n != 1) {
    for (int i = x; i <= m; i++) {
      dfs(n - 1, m, i);
    }
  } else {
    int memo = 0;
    rep(i, Q) {
      if (A[b[i]] == (A[a[i]] + c[i]))
        memo += d[i];
    }
    ::maxd = max(memo, ::maxd);
  }
}
int main() {
  cin >> N >> M >> Q;
  rep(i, Q) {
    int x, y, z, w;
    cin >> x >> y >> z >> w;
    a[i] = x - 1;
    b[i] = y - 1;
    c[i] = z;
    d[i] = w;
  }
  dfs(N, M, 1);
  cout << maxd;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef tuple<int, int, int, int> T;
#define PI 3.14159265359
#define MOD 1000000007
const int MAX = 510000;
int maxd = 0;
vector<int> A(11);
int N, M, Q;
vector<int> a(51);
vector<int> b(51);
vector<int> c(51);
vector<int> d(51);
void dfs(int n, int m, int x) {
  int dn = N - n;
  A[dn] = x;
  if (n != 1) {
    for (int i = x; i <= m; i++) {
      dfs(n - 1, m, i);
    }
  } else {
    int memo = 0;
    rep(i, Q) {
      if (A[b[i]] == (A[a[i]] + c[i]))
        memo += d[i];
    }
    ::maxd = max(memo, ::maxd);
  }
}
int main() {
  cin >> N >> M >> Q;
  rep(i, Q) {
    int x, y, z, w;
    cin >> x >> y >> z >> w;
    a[i] = x - 1;
    b[i] = y - 1;
    c[i] = z;
    d[i] = w;
  }
  dfs(N, M, 1);
  cout << maxd;
}
Problem p02695 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>      //abs()かfabs()で少数絶対値
#include <cstdlib>    //abs()で整数絶対値
#include <functional> //sort第三引数greater<型名>()で降順
#include <iomanip>    //cout<<setw(数字) で空白による桁揃え
#include <iostream>   //cout<<right で右揃え
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> A(N, 1), a(Q), b(Q), c(Q), d(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  int max = 0;
  do {
    A[N - 1]++;
    for (int i = N - 1; i >= 1; i--) {
      if (A[i] == M + 1) {
        A[i - 1]++;
        for (int j = i; j < N; j++) {
          A[j] = A[i - 1];
        }
      }
    }
    int sum = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        sum += d[i];
      }
    }
    if (sum > max)
      max = sum;
  } while (A[0] != M);
  cout << max << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>      //abs()かfabs()で少数絶対値
#include <cstdlib>    //abs()で整数絶対値
#include <functional> //sort第三引数greater<型名>()で降順
#include <iomanip>    //cout<<setw(数字) で空白による桁揃え
#include <iostream>   //cout<<right で右揃え
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> A(N, 1), a(Q), b(Q), c(Q), d(Q);
  A[N - 1] = 0;
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  int max = 0;
  do {
    A[N - 1]++;
    for (int i = N - 1; i >= 1; i--) {
      if (A[i] == M + 1) {
        A[i - 1]++;
        for (int j = i; j < N; j++) {
          A[j] = A[i - 1];
        }
      }
    }
    int sum = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        sum += d[i];
      }
    }
    if (sum > max)
      max = sum;
  } while (A[0] != M);
  cout << max << endl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, m, q, a[50], b[50], c[50], d[50], ans = 0, A[10];
int dfs(int p, int k) {
  if (p == n) {
    int sum = 0;
    for (int i = 0; i < q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        sum += d[i];
    }
    ans = max(ans, sum);
  } else {
    for (int i = k; i <= m; i++) {
      A[p] = i;
      dfs(p + 1, i);
    }
  }
}
int main() {
  cin >> n >> m >> q;
  A[0] = 1;
  for (int i = 0; i < q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  dfs(1, 1);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, q, a[50], b[50], c[50], d[50], ans = 0, A[10];
void dfs(int p, int k) {
  if (p == n) {
    int sum = 0;
    for (int i = 0; i < q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        sum += d[i];
    }
    ans = max(ans, sum);
  } else {
    for (int i = k; i <= m; i++) {
      A[p] = i;
      dfs(p + 1, i);
    }
  }
}
int main() {
  cin >> n >> m >> q;
  A[0] = 1;
  for (int i = 0; i < q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  dfs(1, 1);
  cout << ans << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \
  for (auto elementPrintVec : x) {                                             \
    cout << elementPrintVec << " ";                                            \
  }                                                                            \
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;
int ans = 0;
int n, m, q;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int score = 0;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        score += d[i];
      }
    }
    ans = max(ans, score);
    return;
  }
  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}

int main() {
  cin >> n >> m >> q;
  a.resize(n);
  b.resize(n);
  c.resize(n);
  d.resize(n);
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \
  for (auto elementPrintVec : x) {                                             \
    cout << elementPrintVec << " ";                                            \
  }                                                                            \
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;
int ans = 0;
int n, m, q;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int score = 0;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        score += d[i];
      }
    }
    ans = max(ans, score);
    return;
  }
  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}

int main() {
  cin >> n >> m >> q;
  a.resize(q);
  b.resize(q);
  c.resize(q);
  d.resize(q);
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;

int main() {
  int N, M, Q;
  int dsum = 0;
  cin >> N >> M >> Q;
  vector<vector<int>> kumi(Q, vector<int>(4));
  for (int i = 0; i < Q; i++) {
    cin >> kumi[i][0] >> kumi[i][1] >> kumi[i][2] >> kumi[i][3];
  }
  vector<int> A(N);
  for (int tmp = 0; tmp < (1 << 19); tmp++) {
    bitset<19> s(tmp);
    if (s.count() != M - 1)
      continue;
    if (s.test(N + M - 1))
      break;
    int kazu = 1;
    int j = 0;
    int sum = 0;
    for (int i = N + M - 2; i >= 0; --i) {
      if (s.test(i)) {
        ++kazu;
      } else {
        A[j] = kazu;
        ++j;
      }
    }
    for (int i = 0; i < Q; i++) {
      if (A[kumi[i][1] - 1] - A[kumi[i][0] - 1] == kumi[i][2])
        sum += kumi[i][3];
    }
    dsum = max(dsum, sum);
  }
  cout << dsum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;

int main() {
  int N, M, Q;
  int dsum = 0;
  cin >> N >> M >> Q;
  vector<vector<int>> kumi(Q, vector<int>(4));
  for (int i = 0; i < Q; i++) {
    cin >> kumi[i][0] >> kumi[i][1] >> kumi[i][2] >> kumi[i][3];
  }
  vector<int> A(N);
  for (int tmp = 0; tmp < (1 << 19); tmp++) {
    bitset<19> s(tmp);
    if (s.count() != M - 1)
      continue;
    if (N + M - 1 != 19) {
      if (s.test(N + M - 1))
        break;
    }
    int kazu = 1;
    int j = 0;
    int sum = 0;
    for (int i = N + M - 2; i >= 0; --i) {
      if (s.test(i)) {
        ++kazu;
      } else {
        A[j] = kazu;
        ++j;
      }
    }
    for (int i = 0; i < Q; i++) {
      if (A[kumi[i][1] - 1] - A[kumi[i][0] - 1] == kumi[i][2])
        sum += kumi[i][3];
    }
    dsum = max(dsum, sum);
  }
  cout << dsum << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

int ans;
int a[10], b[10], c[10], d[10];
int n, m, q;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int now = 0;
    rep(i, q) if (A[b[i]] - A[a[i]] == c[i]) now += d[i];
    ans = max(ans, now);
    return;
  }

  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}

int main() {
  cin >> n >> m >> q;
  rep(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];

  dfs(vector<int>(1, 1));
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

int ans;
int a[50], b[50], c[50], d[50];
int n, m, q;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int now = 0;
    rep(i, q) if (A[b[i]] - A[a[i]] == c[i]) now += d[i];
    ans = max(ans, now);
    return;
  }

  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}

int main() {
  cin >> n >> m >> q;
  rep(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];

  dfs(vector<int>(1, 1));
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

template <class T> inline void read(T &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
  return;
}

int a[20], b[20], c[20], d[20], x[20];
int n, m, q, ans;

void dfs(int now, int last) {
  if (now > n) {
    int s = 0;
    for (int i = 1; i <= q; ++i) {
      if (x[b[i]] - x[a[i]] == c[i]) {
        s += d[i];
      }
    }
    ans = std::max(ans, s);
    return;
  }
  for (int i = last; i <= m; ++i) {
    x[now] = i;
    dfs(now + 1, i);
  }
}

int main() {
  read(n), read(m), read(q);
  for (int i = 1; i <= q; ++i)
    read(a[i]), read(b[i]), read(c[i]), read(d[i]);
  dfs(1, 1);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

template <class T> inline void read(T &x) {
  x = 0;
  int f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
  return;
}

int a[60], b[60], c[60], d[60], x[20];
int n, m, q, ans;

void dfs(int now, int last) {
  if (now > n) {
    int s = 0;
    for (int i = 1; i <= q; ++i) {
      if (x[b[i]] - x[a[i]] == c[i]) {
        s += d[i];
      }
    }
    ans = std::max(ans, s);
    return;
  }
  for (int i = last; i <= m; ++i) {
    x[now] = i;
    dfs(now + 1, i);
  }
}

int main() {
  read(n), read(m), read(q);
  for (int i = 1; i <= q; ++i)
    read(a[i]), read(b[i]), read(c[i]), read(d[i]);
  dfs(1, 1);
  printf("%d\n", ans);
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int save[1050][20];
int now[20];
int n, m, Q, tot;
void dfs(int index, int p) {
  if (index == n) {
    for (int i = 0; i < n; i++) {
      save[tot][i] = now[i];
    }
    tot++;
    return;
  }
  if (index > n) {
    return;
  }
  for (int i = p; i <= m; i++) {
    now[index] = i;
    dfs(index + 1, i);
  }
}
void init() {
  tot = 0;
  dfs(0, 1);
}

int qu[60][10];

int get_sum(int index) {
  int sum = 0;
  for (int i = 0; i < Q; i++) {
    if (save[index][qu[i][1] - 1] - save[index][qu[i][0] - 1] == qu[i][2]) {
      sum += qu[i][3];
    }
  }
  return sum;
}

int main() {
  cin >> n >> m >> Q;
  init();
  for (int i = 0; i < Q; i++) {
    cin >> qu[i][0] >> qu[i][1] >> qu[i][2] >> qu[i][3];
  }
  int maxx = 0;
  for (int i = 0; i < tot; i++) {
    int now = get_sum(i);
    if (now >= maxx) {
      maxx = now;
    }
  }
  cout << maxx << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int save[100000][20];
int now[20];
int n, m, Q, tot;
void dfs(int index, int p) {
  if (index == n) {
    for (int i = 0; i < n; i++) {
      save[tot][i] = now[i];
    }
    tot++;
    return;
  }
  if (index > n) {
    return;
  }
  for (int i = p; i <= m; i++) {
    now[index] = i;
    dfs(index + 1, i);
  }
}
void init() {
  tot = 0;
  dfs(0, 1);
}

int qu[60][10];

int get_sum(int index) {
  int sum = 0;
  for (int i = 0; i < Q; i++) {
    if (save[index][qu[i][1] - 1] - save[index][qu[i][0] - 1] == qu[i][2]) {
      sum += qu[i][3];
    }
  }
  return sum;
}

int main() {
  cin >> n >> m >> Q;
  init();
  for (int i = 0; i < Q; i++) {
    cin >> qu[i][0] >> qu[i][1] >> qu[i][2] >> qu[i][3];
  }
  int maxx = 0;
  for (int i = 0; i < tot; i++) {
    int now = get_sum(i);
    if (now >= maxx) {
      maxx = now;
    }
  }
  cout << maxx << endl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define rep(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;

const int mn = 17;
int a[mn], b[mn], c[mn], d[mn], v[mn] = {1};
int n, m, q, ans;

void dfs(int x) {
  if (x == n + 1) {
    int sum = 0;
    for (int i = 1; i <= q; ++i) {
      if (v[b[i]] - v[a[i]] == c[i])
        sum += d[i];
    }
    ans = max(ans, sum);
    return;
  }
  for (int i = v[x - 1]; i <= m; ++i) {
    v[x] = i;
    dfs(x + 1);
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &q);
  rep(i, 1, q) scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
  dfs(1);
  printf("%d", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define rep(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;

const int mn = 57;
int a[mn], b[mn], c[mn], d[mn], v[mn] = {1};
int n, m, q, ans;

void dfs(int x) {
  if (x == n + 1) {
    int sum = 0;
    for (int i = 1; i <= q; ++i) {
      if (v[b[i]] - v[a[i]] == c[i])
        sum += d[i];
    }
    ans = max(ans, sum);
    return;
  }
  for (int i = v[x - 1]; i <= m; ++i) {
    v[x] = i;
    dfs(x + 1);
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &q);
  rep(i, 1, q) scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
  dfs(1);
  printf("%d", ans);
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 10 + 5;
int a[maxn], b[maxn], c[maxn], d[maxn];
int n, m, Q;
int ans = 0;
int res[maxn];
void dfs(int step, int last) {
  if (step == n + 1) {
    int cost = 0;
    for (int i = 1; i <= Q; i++) {
      if (res[b[i]] - res[a[i]] == c[i])
        cost += d[i];
    }
    ans = max(ans, cost);
    return;
  }
  for (int i = last; i <= m; i++) {
    res[step] = i;
    dfs(step + 1, i);
    res[step] = 0;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> Q;
  for (int i = 1; i <= Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(1, 1);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 50 + 5;
int a[maxn], b[maxn], c[maxn], d[maxn];
int n, m, Q;
int ans = 0;
int res[maxn];
void dfs(int step, int last) {
  if (step == n + 1) {
    int cost = 0;
    for (int i = 1; i <= Q; i++) {
      if (res[b[i]] - res[a[i]] == c[i])
        cost += d[i];
    }
    ans = max(ans, cost);
    return;
  }
  for (int i = last; i <= m; i++) {
    res[step] = i;
    dfs(step + 1, i);
    res[step] = 0;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> Q;
  for (int i = 1; i <= Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(1, 1);
  cout << ans << endl;
  return 0;
}