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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool nextvect(vector<int> &A, int N, int M) {
  if (A[N - 1] < M)
    A[N - 1]++;
  else {
    int i = N - 2;
    while (i >= 0 && A[i] == M)
      i--;
    if (i == -1)
      return false;
    A[i]++;
    for (int j = i + 1; j < N; j++)
      A[j] = A[i];
  }
  return true;
}

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

  vector<vector<int>> Queries(Q, vector<int>(4));
  for (int i = 0; i < Q; i++)
    cin >> Queries[i][0] >> Queries[i][1] >> Queries[i][2] >> Queries[i][3];
  vector<int> A(N, 1);
  int MAX = 0;
  while (1) {
    int sum = 0;
    for (int i = 0; i < Q; i++)
      if (A[Queries[i][1] - 1] - A[Queries[i][0] - 1] == Queries[i][2])
        sum += Queries[i][3];
    MAX = max(MAX, sum);
    if (!nextvect(A, N, M))
      break;
  }
  cout << MAX << endl;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

bool nextvect(vector<int> &A, int N, int M) {
  if (A[N - 1] < M)
    A[N - 1]++;
  else {
    int i = N - 2;
    while (i >= 0 && A[i] == M)
      i--;
    if (i == -1)
      return false;
    A[i]++;
    for (int j = i + 1; j < N; j++)
      A[j] = A[i];
  }
}

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

  vector<vector<int>> Queries(Q, vector<int>(4));
  for (int i = 0; i < Q; i++)
    cin >> Queries[i][0] >> Queries[i][1] >> Queries[i][2] >> Queries[i][3];
  vector<int> A(N, 1);
  int MAX = 0;
  while (1) {
    int sum = 0;
    for (int i = 0; i < Q; i++)
      if (A[Queries[i][1] - 1] - A[Queries[i][0] - 1] == Queries[i][2])
        sum += Queries[i][3];
    MAX = max(MAX, sum);
    if (!nextvect(A, N, M))
      break;
  }
  cout << MAX << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;

static const long long MOD = 1000000007;
static const long long INF = 1 << 31;

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

int dfs(int p) {
  int res = 0;

  if (p == n + 1) {
    /*
    rep(i, 1, n+1) {
      printf("%d", x[i]);
    }
    printf("\\n");
    */
    rep(i, 0, q) if (x[b[i]] - x[a[i]] == c[i]) res += d[i];
    return res;
  }

  for (int i = x[p - 1]; i <= m; i++) {
    x[p] = i;
    res = max(res, dfs(p + 1));
  }
  return res;
}

int main(void) {
  cin >> n >> m >> q;
  rep(i, 0, q) { scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]); }

  x[1] = 1;
  int ans = dfs(2);

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

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define pb push_back
#define mp make_pair
#define bg begin()
#define en end()
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)(v).size()

using namespace std;

typedef long long ll;
typedef long double ld;

static const long long MOD = 1000000007;
static const long long INF = 1 << 31;

int ans;
int n, m, q;
int a[55], b[55], c[55], d[55];
int t[15];

int main(void) {
  cin >> n >> m >> q;
  rep(i, 0, q) {
    scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]);
    a[i]--;
    b[i]--;
  }

  for (int i = 0; i < (1 << q); i++) {
    memset(t, 1, sizeof(t));
    for (int j = 0; j < q; j++) {
      if ((i >> j) & 1) {
        t[b[i]] = t[a[i]] + c[i];
      }
    }

    int sum = 0;
    for (int i = 0; i < n; i++) {
      if (t[i] > m) {
        sum = -1;
        break;
      }
      if (t[b[i]] - t[a[i]] == c[i])
        sum += d[i];
    }
    ans = max(ans, sum);
  }
  cout << ans << endl;

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

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

ll ans = 0;
ll N, M, Q;
ll a[51], b[51], c[51], d[51];

ll calc_sum(vector<int> A) {
  ll res = 0;
  rep(i, Q) {
    if (A[b[i] - 1] - A[a[i] - 1] == c[i])
      res += d[i];
  }
  return res;
}

void dfs(vector<int> A) {
  if (int(A.size()) == N) {
    ans = max(ans, calc_sum(A));
  } else {
    for (int i = A[A.size() - 1]; i <= M; ++i) {
      A.push_back(i);
      dfs(A);
      A.erase(A.end() - 1);
    }
  }
}

int main() {
  cin >> N >> M >> Q;
  rep(i, Q) cin >> a[i] >> b[i] >> c[i] >> d[i];
  vector<int> A;
  A.push_back(1);
  dfs(A);
  cout << ans << endl;
}

Error: -11

Fixed submission:

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

ll ans = 0;
ll Q;
vll a(Q), b(Q), c(Q), d(Q);

