alexjercan/bugnet huggingface dataset:
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
long mod = (long)1e9 + 7;
int n, m, q, a[50], b[50], c[50], d[50], ans = 0;

int bfs(vector<int> t) {

  if (t.size() == n) {
    int sum = 0;
    rep(i, q) {
      if (t[b[i]] - t[a[i]] == c[i])
        sum += d[i];
    }
    ans = max(ans, sum);

  } else {
    int back = 1;
    if (t.size() > 0)
      back = t.back();
    for (int i = back; i <= m; i++) {
      t.push_back(i);
      bfs(t);
      t.pop_back();
    }
  }
  return 0;
}

int main() {

  cin >> n >> m >> q;

  rep(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];
  rep(i, q) a[i]--, b[i]--;

  bfs({});

  cout << ans << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
long mod = (long)1e9 + 7;
int n, m, q, a[10], b[10], c[10], d[10], ans = 0;

int bfs(vector<int> t) {

  if (t.size() == n) {
    int sum = 0;
    rep(i, q) {
      if (t[b[i]] - t[a[i]] == c[i])
        sum += d[i];
    }
    ans = max(ans, sum);

  } else {
    int back = 1;
    if (t.size() > 0)
      back = t.back();
    for (int i = back; i <= m; i++) {
      t.push_back(i);
      bfs(t);
      t.pop_back();
    }
  }
  return 0;
}

int main() {

  cin >> n >> m >> q;

  rep(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];
  rep(i, q) a[i]--, b[i]--;

  bfs({});

  cout << ans << "\\n";

  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, d, n) for (int i = (d); i < (n); ++i)
#define all(v) v.begin(), v.end()
using P = pair<int, int>;

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

int calc() {
  int ret = 0;
  rep(i, q) {
    if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
      ret += d[i];
    }
  }
  return ret;
}

void dfs(int depth, int x = 1) {
  if (n == depth) {
    ans = max(ans, calc());
    return;
  }
  for (int i = x; i <= m; ++i) {
    A[depth] = i;
    dfs(depth + 1, i);
  }
}

signed main() {

  cin >> n >> m >> q;
  a.resize(q);
  b.resize(q);
  c.resize(q);
  d.resize(q);
  A.resize(n);
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }

  dfs(0);

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, d, n) for (int i = (d); i < (n); ++i)
#define all(v) v.begin(), v.end()
using P = pair<int, int>;

vector<string> A;
int n, m, q;

void dfs(string str, int x = 1) {
  if (n == str.length()) {
    A.push_back(str);
    return;
  }

  REP(i, x, m + 1) { dfs(str + to_string(i), i); }
}

signed main() {

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

  dfs("");

  int ans = 0;
  for (string s : A) {
    int mx = 0;
    rep(i, q) {
      if ((s[b[i] - 1] - '0') - (s[a[i] - 1] - '0') == c[i]) {
        mx += d[i];
      }
    }
    ans = max(ans, mx);
  }
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

int recursive(int col, vector<int> &current, int n, int m, int q,
              vector<vector<int>> &aa, vector<vector<int>> &cc,
              vector<vector<int>> &dd);

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<vector<int>> aa(n + 1), cc(n + 1), dd(n + 1);
  for (int i = 0; i < q; ++i) {
    aa.at(b[i]).push_back(a[i]);
    cc[b[i]].push_back(c[i]);
    dd[b[i]].push_back(d[i]);
  }

  vector<int> current(n + 1);
  current[0] = 1;
  // fprintf(stderr,"prepared completedn");
  cout << recursive(1, current, n, m, q, aa, cc, dd) << endl;
}

