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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
#define all(v) v.begin(), v.end()
#define repi(i, n, init) for (ll i = init; i < (n); i++)
#define repd(i, n, init) for (ll i = (n); i >= init; i--)
#define repm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define repb(bit, n, init) for (ll bit = init; bit < (1 << n); ++bit)

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  int a[55], b[55], c[55], d[55];
  repi(i, q, 0) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  vector<int> vec(n + m, 1);
  repi(i, m, 0) {
    vec[i] = 0;
    ;
  }
  int ans = 0;
  do {
    int zero_cnt = 0;
    vector<int> A;
    repi(i, n + m, 0) {
      if (vec[i] == 0)
        zero_cnt++;
      else {
        A.push_back(zero_cnt);
        if (zero_cnt == 0) {
          A.clear();
          break;
        }
      }
    }
    if (A.size() != n)
      continue;
    int s = 0;
    repi(i, q, 0) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        s += d[i];
    }
    ans = max(ans, s);
  } while (next_permutation(vec.begin(), vec.end()));
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
#define all(v) v.begin(), v.end()
#define repi(i, n, init) for (ll i = init; i < (n); i++)
#define repd(i, n, init) for (ll i = (n); i >= init; i--)
#define repm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define repb(bit, n, init) for (ll bit = init; bit < (1 << n); ++bit)

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  int a[n], b[n], c[n], d[n];
  repi(i, q, 0) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  vector<int> vec(n + m, 1);
  repi(i, m, 0) {
    vec[i] = 0;
    ;
  }
  int ans = 0;
  do {
    int zero_cnt = 0;
    vector<int> A;
    repi(i, n + m, 0) {
      if (vec[i] == 0)
        zero_cnt++;
      else {
        A.push_back(zero_cnt);
        if (zero_cnt == 0) {
          A.clear();
          break;
        }
      }
    }
    if (A.size() != n)
      continue;
    int s = 0;
    repi(i, q, 0) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        s += d[i];
    }
    ans = max(ans, s);
  } while (next_permutation(vec.begin(), vec.end()));
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, s, g) for (i = s; i < g; i++)
using namespace std;
using ll = long long;
const int inf = 1000000000; // 10^9

ll N, M, Q;
ll ans = 0;
vector<int> a, b, c, d;

void search(int x, vector<int> A) {
  if (x == N) {
    ll cost = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        cost += d[i];
      }
    }
    ans = max(ans, cost); // max,minは型を揃える
    return;               // 関数が終了する
  }
  for (int i = A[x - 1]; i <= M; i++) {
    A.push_back(i);
    search(x + 1, A);
    A.pop_back(); // 一番最後の数字を削除する,次のループに備える
  }
}

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]--;
  }
  for (int i = 1; i <= M; i++) {
    vector<int> A;
    A.push_back(i);
    search(1, A);
  }

  cout << ans << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define rep(i, s, g) for (i = s; i < g; i++)
using namespace std;
using ll = long long;
const int inf = 1000000000; // 10^9

ll N, M, Q;
ll ans = 0;
vector<int> a, b, c, d;

void search(int x, vector<int> A) {
  if (x == N) {
    ll cost = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        cost += d[i];
      }
    }
    ans = max(ans, cost); // max,minは型を揃える
    return;               // 関数が終了する
  }
  for (int i = A[x - 1]; i <= M; i++) {
    A.push_back(i);
    search(x + 1, A);
    A.pop_back(); // 一番最後の数字を削除する,次のループに備える
  }
}

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]--;
  }
  for (int i = 1; i <= M; i++) {
    vector<int> A;
    A.push_back(i);
    search(1, A);
  }

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

#include <bits/stdc++.h>
using namespace std;
using lli = long long;
using pii = pair<int, int>;

#define rep(i, k, n) for (int i = (k); i != int(n); ++i)
#define sz(x) x.size()
#define all(x) x.begin(), x.end()

#define dbg(x) cout << #x << '=' << x << '\\n'
#define dbg2(x, y) cout << #x << '=' << x << ' ' << #y << '=' << y << '\\n'

#define input() (*istream_iterator<int>(cin))
#define strin() (*istream_iterator<string>(cin))
#define output(x) cout << x << '\\n'