ll calc_sum(vector<int> A) {
  ll res = 0;
  rep(i, Q) {
    if (A[b[i] - 1] - A[a[i] - 1] == c[i])
      ans += d[i];
  }
  return res;
}

void dfs(int N, int M, vector<int> A) {
  if (A.size() == N)
    ans = max(ans, calc_sum(A));
  else {
    for (int i = A[A.size() - 1]; i <= M; ++i) {
      A.push_back(i);
    }
  }
}

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

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

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

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

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

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

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

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

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<int, int>;
/* ちゃんと考えてわかって実装 */

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

ll calc_score(const vector<int> &x) {
  ll res = 0;
  for (int i = 0; i < q; i++) {
    if (x[b[i]] - x[a[i]] == c[i])
      res += d[i];
  }
  return res;
}

ll dfs(vector<int> &A) {
  if (A.size() == n) {
    return calc_score(A);
  }

  ll res = 0;
  int last = (A.empty() ? 0 : A.back());
  for (int i = last; i < m; i++) {
    A.push_back(i);
    res = max(res, dfs(A));
    A.pop_back();
  }
  return res;
}

int main(void) {

  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> zzz;
  cout << dfs(zzz) << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<int, int>;
/* ちゃんと考えてわかって実装 */

int main(void) {
  int n, m, q;
  cin >> n >> m >> q;

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

  int ans = 0;

  // 同じ数字が
  for (int bit_n = 0; bit_n < (1 << n - 1); bit_n++) {
    for (int bit_m = 0; bit_m < (1 << m); bit_m++) {
      int tmp_ans = 0;

      int num_n = __builtin_popcount(bit_n);
      int num_m = __builtin_popcount(bit_m);
      if ((n - num_n) != num_m)
        continue;

      int idx = 1;
      vector<int> AA(n + 1);
      for (int i = 0; i < m; i++) {
        if (bit_m & (1 << i)) {
          AA[idx] = i + 1;
          idx++;
          int now_idx = idx - 1;
          for (int j = now_idx; j < n - 1; j++) {
            if (bit_n & (1 << (j)))
              AA[idx] = i + 1;
            idx++;
          }
        }
      }

      for (int i = 0; i < q; i++) {
        if ((AA[b[i]] - AA[a[i]]) == c[i]) {
          tmp_ans += d[i];
        }
      }

      ans = max(ans, tmp_ans);
    }
  }

  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)
#define rep2(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Sort(x) sort((x).begin(), (x).end())
#define Sort2(x) sort((x).begin(), (x).end(), greater<int>())
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

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

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int sum = 0;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i])
        sum += d[i];
    }
    mx_sum = max(sum, mx_sum);
    return;
  }
  // 同じかそれ以上を追加する
  A.pb(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 << mx_sum << endl;
  return 0;
}

/*
g++ c.cpp
./a.out

oj d https://atcoder.jp/contests/abc169/tasks/abc169_b
oj t
*/


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, n) for (int i = 1; i <= (n); ++i)
#define all(x) (x).begin(), (x).end()
#define Sort(x) sort((x).begin(), (x).end())
#define Sort2(x) sort((x).begin(), (x).end(), greater<int>())
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

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

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int sum = 0;
    rep(i, q) {
      if (A[b[i]] - A[a[i]] == c[i])
        sum += d[i];
    }
    mx_sum = max(sum, mx_sum);
    return;
  }
  // 同じかそれ以上を追加する
  A.pb(A.back());
  while (A.back() <= m) {
    dfs(A);
    A.back()++;
  }
}

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

  // 数列を作る。得点を出す。最大を出す。
  dfs(vector<int>(1, 1));
  cout << mx_sum << endl;
}

/*
g++ c.cpp
./a.out

oj d https://atcoder.jp/contests/abc169/tasks/abc169_b
oj t
*/
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

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

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

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

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

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


Error: 0

Fixed submission:

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

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

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

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

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

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

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

Error: 0

Fixed submission:

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

Fixed submission:

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

int n, m, q;

vector<int> a(q), b(q), c(q), d(q);