int recursive(int col, vector<int> &current, int n, int m, int q,
              vector<vector<int>> &aa, vector<vector<int>> &cc,
              vector<vector<int>> &dd) {
  int score, maxscore;

  // fprintf(stderr,"col %d, current %d\\n",col,current[col-1]);
  maxscore = 0;
  for (int i = current[col - 1]; i <= m; ++i) {
    score = 0;
    for (int j = 0; j < aa[col].size(); ++j) {
      if (i - current[aa[col][j]] == cc[col][j]) {
        score += dd[col][j];
      }
    }
    current[col] = i;
    if (col < n) {
      score += recursive(col + 1, current, n, m, q, aa, cc, dd);
    }
    if (score > maxscore) {
      maxscore = score;
    }
  }
  return maxscore;
}


Error: 0

Fixed submission:

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

int recursive(int col, vector<int> &current, int n, int m, int q,
              vector<vector<int>> &aa, vector<vector<int>> &cc,
              vector<vector<int>> &dd);

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<vector<int>> aa(m + 1), cc(m + 1), dd(m + 1);
  for (int i = 0; i < q; ++i) {
    aa.at(b[i]).push_back(a[i]);
    cc[b[i]].push_back(c[i]);
    dd[b[i]].push_back(d[i]);
  }

  vector<int> current(n + 1);
  current[0] = 1;
  // fprintf(stderr,"prepared completedn");
  cout << recursive(1, current, n, m, q, aa, cc, dd) << endl;
}

int recursive(int col, vector<int> &current, int n, int m, int q,
              vector<vector<int>> &aa, vector<vector<int>> &cc,
              vector<vector<int>> &dd) {
  int score, maxscore;

  // fprintf(stderr,"col %d, current %d\\n",col,current[col-1]);
  maxscore = 0;
  for (int i = current[col - 1]; i <= m; ++i) {
    score = 0;
    for (int j = 0; j < aa[col].size(); ++j) {
      if (i - current[aa[col][j]] == cc[col][j]) {
        score += dd[col][j];
      }
    }
    current[col] = i;
    if (col < n) {
      score += recursive(col + 1, current, n, m, q, aa, cc, dd);
    }
    if (score > maxscore) {
      maxscore = score;
    }
  }
  return maxscore;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF -(1 << 30)
using namespace std;
// #define P pair<long long>

map<long long, int> mp;
int Q[60][4];
int n, m, q;

int get(vector<int> &v) {
  int ret = 0;
  for (int i = 0; i < q; i++)
    if (v[Q[i][1]] - v[Q[i][0]] == Q[i][2])
      ret += Q[i][3];
  return ret;
}

int bt(vector<int> &v, int idx, int mn) {
  if (idx == n)
    return get(v);
  int ret = 0;
  for (int i = mn; i <= m; i++) {
    v[idx] = i;
    ret = max(ret, bt(v, idx + 1, i));
  }
  return ret;
}

int main() {

  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < 4; j++)
      cin >> Q[i][j];
    Q[i][0]--, Q[i][1]--;
  }
  vector<int> v(n, -1);
  cout << bt(v, 0, 1) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF -(1 << 30)
using namespace std;
// #define P pair<long long>

map<long long, int> mp;
int Q[60][4];
int n, m, q;

long long getCode(vector<int> &v) {
  long long ret = 0;
  for (int i = 0; i < v.size(); i++) {
    ret = ret * (long long)(m + 1) + ((v[i] == -1) ? 0 : v[i]);
    // cout << "HERE " << v[i] << ' ' << ret << ' ' << ret * (m + 1) << endl;
  }
  return ret;
}

bool isSorted(vector<int> &v) {
  int last = -1;
  for (int i = 0; i < v.size(); i++)
    if (v[i] != -1) {
      if (v[i] < last)
        return false;
      last = v[i];
    }
  return true;
}

