alexjercan/bugnet huggingface dataset:
Problem p02695 (C++) — buggy submission, verdict: 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[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;
}


Error: 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[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;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
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(res, ans);
    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;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
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 <= n; 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]])
        res += d[i];
    }
    ans = max(res, ans);
    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, verdict: 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;
  }
  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;
}


Error: -11

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;
  }
  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, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

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

void dfs(string s, int moji) {

  if (moji == n) {
    ll anstmp = 0;

    rep(i, q) {
      if ((s[b[i]] - '0') - (s[a[i]] - '0') == c[i])
        anstmp += d[i];
    }

    ans = max(ans, anstmp);
    // cout << s << endl;

    return;
  }

  int simo = s[moji - 1] - '0';
  for (int i = simo; i <= m - 1; i++) {
    string tmp = to_string(i);
    string ns = s + tmp;

    dfs(ns, moji + 1);
  }

  return;
}

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

  rep(i, m) {
    string s = to_string(i);
    dfs(s, 1);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

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

void dfs(string s, int moji) {

  if (moji == n) {
    ll anstmp = 0;

    rep(i, q) {
      if ((s[b[i]] - '0') - (s[a[i]] - '0') == c[i])
        anstmp += d[i];
    }

    ans = max(ans, anstmp);
    // cout << s << endl;

    return;
  }

  int simo = s[moji - 1] - '0';

  for (int i = simo; i <= m; i++) {
    string tmp = to_string(i);
    string ns = s + tmp;

    dfs(ns, moji + 1);
  }

  return;
}

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

  rep(i, m) {
    string s = to_string(i + 1);
    dfs(s, 1);
  }

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

#include <bits/stdc++.h>
#ifdef _DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ld PI = 3.1415926535897932;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

void dfs(int i, int x) {
  if (i == n) {
    int res = 0;
    rep(j, q) {
      if (c[j] == v[b[j]] - v[a[j]])
        res += d[j];
    }
    chmax(ans, res);
    return;
  }
  REP(j, x, m + 1) {
    v.push_back(j);
    dfs(i + 1, j);
    v.pop_back();
  }
}

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


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; i++)
#define rep3(i, n, m) for (ll i = n; i >= m; i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }

int n, m, q;
ll ans = -INF;
vector<int> A;
vector<int> a(q + 1);
vector<int> b(q + 1);
vector<int> c(q + 1);
vector<int> d(q + 1);
void dfs(int i, int x) {
  if (i == n) {
    ll now = 0;
    rep(j, q - 1) {
      if (A[b[j]] - A[a[j]] == c[j])
        now += d[j];
    }
    chmax(ans, now);
    return;
  }
  rep2(j, x, m) {
    A.pb(j);
    dfs(i + 1, j);
    A.ppb();
  }
}

int main() {
  cin >> n >> m >> q;
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }
  dfs(0, 1);
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#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;
int ans;

