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

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

#define DFS_ITEM 3
#define DEPTH 4
int N, M, Q, ans = 0;
vector<int> a(100), b(100), c(100), d(100);

void calc(vector<int> v) {
  int tmp = 0;
  for (int i = 0; i < Q; i++) {
    if (v[b[i]] - v[a[i]] == c[i])
      tmp += d[i];
  }

  ans = max(tmp, ans);
}
void DFS(int depth, vector<int> v) {
  if (depth == N) {
    calc(v);
    return;
  }
  int tmp = 0;
  if (depth == 0)
    tmp = 1;
  else
    tmp = v[depth - 1];

  for (int i = tmp; i <= M; i++) {
    v[depth] = i;
    DFS(depth + 1, v);
  }
}

int main() {
  cin >> N >> M >> Q;
  vector<int> v(1000);
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }

  DFS(0, v);

  cout << ans << endl;
}


Error: 0

Fixed submission:

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

#define DFS_ITEM 3
#define DEPTH 4
int N, M, Q, ans = 0;
vector<int> a(10), b(10), c(10), d(10);

vector<int> l(10, -1);

void calc(vector<int> v) {
  int tmp = 0;
  for (int i = 0; i < Q; i++) {
    if (v[b[i]] - v[a[i]] == c[i])
      tmp += d[i];
  }

  ans = max(tmp, ans);
}
void DFS(int depth, vector<int> v) {
  if (depth == N) {
    calc(v);
    return;
  }
  int tmp = 0;
  if (depth == 0)
    tmp = 1;
  else
    tmp = v[depth - 1];

  for (int i = tmp; i <= M; i++) {
    v[depth] = i;
    DFS(depth + 1, v);
  }
}

int main() {
  cin >> N >> M >> Q;
  vector<int> v(N);
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }

  DFS(0, v);

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

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

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

int check() {
  int curr = 0;
  for (int i = 0; i < q; i++) {
    if (t[b[i]] - t[a[i]] == c[i])
      curr += d[i];
  }
  return curr;
}

void dfs(int i, int cur) {
  if (i >= n) {
    mx = max(mx, check());
    return;
  }
  for (int j = cur; j <= m; j++) {
    t.push_back(j);
    dfs(i + 1, j);
    t.pop_back();
  }
}

void solve() {
  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 << mx << "\\n";
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Error: -11

Fixed submission:

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

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

int check() {
  int curr = 0;
  for (int i = 0; i < q; i++) {
    if (t[b[i]] - t[a[i]] == c[i])
      curr += d[i];
  }
  return curr;
}

void dfs(int i, int cur) {
  if (i >= n) {
    mx = max(mx, check());
  }
  for (int j = cur; j <= m; j++) {
    t.push_back(j);
    dfs(i + 1, j);
    t.pop_back();
  }
}

void solve() {
  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 << mx << "\\n";
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
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;
    for (int i = 0; i < q; i++)
      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);
  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;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
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;
    for (int i = 0; i < q; i++)
      if (s[b[i]] - s[a[i]] == c[i])
        now += d[i];
    ans = max(ans, now);
  }
  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);
  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: Runtime Error

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

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

long long dfs(vector<int> &A) {
  if (A.size() == N) {
    return score(A);
  }
  long long res = 0;
  int prev_last = (A.empty() ? 0 : A.back());
  for (int add = prev_last; add < M; ++add) {
    A.push_back(add);
    res = max(res, dfs(A));
    A.pop_back();
  }
  return res;
}

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];
  }
  vector<int> A;
  cout << dfs(A) << endl;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
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;
}

long long dfs(vector<int> &Q) {
  if (Q.size() == n) {
    return score(Q);
  }
  long long res = 0;
  int prev_last = (Q.empty() ? 0 : Q.back());
  for (int add = prev_last; add < m; ++add) {
    Q.push_back(add);
    res = max(res, dfs(Q));
    Q.pop_back();
  }
  return res;
}
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[q], --b[q];
  }
  vector<int> Q;
  cout << dfs(Q) << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
using vi = vector<int>;

// 特別に1を最初の要素とする
int64_t f(vector<int> &A, int i, int m, vi const &a, vi const &b, vi const &c,
          vi const &d) {
  int64_t high = 0;
  if ((i + 1) < A.size()) {
    for (A[i + 1] = A[i]; A[i + 1] <= m; A[i + 1]++) {
      auto ret = f(A, i + 1, m, a, b, c, d);
      if (high < ret)
        high = ret;
    }
    return high;
  } else {
    // スコアを求める
    int q = a.size();
    int64_t score = 0;
    for (int j = 0; j < q; j++) {
      if ((A[b[j]] - A[a[j]]) == c[j])
        score += d[j];
    }
    return score;
  }
}

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  int64_t high = 0;
  vi A(n + 1); // 特別に1を最初の要素とする
  A[0] = 1;
  high = f(A, 0, m, a, b, c, d);
  cout << high;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