int bt(vector<int> &v) {
  if (!isSorted(v)) {
    return INF;
  }
  /*for( int i = 0; i < v.size(); i++ )
      cout << v[i] << ' ';
  cout << endl;
  cout << idx << ' ' << getCode(v) << endl << endl;;
  */
  if (mp.count(getCode(v)))
    return mp[getCode(v)];
  int ret = 0;
  int curScore = 0;
  for (int idx = 0; idx < q; idx++) {
    // int ret = bt(v, idx + 1);
    if (v[Q[idx][0]] == -1 && v[Q[idx][1]] == -1) {
      for (int i = Q[idx][2] + 1; i <= m; i++) {
        v[Q[idx][1]] = i, v[Q[idx][0]] = i - Q[idx][2];
        ret = max(ret, bt(v));
        v[Q[idx][1]] = -1, v[Q[idx][0]] = -1;
      }
    } else if (v[Q[idx][0]] == -1 && v[Q[idx][1]] != -1) {
      if (v[Q[idx][1]] > Q[idx][2]) {
        v[Q[idx][0]] = v[Q[idx][1]] - Q[idx][2];
        ret = max(ret, bt(v));
        v[Q[idx][0]] = -1;
      }
    } else if (v[Q[idx][0]] != -1 && v[Q[idx][1]] == -1) {
      if (v[Q[idx][0]] + Q[idx][2] <= m) {
        v[Q[idx][1]] = v[Q[idx][0]] + Q[idx][2];
        ret = max(ret, bt(v));
        v[Q[idx][1]] = -1;
      }
    } else {
      if (v[Q[idx][1]] - v[Q[idx][0]] == Q[idx][2])
        curScore += Q[idx][3];

      // ret = max(ret, Q[idx][3] + bt(v, idx + 1));
    }
  }
  ret = max(ret, curScore);
  return mp[getCode(v)] = ret;
}

int main() {

  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    for (int j = 0; j < 4; j++)
      cin >> Q[i][j];
    Q[i][0]--, Q[i][1]--;
  }
  vector<int> v(n, -1);
  cout << bt(v) << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcd(long long a, long long b) {
  return a * b / gcd(a, b) / gcd(a, b);
}
int N, M, Q;
vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;
int dfs(vector<int> query, int index, int max) {
  if (index == N) {
    sort(query.begin(), query.end());

    int sum = 0;
    for (int i = 0; i < Q; i++) {
      if (query[b[i]] - query[a[i]] == c[i]) {
        sum += d[i];
      }
    }
    return sum;
  }
  vector<int> result;
  for (int i = max; i < M; i++) {
    vector<int> new_query = query;
    new_query[index] = i;
    result.push_back(dfs(new_query, index + 1, i));
  }
  return *max_element(result.begin(), result.end());
}
int main() {

  cin >> N >> M >> Q;
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  // 全てのやり方を試してみる
  vector<int> query(N);

  int result = dfs(query, 0, 0);
  cout << result << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcd(long long a, long long b) {
  return a * b / gcd(a, b) / gcd(a, b);
}
int N, M, Q;
vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;
int dfs(vector<int> query, int index) {
  if (index == N) {
    sort(query.begin(), query.end());

    int sum = 0;
    for (int i = 0; i < Q; i++) {
      if (query[b[i]] - query[a[i]] == c[i]) {
        sum += d[i];
      }
    }
    return sum;
  }
  vector<int> result;
  for (int i = 0; i < M; i++) {
    vector<int> new_query = query;
    new_query[index] = i;
    result.push_back(dfs(new_query, index + 1));
  }
  return *max_element(result.begin(), result.end());
}
int main() {

  cin >> N >> M >> Q;
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  // 全てのやり方を試してみる
  vector<int> query(N);

  int result = dfs(query, 0);
  cout << result << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
const long long mod = 1e9 + 7;
//////////////////////////////////
int n, m, q;
vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;

long long calc(vector<int> x) {
  // for(int i=0; i<n; i++) cerr << x[i] << " ";
  // cerr << endl;
  long long sum = 0;
  for (int i = 0; i < q; i++) {
    if (x[b[i] - 1] - x[a[i] - 1] == c[i])
      sum += d[i];
  }
  return sum;
}

void solve() {
  long long ans = 0;
  vector<int> x(n, 1);
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= m; j++) {
      x[n - 1 - i] = j;
      ans = max(ans, calc(x));
    }
  }
  cout << ans << endl;
}
long long xxx = 0;
void solve2(vector<int> x, int p) {
  if (p == 0) {
    xxx = max(xxx, calc(x));
  }
  if (p < 1)
    return;
  for (int i = 1; i <= x[p + 1]; i++) {
    x[p] = i;
    solve2(x, p - 1);
    xxx = max(xxx, calc(x));
    // for(int j=0; j<n; j++) cerr << x[j] << " ";
    // cerr << endl;
  }
}

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

  // solve();

  vector<int> x(n, 1);
  for (int i = 1; i <= m; i++) {
    x[n - 1] = i;
    solve2(x, n - 2);
  }
  cout << xxx << endl;

  return 0;
}