int ans;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int now = 0;
    for (int i = 0; i < q; i++) {
      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;
  for (int i = 0; i < q; i++) {
    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: Time Limit Exceeded

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

int n, m, q;
vector<int> a(55, 0);
vector<int> b(55, 0);
vector<int> c(55, 0);
vector<long long> d(55, 0);
vector<vector<int>> ansvec;

void dfs(int pre, int now, vector<int> &vec) {
  long long ans = 0;
  if (now == n) {
    ansvec.emplace_back(vec);
    return;
  }
  for (int i = pre; i <= m; ++i) {
    vec.at(now) = i;
    dfs(i, now + 1, vec);
  }
}

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

  vector<int> A(10, 0);
  dfs(1, 0, A);

  long long mans = 0;
  for (int i = 0; i < ansvec.size(); ++i) {
    long long ans = 0;
    for (int j = 0; j < q; ++j) {
      if (ansvec.at(i).at(b.at(j)) - ansvec.at(i).at(a.at(j)) == c.at(j))
        ans += d.at(j);
    }
    chmax(mans, ans);
  }
  cout << mans << endl;
}

Error: TLE

Fixed submission:

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

int n, m, q;
vector<int> a(55, 0);
vector<int> b(55, 0);
vector<int> c(55, 0);
vector<long long> d(55, 0);
long long mans = 0;
vector<vector<int>> ansvec;

void dfs(int pre, int now, vector<int> &vec) {
  long long ans = 0;
  if (now == n) {
    ansvec.emplace_back(vec);
    return;
  }
  vec.push_back(0);
  for (int i = pre; i <= m; ++i) {
    vec.at(now) = i;
    dfs(i, now + 1, vec);
  }
}

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

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

  long long mans = 0;
  for (int i = 0; i < ansvec.size(); ++i) {
    long long ans = 0;
    for (int j = 0; j < q; ++j) {
      if (ansvec.at(i).at(b.at(j)) - ansvec.at(i).at(a.at(j)) == c.at(j))
        ans += d.at(j);
    }
    chmax(mans, ans);
  }
  cout << mans << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define reps(i, n) for (ll i = 1; i <= (n); ++i)
#define _rep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define _reps(i, n) for (ll i = n; i > 0; --i)
#define all(x) (x).begin(), (x).end()
#define _all(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

ll n, m, q;
ll a[50], b[50], c[50], d[50];
ll x[11];
ll ans = 0;

void dfs(ll r, ll num) {
  if (r > n) {
    ll tmp = 0;
    rep(i, q) {
      if (x[b[i]] - x[a[i]] == c[i]) {
        tmp += d[i];
      }
    }
    ans = max(ans, tmp);
    return;
  }

  for (int i = num; i <= m; i++) {
    x[r] = i;
    dfs(r + 1, i);
  }
}

int main(void) {
  cin >> n >> m >> q;

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

  dfs(1, 1);

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define reps(i, n) for (ll i = 1; i <= (n); ++i)
#define _rep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define _reps(i, n) for (ll i = n; i > 0; --i)
#define all(x) (x).begin(), (x).end()
#define _all(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

ll n, m, q;
ll a[50], b[50], c[50], d[50];
ll x[11];
ll ans = 0;

void dfs(ll r) {
  ll j;
  for (j = 1; j <= n; j++) {
    if (x[j] == 0) {
      break;
    }
  }
  if (j == n + 1) {
    ll tmp = 0;
    rep(i, q) {
      if (x[b[i]] - x[a[i]] == c[i]) {
        tmp += d[i];
      }
    }
    ans = max(ans, tmp);
  }

  rep(i, q) {
    if (b[i] == r) {
      if (x[a[i]] == 0) {
        x[a[i]] = x[b[i]] - c[i];
        dfs(a[i]);
        x[a[i]] = 0;
      }
    } else if (a[i] == r) {
      if (x[b[i]] == 0) {
        x[b[i]] = x[a[i]] + c[i];
        dfs(b[i]);
        x[b[i]] = 0;
      }
    }
  }
}

int main(void) {
  cin >> n >> m >> q;

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

  rep(i, n) { x[i] = -1; }
  rep(i, q) {
    x[a[i]] = 0;
    x[b[i]] = 0;
  }
  x[n] = m;
  dfs(n);

  cout << ans << endl;

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

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

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


Error: 0

Fixed submission:

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

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
#define ll long long
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#define inf 0x3f3f3f3f
using namespace std;
int p[55];
int a[55], b[55], c[55], d[55];
int sum = -inf;
int n, m, q;
void choc() {
  int x = 0;
  for (int i = 0; i < q; i++) {
    if (p[b[i]] - p[a[i]] == c[i]) {
      x += d[i];
    }
  }
  sum = max(sum, x);
  return;
}
void dfs(int now, int s) {
  p[now] = s;
  if (now == n) {
    choc();
    return;
  }
  for (int i = s; i <= m; i++) {
    dfs(now + 1, i);
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(1, 1);
  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
#define ll long long
#define IO                                                                     \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie();                                                                   \\
  cout.tie(0)
#define inf 0x3f3f3f3f
using namespace std;
int p[55];
int a[55], b[55], c[55], d[55];
int sum = -inf;
int n, m, q;
void choc() {
  int x = 0;
  for (int i = 0; i < q; i++) {
    if (p[b[i]] - p[a[i]] == c[i]) {
      x += d[i];
    }
  }
  sum = max(sum, x);
  return;
}
int dfs(int now, int s) {
  p[now] = s;
  if (now == n) {
    choc();
    return 0;
  }
  for (int i = s; i <= m; i++) {
    dfs(now + 1, i);
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(0, 1);
  cout << sum << endl;
  return 0;
}