prithivMLmods/Coder-Stat huggingface dataset:
Problem p02694 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

int main() {

  int x, res = 0, curr = 100;
  cin >> x;

  while (curr < x) {
    res++;
    curr += curr * 0.01;
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {

  long long x, res = 0, curr = 100;
  cin >> x;

  while (curr < x) {
    res++;
    curr += curr * 0.01;
  }
  cout << res << endl;
  return 0;
}
Problem p02694 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
int func(int a, int b, int cnt) {
  if (a >= b) {
    return cnt;
  }
  func(a + a * 0.01, b, cnt + 1);
}
signed main() {
  int x;
  cin >> x;
  cout << func(100, x, 0) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int func(int a, int b, int cnt) {
  if (a >= b) {
    return cnt;
  }
  return func(a + a * 0.01, b, cnt + 1);
}
signed main() {
  int x;
  cin >> x;
  cout << func(100, x, 0) << endl;
}
Problem p02694 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int n = 100;
  int x;
  int count = 0;
  cin >> x;
  while (n < x) {
    count += 1;
    n += n / 100;
  }
  cout << count << '\n';
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {
  long long int n = 100;
  long long int x;
  int count = 0;
  cin >> x;
  while (n < x) {
    count += 1;
    n += n / 100;
  }
  cout << count << '\n';
  return 0;
}
Problem p02694 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

int32_t main() {
  FIO;
  int cur = 100;
  int x;
  cin >> x;
  int yr = 0;
  while (cur < x)
    cur = (101 * cur) / 100, yr++;

  cout << yr << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

int32_t main() {
  FIO;
  int cur = 100;
  int x;
  cin >> x;
  int yr = 0;
  while (cur < x) {
    cur = cur + cur / 100;
    yr++;
  }

  cout << yr << '\n';
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int N, M, Q;
vector<int> a, b, c, d;
long long ans;
long long res, res2;
vector<int> num;
long long dfs(int i, int u) {
  long long t2 = 0;
  if (u == N) {
    long long t = 0;
    for (int j = 0; j < Q; j++) {
      if (num[b[j] - 1] - num[a[j] - 1] == c[j])
        t += d[j];
    }
    return t;
  }
  num[u] = i;
  for (int j = 0; j + i <= M; j++) {
    t2 = max(t2, dfs(i + j, u + 1));
  }
  return t2;
}
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];
  }
  ans = 0;
  for (int i = 1; i <= M; i++) {
    ans = max(ans, dfs(i, 0));
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int N, M, Q;
vector<int> a, b, c, d;
long long ans;
long long res, res2;
vector<int> num;
long long dfs(int i, int u) {
  long long t2 = 0;
  if (u == N) {
    long long t = 0;
    for (int j = 0; j < Q; j++) {
      if (num[b[j] - 1] - num[a[j] - 1] == c[j])
        t += d[j];
    }
    return t;
  }
  num[u] = i;
  for (int j = 0; j + i <= M; j++) {
    t2 = max(t2, dfs(i + j, u + 1));
  }
  return t2;
}
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];
  }
  ans = 0;
  num.resize(N);
  for (int i = 1; i <= M; i++) {
    ans = max(ans, dfs(i, 0));
  }
  cout << ans << endl;
}
Problem p02695 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, q;

vector<int> a, b, c, d, A;
int saiki(int k, int l, int p, vector<int> A);

int main() {
  int as;
  // cerr << "test";
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    int e, f, g, h;
    cin >> e >> f >> g >> h;
    a.push_back(e);
    b.push_back(f);
    c.push_back(g);
    d.push_back(h);
  }
  // cerr << "test";
  as = saiki(n, m, q, A);
  cout << as << endl;
}
int saiki(int k, int l, int p, vector<int> A) {
  int ans = 0;
  vector<int> B = A;
  // cerr << k << l   << p  <<endl;
  if (k != 0) {
    for (int i = 1; i <= l; i++) {
      B.push_back(i);
      ans = max(ans, saiki(k - 1, i, p, B));
      B.pop_back();
    }
    return ans;
  } else {
    for (int i = 0; i < p; i++) {
      for (int j = 0; j < A.size(); j++)
        cerr << A[j];
      cerr << endl;
      if (A.at(A.size() - b.at(i)) - A.at(A.size() - a.at(i)) == c.at(i))
        ans += d.at(i);
    }
    return ans;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, q;

vector<int> a, b, c, d, A;
int saiki(int k, int l, int p, vector<int> A);

int main() {
  int as;
  // cerr << "test";
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++) {
    int e, f, g, h;
    cin >> e >> f >> g >> h;
    a.push_back(e);
    b.push_back(f);
    c.push_back(g);
    d.push_back(h);
  }
  // cerr << "test";
  as = saiki(n, m, q, A);
  cout << as << endl;
}
int saiki(int k, int l, int p, vector<int> A) {
  int ans = 0;
  vector<int> B = A;
  // cerr << k << l   << p  <<endl;
  if (k != 0) {
    for (int i = 1; i <= l; i++) {
      B.push_back(i);
      ans = max(ans, saiki(k - 1, i, p, B));
      B.pop_back();
    }
    return ans;
  } else {
    for (int i = 0; i < p; i++) {
      // for(int j=0;j<A.size();j++)cerr << A[j];
      // cerr << endl;
      if (A.at(A.size() - b.at(i)) - A.at(A.size() - a.at(i)) == c.at(i))
        ans += d.at(i);
    }
    return ans;
  }
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e9
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \
  std::vector<type> c(n);                                                      \
  for (auto &i : c)                                                            \
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

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

int n, m, q;
vector<int> a(20), b(20), c(20), d(20), A(20);

int res = 0;
void dfs(int dep, int x) {

  if (dep == n) {
    int sum = 0;
    rep(i, q) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        sum += d[i];
    }
    res = max(res, sum);
    return;
  }

  REP(i, x, m) {
    A[dep] = i;
    dfs(dep + 1, i);
  }
}

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

  dfs(0, 1);
  cout << res << endl;
}