// EOF


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
const long long mod = 1e9 + 7;
//////////////////////////////////
int n, m, q;
vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;

long long calc(vector<int> x) {
  for (int i = 0; i < n; i++)
    cerr << x[i] << " ";
  cerr << endl;
  long long sum = 0;
  for (int i = 0; i < q; i++) {
    if (x[b[i] - 1] - x[a[i] - 1] == c[i])
      sum += d[i];
  }
  return sum;
}

void solve() {
  long long ans = 0;
  vector<int> x(n, 1);
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= m; j++) {
      x[n - 1 - i] = j;
      ans = max(ans, calc(x));
    }
  }
  cout << ans << endl;
}
long long xxx = 0;
void solve2(vector<int> x, int p) {
  if (p < 0)
    return;
  for (int i = 1; i <= x[p + 1]; i++) {
    x[p] = i;
    solve2(x, p - 1);
    xxx = max(xxx, calc(x));
    // for(int j=0; j<n; j++) cerr << x[j] << " ";
    // cerr << endl;
  }
}

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

  // solve();

  vector<int> x(n, 1);
  for (int i = 1; i <= m; i++) {
    x[n - 1] = i;
    solve2(x, n - 2);
    // cerr << endl;
  }
  cout << xxx << endl;

  return 0;
}