using vi = vector<int>;

// 特別に1を最初の要素とする
int64_t f(vector<int> &A, int i, int m, vi const &a, vi const &b, vi const &c,
          vi const &d) {
  int64_t high = 0;
  if (i < A.size()) {
    for (A[i + 1] = A[i]; A[i + 1] <= m; A[i + 1]++) {
      auto ret = f(A, i + 1, m, a, b, c, d);
      if (high < ret)
        high = ret;
    }
    return high;
  } else {
    // スコアを求める
    int q = a.size();
    int64_t score = 0;
    for (int j = 0; j < q; j++) {
      if ((A[b[j]] - A[a[j]]) == c[j])
        score += d[j];
    }
    return score;
  }
}

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<int> a(q), b(q), c(q), d(q);
  for (int i = 0; i < q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  int64_t high = 0;
  vi A(n + 1); // 特別に1を最初の要素とする
  A[0] = 1;
  high = f(A, 0, m, a, b, c, d);
  cout << high;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

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

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

void pei(vector<int> A) {
  if (A.size() == N + 1) {
    int sum2 = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        sum2 += d[i];
      }
    }
    sum = max(sum, sum2);
    return;
  }
  A.push_back(A.back());
  while (A.back() <= M) {
    pei(A);
    A.back()++;
  }
}

int main() {
  int i;
  cin >> N >> M >> Q;
  a = b = c = d = vector<int>(Q);
  for (i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  pei(vector<int>(1, 1));
  cout << sum << endl;
  return 0;
}


Error: -11

Fixed submission:

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

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

void pei(vector<int> A) {
  if (A.size() == N) {
    int sum2 = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        sum2 += d[i];
      }
    }
    sum = max(sum, sum2);
    return;
  }
  A.push_back(A.back());
  while (A.back() <= M) {
    pei(A);
    A.back()++;
  }
}

int main() {
  int i;
  cin >> N >> M >> Q;
  vector<int> a(Q), b(Q), c(Q), d(Q);
  for (i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  sum = 0;
  pei(vector<int>(1, 1));
  cout << sum << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, M, Q;
set<vector<int>> SE;

void f(vector<int> tmp, int n) {
  if (n == N) {
    SE.insert(tmp);
    return;
  }
  int j;
  if (tmp.size())
    j = tmp.at(tmp.size() - 1);
  else
    j = 1;
  for (int i = j; i <= M; i++) {
    vector<int> tmp2;
    tmp2 = tmp;
    tmp2.push_back(i);
    f(tmp2, n + 1);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M >> Q;
  vector<vector<int>> V(Q, vector<int>(4));
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> V.at(i).at(j);
    }
  }
  vector<int> tmp;
  f(tmp, 0);
  int mx = 0;
  for (auto se : SE) {
    int sum = 0;
    for (int i = 0; i < Q; i++) {
      if (se.at(V.at(i).at(1) - 1) - se.at(V.at(i).at(0) - 1) == V.at(i).at(2))
        sum += V.at(i).at(3);
    }
    mx = max(mx, sum);
  }
  cout << mx << "\\n";
}


Error: TLE

Fixed submission:

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

int N, M, Q;
set<vector<int>> SE;

void f(vector<int> tmp, int n) {
  if (n == N) {
    sort(tmp.begin(), tmp.end());
    SE.insert(tmp);
    return;
  }
  for (int i = 1; i <= M; i++) {
    vector<int> tmp2;
    tmp2 = tmp;
    tmp2.push_back(i);
    f(tmp2, n + 1);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> N >> M >> Q;
  vector<vector<int>> V(Q, vector<int>(4));
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> V.at(i).at(j);
    }
  }
  vector<int> tmp;
  f(tmp, 0);
  int mx = 0;
  for (auto se : SE) {
    int sum = 0;
    for (int i = 0; i < Q; i++) {
      if (se.at(V.at(i).at(1) - 1) - se.at(V.at(i).at(0) - 1) == V.at(i).at(2))
        sum += V.at(i).at(3);
    }
    mx = max(mx, sum);
  }
  cout << mx << "\\n";
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using Graph = vector<vector<int>>;
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(now, ans);
    return;
  }

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

