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

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template <class T1, class T2> ostream &operator<<(ostream &s, pair<T1, T2> P) {
  return s << '<' << P.first << ", " << P.second << '>';
}
template <class T> ostream &operator<<(ostream &s, vector<T> P) {
  for (int i = 0; i < P.size(); ++i) {
    if (i > 0) {
      s << " ";
    }
    s << P[i];
  }
  return s;
}
template <class T> ostream &operator<<(ostream &s, vector<vector<T>> P) {
  for (int i = 0; i < P.size(); ++i) {
    s << endl << P[i];
  }
  return s << endl;
}
template <class T> ostream &operator<<(ostream &s, set<T> P) {
  for (auto it : P) {
    s << "<" << it << "> ";
  }
  return s << endl;
}
template <class T1, class T2> ostream &operator<<(ostream &s, map<T1, T2> P) {
  for (auto it : P) {
    s << "<" << it.first << "->" << it.second << "> ";
  }
  return s << endl;
}

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

long long score(const vector<int> &A) {
  long long res = 0;
  for (int i = 0; i < Q; ++i)
    if (A[b[i]] - A[a[i]] == c[i])
      res += d[i];
  return res;
}

void dfs(vector<int> &A, long long &res) {
  if (A.size() == N) {
    chmax(res, score(A)); // スコアを計算して更新
    return;
  }
  long long ba = (A.empty() ? 0 : A.back());
  for (int add = ba; add < M; ++add) {
    A.push_back(add);
    dfs(A, res);
    A.pop_back();
  }
}

int main() {
  cin >> N >> M >> Q;
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);
  for (int q = 0; q < Q; ++q) {
    cin >> a[q] >> b[q] >> c[q] >> d[q];
    --a[q], --b[q];
  }
  long long res = 0;
  vector<int> A;
  dfs(A, res);
  cout << res << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;

ll ans = 0;
int a[20], b[20], c[20];
ll d[20];