const int mxq = 60;
int n, m, q;
int mx = 0, cnt = 0;
vector<int> A;
struct quad {
  int a, b, c, d;
};
quad arr[mxq];
void solve(int cur = 1, int sz = 0) {
  if (sz == n) {
    int sum = 0;
    for (int i = 1; i <= q; ++i) {
      cnt++;
      if (A[arr[i].b - 1] - A[arr[i].a - 1] == arr[i].c)
        sum += arr[i].d;
    }
    mx = max(sum, mx);
    return;
  }
  for (int i = cur; i <= m; ++i) {
    A.push_back(i);
    solve(i, sz + 1);
    A.pop_back();
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> q;
  for (int i = 1; i <= q; ++i)
    arr[i] = {input(), input(), input(), input()};
  solve();
  cout << mx;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lli = long long;
using pii = pair<int, int>;

#define rep(i, k, n) for (int i = (k); i != int(n); ++i)
#define sz(x) x.size()
#define all(x) x.begin(), x.end()

#define dbg(x) cout << #x << '=' << x << '\\n'
#define dbg2(x, y) cout << #x << '=' << x << ' ' << #y << '=' << y << '\\n'

#define input() (*istream_iterator<int>(cin))
#define strin() (*istream_iterator<string>(cin))
#define output(x) cout << x << '\\n'

const int mxq = 60;
int n, m, q;
int mx;
vector<int> A;
struct quad {
  int a, b, c, d;
};
quad arr[mxq];
int solve(int cur = 1, int sz = 0) {
  if (sz == n) {
    int sum = 0;
    for (int i = 1; i <= q; ++i) {
      int a = arr[i].a, b = arr[i].b, c = arr[i].c, d = arr[i].d;
      if (A[b - 1] - A[a - 1] == c)
        sum += d;
    }
    mx = max(sum, mx);
    return 0;
  }
  for (int i = cur; i <= m; ++i) {
    if (sz + 1 <= n) {
      A.push_back(i);
      solve(i, sz + 1);
      A.pop_back();
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m >> q;
  for (int i = 1; i <= q; ++i)
    arr[i] = {input(), input(), input(), input()};
  solve();
  cout << mx;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

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;
}

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

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    // calc score
    ll 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(void) {
  cin >> n >> m >> q;
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  dfs(vector<int>(1, 1));

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

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;
}

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

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    // calc score
    ll 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(void) {
  cin >> n >> m >> q;
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  dfs(vector<int>(1, 1));

  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++)
using ll = long long int;
using namespace std;
ll mod = 1e9 + 7;

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

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;
  a = b = c = d = vector<int>(q);
  rep(i, q) { cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i); }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

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

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

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(now, ans);
    return;
  }
  A.push_back(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}
int 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(vector<int>(1, 1));
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
const int N = 2e5 + 5;
const ll mod = 1e9 + 7;

struct node {
  int a, b, c, d;
};

int n, m, q;
vector<node> v;
int res = 0;

void solve(int idx, int num, vector<int> a) {
  if (num >= m + 1) {
    return;
  }
  if (idx == n) {
    if (a.size() == n && num <= m + 1) {
      int cur = 0;
      for (int i = 0; i < q; i++) {
        if (a[v[i].b - 1] - a[v[i].a - 1] == v[i].c)
          cur += v[i].d;
      }
      res = max(res, cur);
    }
    return;
  }
  vector<int> next(a);
  if (a.size() < n) {
    next.push_back(num);
    solve(idx + 1, num, next);
    if (num <= m + 1)
      solve(idx + 1, num + 1, next);
  }
  solve(idx, num + 1, a);
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  cin >> n >> m >> q;
  v.resize(q);
  for (int i = 0; i < q; i++) {
    cin >> v[i].a >> v[i].b >> v[i].c >> v[i].d;
  }
  solve(0, 1, vector<int>());
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
const int N = 2e5 + 5;
const ll mod = 1e9 + 7;

int n, m, q;
vector<vector<int>> v;
int res = 0;

void solve(int idx, int num, vector<int> a) {
  if (num >= m + 1) {
    return;
  }
  if (idx == n) {
    if (a.size() == n && num <= m + 1) {
      int cur = 0;
      for (int i = 0; i < q; i++) {
        vector<int> row = v[i];
        if (a[row[1] - 1] - a[row[0] - 1] == row[2])
          cur += row[3];
      }
      res = max(res, cur);
    }
    return;
  }
  vector<int> next(a);
  if (a.size() < n) {
    next.push_back(num);
    solve(idx + 1, num, next);
    if (num <= m + 1)
      solve(idx + 1, num + 1, next);
  }
  solve(idx, num + 1, a);
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  cin >> n >> m >> q;
  v.resize(q);
  for (int i = 0; i < q; i++) {
    vector<int> temp(4);
    for (int j = 0; j < 4; j++) {
      int val;
      cin >> val;
      temp[j] = val;
    }
    v[i] = temp;
  }
  solve(0, 1, vector<int>());
  cout << res << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
#define ITR(v, i) for (auto ite = v.begin(); ite != v.end(); ite++)
const ll INF = 999999999999999;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
vector<vl> V[13];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m >> q;
  vl T[100];
  rep(i, q) {
    cin >> a >> b >> c >> d;
    T[i].push_back(a);
    T[i].push_back(b);
    T[i].push_back(c);
    T[i].push_back(d);
  }

  V[0].push_back({1});
  rep(i, n) {
    for (vl VV : V[i]) {
      for (ll j = VV.back(); j <= m; j++) {
        V[i + 1].push_back(VV);
        V[i + 1].back().push_back(j);
      }
    }
  }

  for (vl VV : V[n]) {
    t = 0;
    rep(i, q) {
      if (VV[T[i][1]] - VV[T[i][0]] == T[i][2])
        t += T[i][3];
    }
    ans = max(ans, t);
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()
#define ITR(v, i) for (auto ite = v.begin(); ite != v.end(); ite++)
const ll INF = 999999999999999;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans;
vector<vl> V[11];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m >> q;
  vl T[10];
  rep(i, q) {
    cin >> a >> b >> c >> d;
    T[i].push_back(a);
    T[i].push_back(b);
    T[i].push_back(c);
    T[i].push_back(d);
  }

  V[0].push_back({1});
  rep(i, n) {
    for (vl VV : V[i]) {
      for (ll j = VV.back(); j <= m; j++) {
        V[i + 1].push_back(VV);
        V[i + 1].back().push_back(j);
      }
    }
  }

  for (vl VV : V[n]) {
    t = 0;
    rep(i, q) {
      if (VV[T[i][1]] - VV[T[i][0]] == T[i][2])
        t += T[i][3];
    }
    ans = max(ans, t);
  }

  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 rrep(i, n) for (int i = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1001001001;
const int mod = 1000000007;
typedef tuple<int, int, int> T;

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

void dfs(int s) {
  if (res.size() == n) {
    int sum = 0;
    rep(i, q) {
      if (res[b[i]] - res[a[i]] == c[i]) {
        sum += d[i];
      }
    }
    ans = max(ans, sum);
    return;
  }
  for (int i = s; i <= m; ++i) {
    res.push_back(i);
    dfs(i);
    res.pop_back();
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> q;
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  dfs(1);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1001001001;
const int mod = 1000000007;
typedef tuple<int, int, int> T;

int dp[15][15];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m, q;
  cin >> n >> m >> q;
  priority_queue<pair<int, tuple<int, int, int>>> p;
  rep(i, q) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    p.emplace(d, make_tuple(a, b, c));
  }
  int ans = 0;
  map<int, int> mp;
  int cnt = 0;
  while (1) {
    int d = p.top().first;
    T t = p.top().second;
    p.pop();
    if (mp[get<0>(t)] >= 1 && mp[get<1>(t)] >= 1)
      continue;
    ans += d;
    mp[get<0>(t)]++;
    mp[get<1>(t)]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

#define name ""
#define ini                                                                    \\
  freopen(name ".inp", "r", stdin);                                            \\
  freopen(name ".out", "w", stdout)
#define foe(it, c) for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define long long long
#define db double
#define pii pair<int, int>
#define pll pair<long, long>
#define all(c) c.begin(), c.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};

const int N = 51;
int x[N], a[N], b[N], c[N], d[N];
long res = 0;
int n, m, Q;

void Try(int u) {
  for (int i = x[u - 1]; i <= m; i++) {
    x[u] = i;
    if (u == n) {
      long cur = 0;
      for (int j = 1; j <= Q; j++) {
        if (x[b[j]] - x[a[j]] == c[j])
          cur += d[j];
      }
      res = max(res, cur);
    } else
      Try(u + 1);
  }
}

int main() {
  fastio;
  cin >> n >> m >> Q;
  for (int i = 1; i <= Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  x[0] = 1;
  Try(1);
  cout << res;
  return 0;
}

Error: WA

Fixed submission:

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

#define name ""
#define ini                                                                    \\
  freopen(name ".inp", "r", stdin);                                            \\
  freopen(name ".out", "w", stdout)
#define foe(it, c) for (__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define long long long
#define db double
#define pii pair<int, int>
#define pll pair<long, long>
#define all(c) c.begin(), c.end()
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};

const int N = 11;
int x[N], a[N], b[N], c[N], d[N];
long res = 0;
int n, m, Q;

void Try(int u) {
  for (int i = x[u - 1]; i <= m; i++) {
    x[u] = i;
    if (u == n) {
      long cur = 0;
      for (int j = 1; j <= Q; j++) {
        if (x[b[j]] - x[a[j]] == c[j])
          cur += d[j];
      }
      res = max(res, cur);
    } else
      Try(u + 1);
  }
}

int main() {
  fastio;
  cin >> n >> m >> Q;
  for (int i = 1; i <= Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  x[0] = 1;
  Try(1);
  cout << res;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int s[11];
int main() {
  int n, m, q, tot, maxn = 0;
  cin >> n >> m >> q;
  int a[q + 1], b[q + 1], c[q + 1], d[q + 1];
  for (int i = 1; i <= q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  while (s[0] != 1) {
    tot = 0;
    s[n]++;
    int x = n;
    while (s[x] >= m && x > 0) {
      s[x - 1]++;
      x--;
      for (int i = x + 1; i <= n; i++)
        s[i] = s[x];
    }
    for (int i = 1; i <= q; i++)
      if (s[b[i]] - s[a[i]] == c[i])
        tot += d[i];
    maxn = max(maxn, tot);
  }
  cout << maxn << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int s[11];
int main() {
  int n, m, q, tot, maxn = 0;
  cin >> n >> m >> q;
  int a[q + 1], b[q + 1], c[q + 1], d[q + 1];
  for (int i = 1; i <= q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  while (s[0] != 1) {
    tot = 0;
    s[n]++;
    int x = n;
    while (s[x] >= m) {
      s[x - 1]++;
      s[x] = s[x - 1];
      x--;
      for (int i = x + 1; i <= n; i++)
        s[i] = s[x];
    }
    for (int i = 1; i <= q; i++)
      if (s[b[i]] - s[a[i]] == c[i])
        tot += d[i];
    maxn = max(maxn, tot);
  }
  cout << maxn << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct point {
  int a, b, c, d;
};
int n, m, q;
point query[55];
int arr[15];
int mx, call;
void f(int curr, int before) {
  call++;
  for (int i = before; i <= m; i++) {
    arr[curr] = i;
    if (curr == n) {
      int sum = 0;
      for (int j = 0; j < q; j++) {
        point &k = query[j];
        if (arr[k.b] - arr[k.a] == k.c)
          sum += k.d;
      }
      mx = max(mx, sum);
    } else
      f(curr + 1, i);
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> query[i].a >> query[i].b >> query[i].c >> query[i].d;
  }
  f(1, 1);
  cout << mx;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct point {
  int a, b, c, d;
};
int n, m, q;
point query[55];
int arr[15];
int mx, call;
int f(int curr, int before) {
  call++;
  for (int i = before; i <= m; i++) {
    arr[curr] = i;
    if (curr == n) {
      int sum = 0;
      for (int j = 0; j < q; j++) {
        point &k = query[j];
        if (arr[k.b] - arr[k.a] == k.c)
          sum += k.d;
      }
      mx = max(mx, sum);
    } else
      f(curr + 1, i);
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> query[i].a >> query[i].b >> query[i].c >> query[i].d;
  }
  f(1, 1);
  cout << mx;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
#define MOD 1000000007

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template <class T> void print(T &x) { printf("%d\\n", x); }

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

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;
  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;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
#define MOD 1000000007

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template <class T> void print(T &x) { printf("%d\\n", x); }

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

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

  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;

  return 0;
}