int main() {
  cin >> n >> m >> q;
  a = b = c = d = vector<int>(q);
  ans = 0;
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
  return 0;
}

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 rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> a(Q);
  vector<int> b(Q);
  vector<int> c(Q);
  vector<int> d(Q);
  rep(i, Q) { cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i); }
  vector<int> sum(M);
  rep(i, M) {
    vector<int> A(N);
    A.at(i) = i + 1;
    int x = 0;
    rep(k, M - 1) {
      rep(j, M - A.at(k) + 1) {
        A.at(k + 1) = A.at(k) + x;
        x++;
        rep(l, Q) {
          if (k + 1 == b.at(l)) {
            if (A.at(b.at(l) - 1) - A.at(a.at(l) - 1) == c.at(l)) {
              sum.at(i) += d.at(l);
            }
          }
        }
      }
    }
  }
  int maxsum = 0;
  rep(i, M - 1) { maxsum = max(sum.at(i), sum.at(i + 1)); }
  cout << maxsum << endl;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int dfs(int n, int m, int q, vector<int> a, vector<int> b, vector<int> c,
        vector<int> d, vector<int> A) {
  int sum = 0;
  if (A.size() == n + 1) {
    for (int i = 0; i < q; ++i) {
      if (A[b[i]] - A[a[i]] == c[i])
        sum += d[i];
    }
    return sum;
  }

  A.emplace_back(A.back());
  while (A.back() <= m) {
    sum = max(sum, dfs(n, m, q, a, b, c, d, A));
    A.back()++;
  }

  return sum;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  printf("%d\\n", dfs(n, m, q, a, b, c, d, vector<int>(1, 1)));

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

void dfs(vector<int> A) {
  if (A.size() == m) {
    int sum = 0;

    for (int i = 0; i < q; ++i) {
      if (A.at(a.at(i).at(1) - 1) - A.at(a.at(i).at(0) - 1) == a.at(i).at(2)) {
        sum += a.at(i).at(3);
      }
    }

    ans = max(ans, sum);

    return;
  }

  for (int i = A.back(); i <= m; ++i) {
    A.push_back(i);
    dfs(A);
    A.pop_back();
  }

  return;
}

int main() {

  cin >> n >> m >> q;

  a = vector<vector<int>>(q, vector<int>(4));

  for (int i = 0; i < q; ++i) {
    for (int j = 0; j < 4; ++j) {
      cin >> a.at(i).at(j);
    }
  }

  for (int i = 1; i <= m; ++i) {
    dfs(vector<int>(1, i));
  }

  printf("%d\\n", ans);

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

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

#define lp(var, start, end) for (ll var = start; var < end; ++var)
#define rlp(var, start, end) for (ll var = start; var >= end; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define vld vector<ld>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define vpll vector<pll>
#define vpld vector<pld>
#define all(X) X.begin(), X.end()
#define endl "\\n"
#define sz(x) ((ll)((x).size()))
const ll N = 1e2 + 5, inf = 1e9;

ll mx = 0;
ll q;
ll a[N], b[N], c[N], d[N];

ll cc = 0;
ll solve(ll n, ll m, ll i, ll p, vll v) {
  // cout<<i<<" ";
  if (i >= n) {
    ll ans = 0;
    cc += sz(v);
    lp(j, 0, q) {
      if (v[b[j] - 1] - v[a[j] - 1] == c[j]) {
        ans += d[j];
      }
    }
    mx = max(ans, mx);
    return 0;
  }
  lp(j, p, m + 1) {
    // cout<<j<<" ";
    v.pb(j);
    solve(n, m, i + 1, j, v);
    v.pop_back();
  }
  return 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  vll v;
  ll m;
  cin >> m;
  cin >> q;
  vpll v2;
  lp(i, 0, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    v2.pb(mp(a[i], c[i]));
  }
  solve(n, m, 0, 1, v);
  // cout<<cc<<endl;
  cout << mx << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define lp(var, start, end) for (ll var = start; var < end; ++var)
#define rlp(var, start, end) for (ll var = start; var >= end; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define vld vector<ld>
#define pll pair<ll, ll>
#define pld pair<ld, ld>
#define vpll vector<pll>
#define vpld vector<pld>
#define all(X) X.begin(), X.end()
#define endl "\\n"
#define sz(x) ((ll)((x).size()))
const ll N = 1e2 + 5, inf = 1e9;

ll mx = 0;
ll q;
ll a[N], b[N], c[N], d[N];
ll pre[N][20];
ll cnt[N];
ll solve(ll n, ll m, ll i, ll p, vll v) {
  if (i >= n) {
    ll ans = 0;
    lp(j, 0, q) {
      if (v[b[j] - 1] - v[a[j] - 1] == c[j]) {
        ans += d[j];
      }
    }
    mx = max(ans, mx);
    return 0;
  }
  lp(j, p, m + 1) {
    v.pb(j);
    solve(n, m, i + 1, j, v);
    v.pop_back();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  vll v;
  ll m;
  cin >> m;
  cin >> q;
  vpll v2;
  lp(i, 0, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    v2.pb(mp(a[i], c[i]));
  }
  solve(n, m, 0, 1, v);
  cout << mx << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>      //abs()かfabs()で少数絶対値
#include <cstdlib>    //abs()で整数絶対値
#include <functional> //sort第三引数greater<型名>()で降順
#include <iomanip>    //cout<<setw(数字) で空白による桁揃え
#include <iostream>   //cout<<right で右揃え
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> A(N, 1), a(Q), b(Q), c(Q), d(Q);
  A[N - 1] = 0;
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  int max = 0;
  do {
    A[N - 1]++;
    for (int i = N - 1; i >= 1; i--) {
      if (A[i] == M + 1) {
        A[i - 1]++;
        for (int j = i; j < N; j++) {
          A[j] = A[i - 1];
        }
      }
    }
    int sum = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        sum += d[i];
      }
    }
    if (sum > max)
      max = sum;
  } while (A[0] != M);
  cout << max << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>      //abs()かfabs()で少数絶対値
#include <cstdlib>    //abs()で整数絶対値
#include <functional> //sort第三引数greater<型名>()で降順
#include <iomanip>    //cout<<setw(数字) で空白による桁揃え
#include <iostream>   //cout<<right で右揃え
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> A(N, 1), 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 max = 0;
  do {
    A[N - 1]++;
    for (int i = N - 1; i >= 1; i--) {
      if (A[i] == M + 1) {
        A[i - 1]++;
        for (int j = i; j < N; j++) {
          A[j] = A[i - 1];
        }
      }
    }
    int sum = 0;
    for (int i = 0; i < Q; i++) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i]) {
        sum += d[i];
      }
    }
    if (sum > max)
      max = sum;
  } while (A[0] != M);
  cout << max << endl;
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int q, ans = 0;
vector<int> e;
vector<int> f;
struct matter {
  int a, b, c, d;
};
matter p[50];
void check() {
  int val = 0;
  for (int i = 0; i < q; i++) {
    if ((f[p[i].b - 1] - f[p[i].a - 1]) == p[i].c)
      val = val + p[i].d;
  }
  ans = max(ans, val);
}
void solve(int init, int k) {
  if (k == 0) {
    check();
    return;
  }
  int prev = 0;
  if (init != 0)
    prev = init - 1;
  for (int i = prev; i < e.size(); i++) {
    f.push_back(e[i]);
    solve(i + 1, k - 1);
    f.pop_back();
  }
}
int main() {
  int n, m;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < q; i++)
    scanf("%d%d%d%d", &p[i].a, &p[i].b, &p[i].c, &p[i].d);
  for (int i = 0; i < m; i++)
    e.push_back(i + 1);
  solve(0, n);
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int q, ans = 0;
vector<int> e;
vector<int> f;
struct matter {
  int a, b, c, d;
};
matter p[50];
void check() {
  int val = 0;
  for (int i = 0; i < q; i++) {
    if ((f[p[i].b - 1] - f[p[i].a - 1]) == p[i].c)
      val = val + p[i].d;
  }
  ans = max(ans, val);
}
void solve(int init, int k) {
  if (k == 0) {
    check();
    return;
  }
  int prev = 0;
  if (init != 0)
    prev = init - 1;
  for (int i = prev; i <= (e.size() - k); i++) {
    f.push_back(e[i]);
    solve(i + 1, k - 1);
    f.pop_back();
  }
}
int main() {
  int n, m;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < q; i++)
    scanf("%d%d%d%d", &p[i].a, &p[i].b, &p[i].c, &p[i].d);
  for (int i = 0; i < m; i++)
    e.push_back(i + 1);
  solve(0, n);
  printf("%d\\n", ans);
  return 0;
}
Problem p02695 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <vector>
#define int long long
typedef long long ll;
using namespace std;

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

void dfs(ll id, vector<ll> v) {
  // if(v[id-1]>m) return;
  if (id == n + 1) {
    // for(int i=1;i<=n;i++) cout << " " << v[i];
    // cout << endl;
    // cout << endl;
    ll maxv = 0;
    for (int k = 0; k < q; k++) {
      if (v[b[k]] - v[a[k]] == c[k])
        maxv += d[k];
    }
    ans = max(ans, maxv);
    return;
  }
  for (int i = v[id - 1]; i <= m; i++) {
    v[id] = i;
    dfs(id + 1, v);
  }
  // v[id]=v[id-1];
  // dfs(id+1, v);
  // v[id]=v[id-1]+1;
  // dfs(id+1, v);
}

signed 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];
  }
  vector<ll> v(n + 1, 1);
  // cout << ans << endl;
  dfs(1, v);
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <map>
#include <vector>
#define int long long
typedef long long ll;
using namespace std;

ll n, m, q;
vector<int> a, b, c, d;
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()++;
  }
}

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