void dfs(vector<int> A, int N, int M, int Q) {
  int P = A.size();
  // cout << P << endl;
  if (P - 1 == N) {
    ll tmp = 0;
    rep(i, Q) {
      if (A[b[i]] - A[a[i]] == c[i])
        tmp += d[i];
    }
    ans = max(ans, tmp);
    // cout << "OK" << endl;
    return;
  }
  for (int i = A[A.size() - 1]; i <= M; i++) {
    A.push_back(i);
    dfs(A, N, M, Q);
    A.pop_back();
  }
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  rep(i, Q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  vector<int> T;
  T.push_back(1);
  dfs(T, N, M, Q);
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
using namespace std;
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using ll = long long;
using P = pair<int, int>;
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int N, M, Q;
vi x;
int score = 0;
int a[100], b[100], c[100], d[100];
int cal(vi &q) {
  int ans = 0;
  rep(i, Q) {
    if (q[b[i]] - q[a[i]] == c[i])
      ans += d[i];
  }
  return ans;
}
void dfs(int v, int p) {
  x[p] = v;
  if (++p >= N) {
    chmax(score, cal(x));
    return;
  }
  rep2(i, v, M + 1) { dfs(i, p); }
}

int main() {

  cin >> N >> M >> Q;
  x.assign(N, 0);

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

  rep(i, N) { dfs(i + 1, 0); }
  cout << score << endl;
  // vi A(N, 1);
  // int ans = 0;
  // int pos = 1;

  // while(1)
  // {
  //     int x = 0;
  //     rep(i, Q)
  //     {
  //         if(A[b[i]]-A[a[i]] == c[i]) x += d[i]
  //     }
  //     chmax(ans, x);
  //     rep2()

  //     bool ok = true;
  //     rep(i, N){
  //         if(A[i] > M) ok = false;
  //     }
  //     if(!ok)
  //     {
  //         break;
  //     }
  // }

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define pb push_back
#define eb emplace_back
using namespace std;
const long long INF = 1LL << 60; // 仮想的な無限大の値;
using ll = long long;
using P = pair<int, int>;
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int N, M, Q;
vi x(N);
int score = 0;
int a[100], b[100], c[100], d[100];
int cal(vi &q) {
  int ans = 0;
  rep(i, Q) {
    if (q[b[i]] - q[a[i]] == c[i])
      ans += d[i];
  }
  return ans;
}
void dfs(int v, int p) {
  x[p] = v;
  if (++p >= N) {
    chmax(score, cal(x));
    return;
  }
  rep2(i, v, M + 1) { dfs(v, p); }
}

int main() {

  cin >> N >> M >> Q;

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

  rep(i, N) { dfs(i, 0); }
  cout << score << endl;
  // vi A(N, 1);
  // int ans = 0;
  // int pos = 1;

  // while(1)
  // {
  //     int x = 0;
  //     rep(i, Q)
  //     {
  //         if(A[b[i]]-A[a[i]] == c[i]) x += d[i]
  //     }
  //     chmax(ans, x);
  //     rep2()

  //     bool ok = true;
  //     rep(i, N){
  //         if(A[i] > M) ok = false;
  //     }
  //     if(!ok)
  //     {
  //         break;
  //     }
  // }

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

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

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

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

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

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; ++i) {
    int t1, t2, t3, t4;
    cin >> t1 >> t2 >> t3 >> t4;
    a.push_back(t1);
    b.push_back(t2);
    c.push_back(t3);
    d.push_back(t4);
  }
  ans = 0;
  dfs(vector<int>(1, 1));
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int N, M, Q;
vector<int> a;
vector<int> b;
vector<int> c;
vector<int> d;
int A[10];
long long maxv;
long long maxp;

int dfs(int aa, int num) {
  A[num - 1] = aa;
  if (num == N) {
    long long ans = 0;
    for (int i = 0; i < Q; ++i) {
      if ((A[b[i] - 1] - A[a[i] - 1]) == c[i]) {
        ans += d[i];
      }
    }
    if (maxv < ans)
      maxv = ans;
    if (ans == maxp)
      return -1;
    return 0;
  }

  for (int i = 1; i <= M; ++i) {
    if (dfs(i, num + 1) == -1) {
      return -1;
    }
  }
}

int main() {
  maxp = 0;
  cin >> N >> M >> Q;
  for (int i = 0; i < Q; ++i) {
    int t1, t2, t3, t4;
    cin >> t1 >> t2 >> t3 >> t4;
    a.push_back(t1);
    b.push_back(t2);
    c.push_back(t3);
    d.push_back(t4);
    maxp += t4;
  }

  for (int i = 0; i < N; ++i) {
    A[i] = 0;
  }
  maxv = -1;

  for (int i = 1; i <= M; ++i) {
    int num = 1;
    if (dfs(i, num) == -1) {
      break;
    }
  }
  cout << maxv << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &x) {
  x = 0;
  int f(1);
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
int n, m, q, va[11], ans;
struct edge {
  int a, b, c, d;
} A[55];
void solve(int, int);
int main() {
  read(n);
  read(m);
  read(q);
  for (int i(1); i <= q; ++i)
    scanf("%d%d%d%d", &A[i].a, &A[i].b, &A[i].c, &A[i].d);
  solve(1, 1);
  printf("%d\\n", ans);
  return 0;
}
void solve(int x, int lst) {
  if (x == n + 1) {
    int sum = 0;
    for (int i(1); i <= q; ++i)
      if (va[A[i].b] - va[A[i].a] == A[i].c)
        sum += A[i].d;
    ans = max(ans, sum);
    return;
  }
  for (int i = lst; i <= m; ++i)
    va[x] = i, solve(x + 1, i), va[x] = 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &x) {
  x = 0;
  int f(1);
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
int n, m, q, va[11], ans;
struct edge {
  int a, b, c, d;
} A[55];
void solve(int, int);
int main() {
  read(n);
  read(m);
  read(q);
  for (int i(1); i <= q; ++i)
    scanf("%d%d%d%d", &A[i].a, &A[i].b, &A[i].c, &A[i].d);
  solve(1, 0);
  printf("%d\\n", ans);
  return 0;
}
void solve(int x, int lst) {
  if (x == n + 1) {
    int sum = 0;
    for (int i(1); i <= q; ++i)
      if (va[A[i].b] - va[A[i].a] == A[i].c)
        sum += A[i].d;
    ans = max(ans, sum);
  }
  for (int i = lst; i <= m; ++i)
    solve(x + 1, i);
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, m, q;
  cin >> n >> m >> q;
  VL a(q), b(q), c(q), d(q);
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }

  VI ks(n);
  auto rec = [&](auto rec, int z, int pi) {
    ll ans = 0;
    if (z == n) {
      ll sm = 0;
      rep(i, q) {
        if (ks[b[i]] - ks[a[i]] == c[i])
          sm += d[i];
      }
      return sm;
    }

    rep2(i, pi, m) {
      ks[z] = i;
      ans = max(ans, rec(rec, z + 1, i));
    }
    return ans;
  };
  cout << rec(rec, 0, 0) << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep2(x, fr, to) for (int x = (fr); x < (to); x++)
#define rep(x, to) for (int x = 0; x < (to); x++)
#define repr(x, fr, to) for (int x = (fr); x >= (to); x--)
#define all(c) c.begin(), c.end()
#define sz(v) (int)v.size()

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<ll> VL;
const int MD = 1e9 + 7;
void dbg() { cerr << "\\n"; }
template <class F, class... S> void dbg(const F &f, const S &...s) {
  cerr << f << ": ";
  dbg(s...);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, m, q;
  cin >> n >> m >> q;
  VL a(n), b(n), c(n), d(n);
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }

  VI ks(n);
  auto rec = [&](auto rec, int z, int pi) {
    ll ans = 0;
    if (z == n) {
      ll sm = 0;
      rep(i, q) {
        if (ks[b[i]] - ks[a[i]] == c[i])
          sm += d[i];
      }
      return sm;
    }

    rep2(i, pi, m) {
      ks[z] = i;
      ans = max(ans, rec(rec, z + 1, i));
    }
    return ans;
  };
  cout << rec(rec, 0, 0) << "\\n";
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define INF 1e9
#define MAXN 100005
#define MAXM 100005
#define MOD 1000000007
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define pii pair<int, int>

int ans = 0, N, M, Q;
vi A, B, C, D, num;

int checkReq(vi tmp) {
  int res = 0;

  rep(i, Q) {
    if (tmp[B[i]] - tmp[A[i]] == C[i])
      res += D[i];
  }

  return res;
}

void dfs(vi tmp) {
  int n = tmp.size();

  if (n == N) {
    // rep(i, N)
    //     cout << tmp[i+1] << " ";
    // cout << endl;
    ans = max(ans, checkReq(tmp));
  } else {
    tmp.push_back(tmp[n - 1]);
    while (tmp[n] <= M) {
      dfs(tmp);
      tmp[n]++;
    }
    tmp.pop_back();
  }
}

void solve() {

  rep(i, M) {
    num.push_back(i + 1);
    dfs(num);
    num.pop_back();
  }

  cout << ans << endl;
}

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];
    --A[i], --B[i];
  }

  solve();
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define INF 1e9
#define MAXN 100005
#define MAXM 100005
#define MOD 1000000007
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define pii pair<int, int>

int ans = 0, N, M, Q;
vi A, B, C, D, num;

int checkReq(vi tmp) {
  int res = 0;

  rep(i, Q) {
    if (tmp[B[i]] - tmp[A[i]] == C[i])
      res += D[i];
  }

  return res;
}

bool dfs(vi tmp) {
  int n = tmp.size();

  if (n == N) {
    // rep(i, N)
    //     cout << tmp[i+1] << " ";
    // cout << endl;
    ans = max(ans, checkReq(tmp));
  } else {
    tmp.push_back(tmp[n - 1]);
    while (tmp[n] <= M) {
      dfs(tmp);
      tmp[n]++;
    }
    tmp.pop_back();
  }
}

void solve() {

  rep(i, M) {
    num.push_back(i + 1);
    dfs(num);
    num.pop_back();
  }

  cout << ans << endl;
}

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];
    --A[i], --B[i];
  }

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

#include <iostream>
using namespace std;

int a[50], b[50], c[50], d[50], e[50];
int n, m, q;
int ret = 0;

void dfs(int pos, int val) {
  if (pos == n) {
    int tmp = 0;
    for (int i = 0; i < q; i++) {
      tmp += d[i] * (e[b[i]] - e[a[i]] == c[i]);
    }
    ret = max(ret, tmp);
    return;
  }
  for (int i = val; i <= m; i++) {
    e[pos] = i;
    dfs(pos + 1, i);
  }
}

int 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(0, 1);
  cout << ret << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int a[10], b[10], c[10], d[10], e[10];
int n, m, q;
int ret = 0;

void dfs(int pos, int val) {
  if (pos == n) {
    int tmp = 0;
    for (int i = 0; i < q; i++) {
      tmp += d[i] * (e[b[i]] - e[a[i]] == c[i]);
    }
    ret = max(ret, tmp);
    return;
  }
  for (int i = val; i <= m; i++) {
    e[pos] = i;
    dfs(pos + 1, i);
  }
}

int 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(0, 1);
  cout << ret << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
typedef long long ll;
namespace mp = boost::multiprecision;
using Mint = mp::cpp_int;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
#define sort_g(v) sort(all(v), greater<int>())
#define dump(x) cerr << #x << ' ' << '=' << ' ' << x << endl

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  int a[55], b[55], c[55], d[55];
  rep(i, Q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  vector<int> vec(N + M, 1);
  rep(i, M - 1) { vec.at(i) = 0; }

  int ans = 0;
  do {
    int cnt = 0;
    int tmp = 0;
    vector<int> A;
    rep(i, N + M) {
      if (vec.at(i) == 0)
        cnt++;
      else
        A.push_back(cnt + 1);
    }
    rep(i, Q) {
      if (A.at(b[i]) - A.at(a[i]) == c[i])
        tmp += d[i];
    }
    ans = max(ans, tmp);
  } while (next_permutation(all(vec)));

  cout << ans << endl;
}

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


Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
typedef long long ll;
namespace mp = boost::multiprecision;
using Mint = mp::cpp_int;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
#define sort_g(v) sort(all(v), greater<int>())
#define dump(x) cerr << #x << ' ' << '=' << ' ' << x << endl

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  int a[55], b[55], c[55], d[55];
  rep(i, Q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  vector<int> vec(N + M - 1, 1);
  rep(i, M - 1) { vec.at(i) = 0; }

  int ans = 0;
  do {
    int cnt = 0;
    int tmp = 0;
    vector<int> A;
    rep(i, N + M) {
      if (vec.at(i) == 0)
        cnt++;
      else
        A.push_back(cnt);
    }
    rep(i, Q) {
      if (A.at(b[i]) - A.at(a[i]) == c[i])
        tmp += d[i];
    }
    ans = max(ans, tmp);
  } while (next_permutation(all(vec)));

  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

int an = 0;

void dfs(int cnt, int n, int m, int q, vector<int> a, vector<int> b,
         vector<int> c, vector<int> d, vector<int> ans) {
  if (cnt == n) {
    int kari = 0;
    // rep(i,n){
    //     cout<<ans[i]<<" ";
    // }
    // cout<<endl;
    rep(i, q) {
      if (ans[b[i] - 1] - ans[a[i] - 1] == c[i]) {
        kari += d[i];
      }
    }
    an = max(an, kari);
  } else {
    if (cnt == 0) {
      rep(i, m) {
        vector<int> coans(n);
        rep(i, n) coans[i] = ans[i];
        coans[0] = i + 1;
        dfs(cnt + 1, n, m, q, a, b, c, d, coans);
      }
    } else {
      int x = ans[cnt - 1];
      for (int i = x; i <= m; ++i) {
        vector<int> coans(n);
        rep(i, n) coans[i] = ans[i];
        coans[cnt] = i;
        dfs(cnt + 1, n, m, q, a, b, c, d, coans);
      }
    }
  }
}

int main() {
  int n, m, q;
  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];
  vector<int> ans(n, 0);
  dfs(0, n, m, q, a, b, c, d, ans);
  cout << an << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

int an = 0;

bool dfs(int cnt, int n, int m, int q, vector<int> a, vector<int> b,
         vector<int> c, vector<int> d, vector<int> ans) {
  if (cnt == n) {
    int kari = 0;
    // rep(i,n){
    //     cout<<ans[i]<<" ";
    // }
    // cout<<endl;
    rep(i, q) {
      if (ans[b[i] - 1] - ans[a[i] - 1] == c[i]) {
        kari += d[i];
      }
    }
    an = max(an, kari);
    return true;
  } else {
    if (cnt == 0) {
      rep(i, m) {
        vector<int> coans(n);
        rep(i, n) coans[i] = ans[i];
        coans[0] = i + 1;
        dfs(cnt + 1, n, m, q, a, b, c, d, coans);
      }
    } else {
      int x = ans[cnt - 1];
      for (int i = x; i <= m; ++i) {
        vector<int> coans(n);
        rep(i, n) coans[i] = ans[i];
        coans[cnt] = i;
        dfs(cnt + 1, n, m, q, a, b, c, d, coans);
      }
    }
  }
}

int main() {
  int n, m, q;
  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];
  vector<int> ans(n, 0);
  dfs(0, n, m, q, a, b, c, d, ans);
  cout << an << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

long long int x[15], n, m, i, ans, a[55], b[55], c[55], d[55], q;

void bt(long long int p) {
  long long int t, s;
  if (p <= n) {
    for (t = x[p - 1]; t < m + 1; t++) {
      x[p] = t;
      bt(p + 1);
    }
    return;
  }
  s = 0;
  for (t = 1; t < q + 1; t++)
    if (x[b[t]] - x[a[t]] == c[t])
      s = s + d[t];
  ans = max(ans, s);
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> q;
  for (i = 1; i < q + 1; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  ans = 0;
  x[0] = 1;
  bt(1);
  cout << ans;
}


Error: WA

Fixed submission:

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

long long int x[15], n, m, i, ans, a[15], b[15], c[15], d[15], q;

void bt(long long int p) {
  long long int t, s;
  if (p <= n) {
    for (t = x[p - 1]; t < m + 1; t++) {
      x[p] = t;
      bt(p + 1);
    }
    return;
  }
  s = 0;
  for (t = 1; t < q + 1; t++)
    if (x[b[t]] - x[a[t]] == c[t])
      s = s + d[t];
  ans = max(ans, s);
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m >> q;
  for (i = 1; i < q + 1; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  ans = 0;
  x[0] = 1;
  bt(1);
  cout << ans;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

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


Error: TLE

Fixed submission:

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

const int INF = 1 << 30;

vector<tuple<int, int, int, int>> vec;

int main() {
  int n, m, q;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < q; i++) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    vec.push_back(make_tuple(a, b, c, d));
  }
  int ans = -INF;
  for (int i = 0; i < (1 << 10); i++) {
    if (__builtin_popcount(i) == n) {
      int temp = 0;
      vector<int> p(n);
      while (i) {
        p.push_back(i & (-i));
        i &= i - 1;
      }
      for (int j = 0; j < q; j++) {
        int a, b, c, d;
        tie(a, b, c, d) = vec[j];
        if (p[b - 1] - p[a - 1] == c)
          temp += d;
      }
      ans = max(ans, temp);
    }
  }
  printf("%d\\n", ans);
}