Fixed submission:
#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e9
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \
  std::vector<type> c(n);                                                      \
  for (auto &i : c)                                                            \
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

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

int n, m, q;
vector<int> a(100), b(100), c(100), d(100), A(20);

int res = 0;
void dfs(int dep, int x) {

  if (dep == n) {
    int sum = 0;
    rep(i, q) {
      if (A[b[i] - 1] - A[a[i] - 1] == c[i])
        sum += d[i];
    }
    res = max(res, sum);
    return;
  }

  REP(i, x, m) {
    A[dep] = i;
    dfs(dep + 1, i);
  }
}

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

  dfs(0, 1);
  cout << res << endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int a[20], b[20], c[20], d[20];
long long ans = 0;
int l[20];
int n, m, q;
void dfs(int x, int y) // x当前第几位,这一位是几
{
  l[x] = y;
  if (x == n) {
    long long sum = 0;
    for (int i = 1; i <= q; i++)
      if (l[b[i]] - l[a[i]] == c[i])
        sum += d[i];
    ans = max(ans, sum);
    return;
  } else {
    for (int i = y; i <= m; i++) {
      dfs(x + 1, i);
    }
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(1, 1);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int a[100], b[100], c[100], d[100];
long long ans = 0;
int l[20];
int n, m, q;
void dfs(int x, int y) // x当前第几位,这一位是几
{
  l[x] = y;
  if (x == n) {
    long long sum = 0;
    for (int i = 1; i <= q; i++)
      if (l[b[i]] - l[a[i]] == c[i])
        sum += d[i];
    ans = max(ans, sum);
    return;
  } else {
    for (int i = y; i <= m; i++) {
      dfs(x + 1, i);
    }
  }
}
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(1, 1);
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: 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 = 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;
}

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 = 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;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int odp = 0, n, m, q;

void rec(vector<int> x, int nr, int co, vector<int> &a, vector<int> &b,
         vector<int> &c, vector<int> &d) {
  x[nr] = co;
  if (nr == n) {
    int wyn = 0;
    for (int i = 0; i < q; ++i)
      if (x[b[i]] - x[a[i]] == c[i])
        wyn += d[i];
    odp = max(odp, wyn);
    return;
  }
  for (int i = co; i <= m; ++i)
    rec(x, nr + 1, i, a, b, c, d);
}

int main() {
  ios_base::sync_with_stdio(0);
  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];
    --a[i];
    --b[i];
  }
  vector<int> x(n);
  for (int i = 1; i <= m; ++i)
    rec(x, 0, i, a, b, c, d);
  cout << odp;
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int odp = 0, n, m, q;