// EOF
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int R[55][4];
int X[11];
int N, M, Q;
int Ans;
void dfs(int x) {
  if (x > N) {
    int Cnt = 0;
    for (int i = 0; i < Q; i++)
      if (X[R[i][1]] - X[R[i][0]] == R[i][2])
        Cnt += R[i][3];
    Ans = max(Ans, Cnt);
    return;
  }
  for (int i = X[x - 1]; i <= M; i++) {
    X[x] = i;
    dfs(x + 1);
  }
  return;
}
int main() {
  cin >> N >> M >> Q;
  for (int i = 0; i < Q; i++)
    for (int j = 0; j < 4; j++)
      cin >> R[i][j];
  X[0] = 1;
  dfs(1);
  cout << Ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int X[55][4];
int Ch[55][3], PC;
int Deg[11], N, M, Q;
int Cnt[11];
vector<int> Topo;
int Ans;
bool Unu[11];
void dfs(int x, int sum) {
  if (x == Q) {
    for (int i = 0; i < 11; i++)
      Cnt[i] = 0, Unu[i] = true;
    for (int i = 0; i < N; i++)
      for (int j = 0; j < PC; j++)
        if (Ch[j][0] == Topo[i])
          if (!Unu[Ch[j][1]] && Cnt[Ch[j][1]] != Cnt[Ch[j][0]] + Ch[j][2])
            return;
          else {
            Cnt[Ch[j][1]] = Cnt[Ch[j][0]] + Ch[j][2];
            Unu[Ch[j][1]] = Unu[Ch[j][0]] = false;
          }
    for (int i = 1; i <= N; i++)
      if (Cnt[i] >= M)
        return;
    Ans = max(Ans, sum);
    return;
  }
  dfs(x + 1, sum);
  for (int i = 0; i < 3; i++)
    Ch[PC][i] = X[x][i];
  PC++;
  dfs(x + 1, sum + X[x][3]);
  PC--;
  return;
}
void Top() {
  queue<int> Q;
  for (int i = 1; i <= N; i++)
    if (!Deg[i])
      Q.push(i);
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();
    Topo.push_back(x);
    for (int i = 0; i < (::Q); i++)
      if (X[i][0] == x) {
        Deg[X[i][1]]--;
        if (!Deg[X[i][1]])
          Q.push(X[i][1]);
      }
  }
  return;
}
int main() {
  cin >> N >> M >> Q;
  for (int i = 0; i < Q; i++)
    for (int j = 0; j < 4; j++)
      cin >> X[i][j];
  for (int i = 0; i < Q; i++)
    Deg[X[i][1]]++;
  Top();
  dfs(0, 0);
  cout << Ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

long m, q;
long unsigned int n;
vector<vector<int>> v;

int ans = 0;

void reFun(vector<int> nv) {
  if (nv.size() == n) {
    int sum = 0;
    for (int i = 0; i < q; i++) {
      if (nv[v[i][1] - 1] - nv[v[i][0] - 1] == v[i][2]) {
        sum += v[i][3];
      }
    }
    if (sum > ans) {
      ans = sum;
    }
    return;
  }
  int s = 1;
  if (nv.size()) {
    s = nv[nv.size() - 1];
  }
  for (int i = s; i <= m; i++) {
    vector<int> new_nv = nv;
    new_nv.push_back(i);
    reFun(new_nv);
  }
  return;
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    vector<int> vx;
    for (int i = 0; i < 4; i++) {
      int x;
      cin >> x;
      vx.push_back(x);
    }
    v.push_back(vx);
  }
  vector<int> nv;
  reFun(nv);
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

long m, q;
long unsigned int n;
vector<vector<int>> v;

int ans = 0;

void reFun(int num, vector<int> nv) {
  if (nv.size() == n) {
    int sum = 0;
    for (int i = 0; i < q; i++) {
      if (nv[v[i][1] - 1] - nv[v[i][0] - 1] == v[i][2]) {
        sum += v[i][3];
      }
    }
    if (sum > ans) {
      ans = sum;
    }
    return;
  }
  int s;
  if (nv.size()) {
    s = 1;
  } else {
    s = nv[nv.size() - 1];
  }
  for (int i = s; i <= m; i++) {
    vector<int> new_nv = nv;
    new_nv.push_back(i);
    reFun(num + 1, new_nv);
  }
  return;
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    vector<int> vx;
    for (int i = 0; i < 4; i++) {
      int x;
      cin >> x;
      vx.push_back(x);
    }
    v.push_back(vx);
  }
  vector<int> nv;
  reFun(0, nv);
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int odp = 0, n, m, q;

void rec(vector<int> x, int nr, int co, vector<int> &a, vector<int> &b,
         vector<int> &c, vector<int> &d) {
  x[nr] = co;
  if (nr == n - 1) {
    int wyn = 0;
    for (int i = 0; i < q; ++i)
      if (x[b[i]] - x[a[i]] == c[i])
        wyn += d[i];
    odp = max(odp, wyn);
    return;
  }
  for (int i = co; i <= m; ++i)
    rec(x, nr + 1, i, a, b, c, d);
}

int main() {
  ios_base::sync_with_stdio(0);
  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];
    --a[i];
    --b[i];
  }
  vector<int> x(n);
  for (int i = 1; i <= m; ++i)
    rec(x, 0, i, a, b, c, d);
  cout << odp;
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int odp = 0, n, m, q;