void dfs(vector<int> s) {
  if (s.size() == n + 1) {
    int now = 0;
    rep(i, q) {
      if (s[b[i]] - s[a[i]] == c[i])
        now += d[i];
    }
    ans = max(ans, now);
    return;
  }
  s.push_back(s.back());
  while (s.back() <= m) {
    dfs(s);
    s.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: -11

Fixed submission:

#include <bits/stdc++.h>
#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;
int ans = 0;

void dfs(vector<int> s) {
  if (s.size() == n + 1) {
    int now = 0;
    rep(i, q) {
      if (s[b[i]] - s[a[i]] == c[i])
        now += d[i];
    }
    ans = max(ans, now);
    return;
  }
  s.push_back(s.back());
  while (s.back() <= m) {
    dfs(s);
    s.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;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

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

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


Fixed submission:

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

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

void dfs(vector<int> A) {
  if (A.size() == N) {
    int now = 0;
    rep(i, Q) {
      if (A.at(b.at(i)) - A.at(a.at(i)) == c.at(i)) {
        now += d.at(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);
  for (int i = 0; i < Q; i++) {
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
  }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: 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;
}

Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


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, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define fr first
#define se second

using namespace std;

const long long N = 2e5 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;

int n;
int m;
int q;
int res;
int a[N];
int b[N];
int c[N];
int d[N];
int idx[N];
map<vector<int>, int> used;
int check(vector<int> p) {
  int ans = 0;
  for (int i = 1; i <= q; i++) {
    if (p[b[i] - 1] - p[a[i] - 1] == c[i]) {
      ans += d[i];
    }
  }
  return ans;
}
void rec(vector<int> a) {
  if (used.count(a)) {
    return;
  }
  used[a] = 1;
  res = max(res, check(a));
  for (int i = n; i > 0; i--) {
    if (a[i] > a[i - 1]) {
      vector<int> t = a;
      t[i - 1]++;
      rec(t);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);

  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  vector<int> p(n, 1);
  p.push_back(m);
  rec(p);
  cout << res << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define fr first
#define se second

using namespace std;

const long long N = 2e5 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;

int n;
int m;
int q;
int a[N];
int b[N];
int c[N];
int d[N];
int idx[N];
vector<int> getNext(vector<int> a) {

  if (a[n - 1] != m) {
    a[n - 1]++;
  } else {
    int idx = -1;
    for (int i = n - 1; i >= 0; i--) {
      if (a[i] != m) {
        idx = i;
        break;
      }
    }
    if (idx == -1) {
      return {};
    }
    a[idx]++;
    for (int i = n - 1; i > idx; i--) {
      a[i] = 1;
    }
  }
  return a;
}
int main() {
  ios_base::sync_with_stdio(false);

  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  int res = 0;
  vector<int> p(n, 1);
  while (!p.empty()) {
    if (is_sorted(p.begin(), p.end())) {
      int ans = 0;
      for (int i = 1; i <= q; i++) {
        if (p[b[i] - 1] - p[a[i] - 1] == c[i]) {
          ans += d[i];
        }
      }
      res = max(res, ans);
    }
    p = getNext(p);
  }
  cout << res << "\\n";
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

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[i] >> b[i] >> c[i] >> d[i]; }

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

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


Error: WA

Fixed submission:

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

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  int data[Q][4];
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> data[i][j];
    }
  }
  queue<ll> que_one;
  queue<ll> que_twe;
  for (int i = 1; i <= M; i++) {
    que_one.push(i);
    que_twe.push(i);
  }
  vector<ll> num(N);
  ll MAX = 0;
  while (true) {
    // 取り出す
    for (int i = 0; i < N; i++) {
      num[i] = que_one.front();
      que_one.pop();
      cout << num[i] << " ";
    }
    cout << endl;
    vector<ll> ans(N);
    vector<ll> pre_sort = num;
    sort(num.begin(), num.end());
    do {
      ll sum = 0;
      // 順列
      for (int i = 0; i < N; i++) {
        ans[i] = num[i];
      }
      // 条件満たしているか
      for (int i = 0; i < Q; i++) {
        if (ans[data[i][1] - 1] - ans[data[i][0] - 1] == data[i][2]) {
          sum += data[i][3];
        }
      }
      MAX = max(sum, MAX);
    } while (next_permutation(num.begin(), num.end()));

    // 戻す
    for (int i = 0; i < N; i++) {
      que_one.push(pre_sort[i]);
    }
    if (que_one == que_twe) {
      break;
    }
  }
  cout << MAX << endl;
}
Problem p02695 (C++) — buggy submission, verdict: 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;
}

Error: TLE

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, verdict: Runtime Error

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

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

void dfs(int cnt, vector<int> A, int now) {
  if (cnt == n) {
    int num = 0;
    for (int i = 0; i < q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        num += d[i];
      }
    }
    ans = max(num, ans);
  } else {
    for (int i = now; i <= m; i++) {
      A[cnt] = i;
      dfs(cnt + 1, A, i);
    }
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    int aa, bb, cc, dd;
    cin >> aa >> bb >> cc >> dd;
    a.push_back(aa);
    b.push_back(bb);
    c.push_back(cc);
    d.push_back(dd);
  }

  vector<int> A(n, 0);
  A[0] = 1;
  dfs(0, A, 1);
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

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

void dfs(int cnt, vector<int> A, int now) {
  if (cnt == n) {
    int num = 0;
    for (int i = 0; i < q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        num += d[i];
      }
    }
    ans = max(num, ans);
  } else {
    for (int i = now; i <= m; i++) {
      A[cnt] = i;
      dfs(cnt + 1, A, i);
    }
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    int aa, bb, cc, dd;
    cin >> aa >> bb >> cc >> dd;
    a.push_back(aa);
    b.push_back(bb);
    c.push_back(cc);
    d.push_back(dd);
  }

  vector<int> A(q, 1);
  A[0] = 1;
  dfs(0, A, 1);
  cout << ans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

ll MOD = 1000000007;
vector<ll> a, b, c, d;
ll n, m, q;
vector<ll> arr;
ll maxi = -1;
void check() {
  ll i;
  ll ans = 0;

  for (i = 0; i < q; i++) {
    if (arr[b[i]] - arr[a[i]] == c[i]) {
      ans += d[i];
    }
  }
  maxi = max(maxi, ans);
}
void recurse(ll idx) {
  if (idx == n) {
    check();
  } else {
    ll i;
    for (i = arr[idx - 1]; i <= m; i++) {
      arr[idx] = i;
      recurse(idx + 1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // cin >>t;
  ll t = 1;

  while (t--) {
    // ll n,m,q;
    cin >> n >> m >> q;
    // ll a[q],b[q],c[q],d[q];
    ll i;
    a.resize(q);
    b.resize(q);
    c.resize(q);
    d.resize(q);
    arr.resize(n);

    for (i = 0; i < q; i++) {
      cin >> a[i] >> b[i] >> c[i] >> d[i];
      a[i]--;
      b[i]--;
    }

    arr[0] = 1;

    recurse(1);
    cout << maxi << endl;
    break;
  }
  return 0;
}

Error: 0

Fixed submission:

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

ll MOD = 1000000007;
vector<ll> a, b, c, d;
ll n, m, q;
vector<ll> arr;
ll maxi = -1;
void check() {
  ll i;
  ll ans = 0;

  for (i = 0; i < q; i++) {
    if (arr[b[i]] - arr[a[i]] == c[i]) {
      ans += d[i];
    }
  }
  maxi = max(maxi, ans);
}
ll recurse(ll idx) {
  if (idx == n) {
    check();
  } else {
    ll i;
    for (i = arr[idx - 1]; i <= m; i++) {
      arr[idx] = i;
      recurse(idx + 1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // cin >>t;
  ll t = 1;

  while (t--) {
    // ll n,m,q;
    cin >> n >> m >> q;
    // ll a[q],b[q],c[q],d[q];
    ll i;
    a.resize(q);
    b.resize(q);
    c.resize(q);
    d.resize(q);
    arr.resize(n);

    for (i = 0; i < q; i++) {
      cin >> a[i] >> b[i] >> c[i] >> d[i];
      a[i]--;
      b[i]--;
    }

    arr[0] = 1;

    recurse(1);
    cout << maxi << endl;
  }
  return 0;
}