void rec(vector<int> x, int nr, int co, vector<int> &a, vector<int> &b,
         vector<int> &c, vector<int> &d) {
  x[nr] = co;
  if (nr == n - 1) {
    int wyn = 0;
    for (int i = 0; i < q; ++i)
      if (x[b[i]] - x[a[i]] == c[i])
        wyn += d[i];
    odp = max(odp, wyn);
    return;
  }
  for (int i = co; i <= m; ++i)
    rec(x, nr + 1, i, a, b, c, d);
}

int main() {
  ios_base::sync_with_stdio(0);
  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];
    --a[i];
    --b[i];
  }
  vector<int> x(n);
  for (int i = 1; i <= m; ++i)
    rec(x, 0, i, a, b, c, d);
  cout << odp;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: 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;   // 1  1 1
  while (!a[0]) // a[0]=0是标志位   不等于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;
}


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 <= q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  for (int i = 1; i <= n; i++)
    A[i] = 1;   // 1  1 1
  while (!A[0]) // A[0]=0是标志位   不等于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;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int recursive(int col, vector<int> &current, int n, int m, int q,
              vector<vector<int>> &aa, vector<vector<int>> &cc,
              vector<vector<int>> &dd);

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

  vector<vector<int>> aa(m + 1), cc(m + 1), dd(m + 1);
  for (int i = 0; i < q; ++i) {
    aa.at(b[i]).push_back(a[i]);
    cc[b[i]].push_back(c[i]);
    dd[b[i]].push_back(d[i]);
  }

  vector<int> current(n + 1);
  current[0] = 1;
  // fprintf(stderr,"prepared completedn");
  cout << recursive(1, current, n, m, q, aa, cc, dd) << endl;
}

int recursive(int col, vector<int> &current, int n, int m, int q,
              vector<vector<int>> &aa, vector<vector<int>> &cc,
              vector<vector<int>> &dd) {
  int score, maxscore;

  // fprintf(stderr,"col %d, current %d\n",col,current[col-1]);
  maxscore = 0;
  for (int i = current[col - 1]; i <= m; ++i) {
    score = 0;
    for (int j = 0; j < aa[col].size(); ++j) {
      if (i - current[aa[col][j]] == cc[col][j]) {
        score += dd[col][j];
      }
    }
    current[col] = i;
    if (col < n) {
      score += recursive(col + 1, current, n, m, q, aa, cc, dd);
    }
    if (score > maxscore) {
      maxscore = score;
    }
  }
  return maxscore;
}


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

int recursive(int col, vector<int> &current, int n, int m, int q,
              vector<vector<int>> &aa, vector<vector<int>> &cc,
              vector<vector<int>> &dd);

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

  vector<vector<int>> aa(n + 1), cc(n + 1), dd(n + 1);
  for (int i = 0; i < q; ++i) {
    aa.at(b[i]).push_back(a[i]);
    cc[b[i]].push_back(c[i]);
    dd[b[i]].push_back(d[i]);
  }

  vector<int> current(n + 1);
  current[0] = 1;
  // fprintf(stderr,"prepared completedn");
  cout << recursive(1, current, n, m, q, aa, cc, dd) << endl;
}

int recursive(int col, vector<int> &current, int n, int m, int q,
              vector<vector<int>> &aa, vector<vector<int>> &cc,
              vector<vector<int>> &dd) {
  int score, maxscore;

  // fprintf(stderr,"col %d, current %d\n",col,current[col-1]);
  maxscore = 0;
  for (int i = current[col - 1]; i <= m; ++i) {
    score = 0;
    for (int j = 0; j < aa[col].size(); ++j) {
      if (i - current[aa[col][j]] == cc[col][j]) {
        score += dd[col][j];
      }
    }
    current[col] = i;
    if (col < n) {
      score += recursive(col + 1, current, n, m, q, aa, cc, dd);
    }
    if (score > maxscore) {
      maxscore = score;
    }
  }
  return maxscore;
}
Problem p02695 (C++)