void rec(vector<int> x, int nr, int co, vector<int> &a, vector<int> &b,
         vector<int> &c, vector<int> &d) {
  x[nr] = co;
  if (nr == n) {
    int wyn = 0;
    for (int i = 0; i < q; ++i)
      if (x[b[i]] - x[a[i]] == c[i])
        wyn += d[i];
    odp = max(odp, wyn);
    return;
  }
  for (int i = co; i <= m; ++i)
    rec(x, nr + 1, i, a, b, c, d);
}

int main() {
  ios_base::sync_with_stdio(0);
  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];
    --a[i];
    --b[i];
  }
  vector<int> x(n);
  for (int i = 1; i <= m; ++i)
    rec(x, 0, i, a, b, c, d);
  cout << odp;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

#define INF 1000000000000
int n, m, q;
vector<int> a(50);
vector<int> b(50);
vector<int> c(50);
vector<int> d(50);

int ans = 0;
vector<int> h(10);
void dfs(int pos, int last) {
  if (pos == n) {
    int temp = 0;
    rep(j, q) {
      if (h.at(b.at(j) - 1) - h.at(a.at(j) - 1) == c.at(j)) {
        temp += d.at(j);
      }
    }
    ans = max(ans, temp);
    return;
  }

  h.at(pos) = last;

  for (int k = last; k <= m - 1; k++) {
    dfs(pos + 1, k);
  }
}

signed main() {
  cin >> n >> m >> q;

  rep(i, q) { cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i); }

  dfs(0, 0);

  cout << ans;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)


Fixed submission:

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

#define INF 1000000000000
int n, m, q;
vector<int> a(q);
vector<int> b(q);
vector<int> c(q);
vector<int> d(q);

int ans = 0;
vector<int> h(n);
void dfs(int pos, int last) {
  if (pos == n) {
    int temp = 0;
    rep(j, q) {
      if (h.at(b.at(j) - 1) - h.at(a.at(j) - 1) == c.at(j)) {
        temp += d.at(j);
      }
    }
    ans = max(ans, temp);
    return;
  }

  h.at(pos) = last;

  for (int k = last; k <= m - 1; k++) {
    dfs(pos + 1, k);
  }
}

signed main() {
  cin >> n >> m >> q;

  rep(i, q) { cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i); }

  dfs(0, 0);

  cout << ans;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1000000007;
using Graph = vector<vector<int>>;

int N, M, Q;
vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;

vector<int> num;
int ans = 0;
int now = 0;
void dfs(int S, int cnt) {
  if (cnt == N) {
    now = 0;
    for (int i = 0; i < Q; i++) {
      if (num.at(b[i]) - num.at(a[i]) == c[i])
        now += d[i];
    }
    if (now > ans)
      ans = now;
  } else {
    for (int i = S; i <= M; i++) {
      num.push_back(i);
      dfs(i, cnt + 1);
      num.pop_back();
    }
  }
}

signed main() {

  cin >> N >> M >> Q;
  for (int i = 0; i < Q; i++) {
    int A, B, C, D;
    cin >> A >> B >> C >> D;
    A--;
    B--;
    a.push_back(A);
    b.push_back(B);
    c.push_back(C);
    d.push_back(D);
  }
  dfs(1, 0);
  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1000000007;
using Graph = vector<vector<int>>;

int N, M, Q;
vector<int> a(Q);
vector<int> b(Q);
vector<int> c(Q);
vector<int> d(Q);

vector<int> num;
int ans = 0;
int now = 0;
void dfs(int S, int cnt) {
  if (cnt == N) {
    now = 0;
    for (int i = 0; i < Q; i++) {
      if (num.at(b[i]) - num.at(a[i]) == c[i])
        now += d[i];
    }
    if (now > ans)
      ans = now;
  } else {
    for (int i = S; i <= M; i++) {
      num.push_back(i);
      dfs(i, cnt + 1);
      num.pop_back();
    }
  }
}

signed main() {

  cin >> N >> M >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  dfs(1, 0);
  cout << ans << endl;
}