Buggy submission (status: 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;
  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;
}

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;
  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;
}
Problem p02695 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int A[15], n, m, q, a[55], b[55], c[55], d[55], res, 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;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int A[15], n, m, q, a[55], b[55], c[55], d[55], res, 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;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i < (n + 1); 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;
}


Fixed submission:
#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;
}
Problem p02695 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p02695 (C++)

Buggy submission (status: 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(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;
}

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(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;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int funcovf(int *Ax, int i, int M) {
  Ax[i]++;

  if (Ax[i] > M) {
    funcovf(Ax, i - 1, M);
    Ax[i] = Ax[i - 1];
  }
  return 0;
}

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

  int ai[50];
  int bi[50];
  int ci[50];
  int di[50];

  for (int i = 0; i < Q; i++) {
    cin >> ai[i] >> bi[i] >> ci[i] >> di[i];
  }

  int Ax[10];
  for (int i = 0; i < 10; i++) {
    Ax[i] = 1;
  }

  int maxnum = 0;

  while (1) {
    // check
    int tempnum = 0;
    for (int i = 0; i < Q; i++) {
      if ((Ax[bi[i] - 1] - Ax[ai[i] - 1]) == ci[i]) {
        tempnum = tempnum + di[i];
      }
    }
    if (tempnum > maxnum) {
      maxnum = tempnum;
    }

    // Ax[i] change
    funcovf(Ax, N - 1, M);
    if (Ax[0] > 1) {
      break;
    }
  }
  cout << maxnum << std::endl;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int funcovf(int *Ax, int i, int M) {
  if (i == 0) {
    Ax[0] = 2;
    return 0;
  }

  Ax[i]++;

  if (Ax[i] > M) {
    funcovf(Ax, i - 1, M);
    Ax[i] = Ax[i - 1];
  }
  return 0;
}

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

  int ai[50];
  int bi[50];
  int ci[50];
  int di[50];

  for (int i = 0; i < Q; i++) {
    cin >> ai[i] >> bi[i] >> ci[i] >> di[i];
  }

  int Ax[10];
  for (int i = 0; i < 10; i++) {
    Ax[i] = 1;
  }

  int maxnum = 0;

  while (1) {
    // check
    int tempnum = 0;
    for (int i = 0; i < Q; i++) {
      if ((Ax[bi[i] - 1] - Ax[ai[i] - 1]) == ci[i]) {
        tempnum = tempnum + di[i];
      }
    }
    if (tempnum > maxnum) {
      maxnum = tempnum;
    }

    // Ax[i] change
    funcovf(Ax, N - 1, M);
    if (Ax[0] > 1) {
      break;
    }
  }
  cout << maxnum << std::endl;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using lint = long long;

int arr[50][4] = {0};
int Q;

int dfs(vector<int> A, int digit, int max, int N, int M) {
  if (A.size() < digit) {
    int sum = 0;
    for (int i = 0; i <= Q; i++) {
      if (A[arr[i][1]] - A[arr[i][0]] == arr[i][2]) {
        sum += arr[i][3];
      }
    }

    return sum;
  }
  for (int i = A[digit - 1]; i < M; i++) {
    A[digit] = i;
    int x = dfs(A, digit + 1, max, N, M);
    if (x > max) {
      max = x;
    }
  }
  return max;
}

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

  for (int i = 0; i < Q; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2] >> arr[i][3];
  }
  vector<int> A(N + 1, 0);
  cout << dfs(A, 1, 0, N, M) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using lint = long long;

int arr[50][4] = {0};
int Q;

int dfs(vector<int> A, int digit, int max, int N, int M) {
  if (N < digit) {
    int sum = 0;
    for (int i = 0; i <= Q; i++) {
      if (A[arr[i][1]] - A[arr[i][0]] == arr[i][2]) {
        sum += arr[i][3];
      }
    }

    return sum;
  }
  for (int i = A[digit - 1]; i < M; i++) {
    A[digit] = i;
    int x = dfs(A, digit + 1, max, N, M);
    if (x > max) {
      max = x;
    }
  }
  return max;
}

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

  for (int i = 0; i < Q; i++) {
    cin >> arr[i][0] >> arr[i][1] >> arr[i][2] >> arr[i][3];
  }
  vector<int> A(N + 1, 0);
  cout << dfs(A, 1, 0, N, M) << endl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: 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());
  }
  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;
}


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());
    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;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
long mod = (long)1e9 + 7;
int n, m, q, a[10], b[10], c[10], d[10], ans = 0;

int bfs(vector<int> t) {

  if (t.size() == n) {
    int sum = 0;
    rep(i, q) {
      if (t[b[i]] - t[a[i]] == c[i])
        sum += d[i];
    }
    ans = max(ans, sum);

  } else {
    int back = 1;
    if (t.size() > 0)
      back = t.back();
    for (int i = back; i <= m; i++) {
      t.push_back(i);
      bfs(t);
      t.pop_back();
    }
  }
  return 0;
}

int main() {

  cin >> n >> m >> q;

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

  bfs({});

  cout << ans << "\n";

  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
long mod = (long)1e9 + 7;
int n, m, q, a[50], b[50], c[50], d[50], ans = 0;

int bfs(vector<int> t) {

  if (t.size() == n) {
    int sum = 0;
    rep(i, q) {
      if (t[b[i]] - t[a[i]] == c[i])
        sum += d[i];
    }
    ans = max(ans, sum);

  } else {
    int back = 1;
    if (t.size() > 0)
      back = t.back();
    for (int i = back; i <= m; i++) {
      t.push_back(i);
      bfs(t);
      t.pop_back();
    }
  }
  return 0;
}

int main() {

  cin >> n >> m >> q;

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

  bfs({});

  cout << ans << "\n";

  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

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

void dfs(vector<int> v, int depth, int lastNum) {
  if (depth == N) {
    // 数列が完成したらスコア計算
    long long score = 0;
    for (int i = 0; i < Q; i++) {
      if (v[b[i] - 1] - v[a[i] - 1] == c[i])
        score += d[i];
    }
    ans = max(ans, score);
  } else {
    for (int i = lastNum; i < M + 1; i++) {
      // 末尾の数字以上の数字を結合したパターン分岐
      vector<int> nw = v;
      nw.push_back(i);
      dfs(nw, depth + 1, i);
    }
  }
}

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

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

int N, M, Q;
long long ans = 0;
vector<int> a(50), b(50), c(50), d(50);

void dfs(vector<int> v, int depth, int lastNum) {
  if (depth == N) {
    // 数列が完成したらスコア計算
    long long score = 0;
    for (int i = 0; i < Q; i++) {
      if (v[b[i] - 1] - v[a[i] - 1] == c[i])
        score += d[i];
    }
    ans = max(ans, score);
  } else {
    for (int i = lastNum; i < M + 1; i++) {
      // 末尾の数字以上の数字を結合したパターン分岐
      vector<int> nw = v;
      nw.push_back(i);
      dfs(nw, depth + 1, i);
    }
  }
}

int main() {
  cin >> N >> M >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  vector<int> v;
  dfs(v, 0, 1);
  cout << ans;
}
Problem p02695 (C++)

Buggy submission (status: 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(q, 1);
  A[0] = 1;
  dfs(0, A, 1);
  cout << ans << endl;
}

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(n, 0);
  A[0] = 1;
  dfs(0, A, 1);
  cout << ans << endl;
}
Problem p02695 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define repe(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep_bit(n) for (int bit = 0; bit < (1 << n); ++bit)
#define ll long long
using namespace std;
typedef vector<vector<int>> Graph;

int ans = 0;
int N, M, Q;
vector<int> a, b, c, d;
void dfs(vector<int> vec) {
  int now = 0;
  if (vec.size() == N + 1) {
    rep(i, Q) {
      if (vec.at(b.at(i)) - vec.at(a.at(i)) == c.at(i))
        now += d.at(i);
    }
    ans = max(ans, now);
  }
  vec.push_back(vec.back());
  while (vec.back() <= M) {
    dfs(vec);
    vec.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);
  vector<int> vec(1, 1);
  dfs(vec);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define repe(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep_bit(n) for (int bit = 0; bit < (1 << n); ++bit)
#define ll long long
using namespace std;
typedef vector<vector<int>> Graph;

int ans = 0;
int N, M, Q;
vector<int> a, b, c, d;
void dfs(vector<int> vec) {
  int now = 0;
  if (vec.size() == N + 1) {
    rep(i, Q) {
      if (vec.at(b.at(i)) - vec.at(a.at(i)) == c.at(i))
        now += d.at(i);
    }
    ans = max(ans, now);
    return;
  }
  vec.push_back(vec.back());
  while (vec.back() <= M) {
    dfs(vec);
    vec.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);
  vector<int> vec(1, 1);
  dfs(vec);
  cout << ans << endl;
}
Problem p02695 (C++)

Buggy submission (status: 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;
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;
}

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;
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;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

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

void calc(vector<int> &A) {
  long long cur = 0;
  for (int i = 0; i < q; i++) {
    if (A[a[i] - 1] + c[i] == A[b[i] - 1])
      cur += d[i];
  }
  ans = max(ans, cur);
}

void rec(vector<int> &A, int idx) {
  if (idx < n) {
    for (int nx = A[idx - 1]; nx <= m; nx++) {
      vector<int> B(A.begin(), A.end());
      B[idx] = nx;
      rec(B, idx + 1);
    }
  } else {
    calc(A);
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];

  vector<int> A(n, 1);
  rec(A, 1);
  cout << ans << endl;

  return 0;
}

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

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

void calc(vector<int> &A) {
  long long cur = 0;
  for (int i = 0; i < q; i++) {
    if (A[a[i] - 1] + c[i] == A[b[i] - 1])
      cur += d[i];
  }
  ans = max(ans, cur);
}

void rec(vector<int> &A, int idx) {
  if (idx < n) {
    for (int nx = A[idx - 1]; nx <= m; nx++) {
      vector<int> B(A.begin(), A.end());
      B[idx] = nx;
      rec(B, idx + 1);
    }
  } else {
    calc(A);
  }
}

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];

  vector<int> A(n, 1);
  rec(A, 1);
  cout << ans << endl;

  return 0;
}
Problem p02695 (C++)

Buggy submission (status: 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);
  }
  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;
}


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);
    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;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> a, b, c, d;
int n, m, q;
int ans;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int sum = 0;
    for (int i = 0; i < q; i++) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        sum += d[i];
      }
    }
    ans = max(ans, sum);
    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[i] >> b[i] >> c[i] >> d[i];
  }
  vector<int> A(1, 1);
  dfs(A);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> a, b, c, d;
int n, m, q;
int ans;

void dfs(vector<int> A) {
  if (A.size() == n + 1) {
    int sum = 0;
    for (int i = 0; i < q; i++) {
      if (A[b[i]] - A[a[i]] == c[i]) {
        sum += d[i];
      }
    }
    ans = max(ans, sum);
    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[i] >> b[i] >> c[i] >> d[i];
  }
  vector<int> A(1, 1);
  dfs(A);
  cout << ans << endl;
  return 0;
}
Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

int A[10], a[10], b[10], c[10], d[10];

int ans;

int n, m, q;

void dfs(int k) {

  if (k == n) {
    int sum = 0;
    for (int i = 0; i < q; ++i) {
      if (A[b[i]] - A[a[i]] == c[i])
        sum += d[i];
    }
    ans = std::max(ans, sum);
    return;
  }

  for (int i = A[k - 1]; i <= m; ++i) {
    A[k] = i;
    dfs(k + 1);
  }
}

int main() {

  A[0] = 1;

  std::cin >> n >> m >> q;

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

  dfs(1);

  std::cout << ans << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

int A[10], a[50], b[50], c[50], d[50];

int ans;

int n, m, q;

void dfs(int k) {

  if (k == n) {
    int sum = 0;
    for (int i = 0; i < q; ++i) {
      if (A[b[i]] - A[a[i]] == c[i])
        sum += d[i];
    }
    ans = std::max(ans, sum);
    return;
  }

  for (int i = A[k - 1]; i <= m; ++i) {
    A[k] = i;
    dfs(k + 1);
  }
}

int main() {

  A[0] = 1;

  std::cin >> n >> m >> q;

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

  dfs(1);

  std::cout << ans << std::endl;

  return 0;
}