prithivMLmods/Coder-Stat huggingface dataset:
Problem p02838 (Python)

Buggy submission (status: Time Limit Exceeded):
from collections import Counter
import sys

input = sys.stdin.readline


def main():
    mod = 10**9 + 7
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    a_dict = Counter(a)
    for i in range(n):
        a_dict[a[i]] -= 1
        for j in a_dict:
            ans += a_dict[j] * (a[i] ^ j)
    print(ans % mod)


if __name__ == "__main__":
    main()


Fixed submission:
n = int(input())
a = map(int, input().split())
mod = 10**9 + 7
p = [0] * 60
for a_ in a:
    for i in range(60):
        p[i] += (a_ >> i) & 1
ans = 0
for i in range(60):
    sn = (n - p[i]) * p[i]
    ans = (ans + (sn << i)) % mod
print(ans)
Problem p02838 (Python)

Buggy submission (status: Time Limit Exceeded):
import itertools

MOD = 10**9 + 7
N = int(input())
A = [int(i) for i in input().split()]


def main():
    ans = sum([a ^ b for (a, b) in itertools.combinations(A, 2)]) % MOD
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import itertools

MOD = 10**9 + 7
N = int(input())
A = [int(i) for i in input().split()]


def main():
    count_1 = [0] * 61
    for i in range(60):
        tmp = 0
        for j, a in enumerate(A):
            tmp += (a >> i) & 1
        count_1[i] += tmp
    ans = 0
    for i, c1 in enumerate(count_1):
        c0 = len(A) - c1
        ans += c0 * c1 * 2**i
    ans %= MOD
    print(ans)


if __name__ == "__main__":
    main()
Problem p02838 (C++)

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

#define int long long int
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);
#ifndef LOCAL
#define dbg(...) ;
#define endl '\n'
#endif

const int inf = 1e15;
const int MOD = 1e9 + 7;
const int N = 2e5 + 5;

int a[N], sum[N];

signed main() {
  IOS;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int ans = 0;
  for (int i = 0; i < 60; i++) {
    int o = 0, z = 0;
    for (int j = 1; j <= n; j++) {
      if (a[j] & (1ll << i))
        o++;
      else
        z++;
    }
    ans += ((o * z) % MOD * ((1ll << i) % MOD)) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}

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

#define int long long int
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL);
#ifndef LOCAL
#define dbg(...) ;
#define endl '\n'
#endif

const int inf = 1e15;
const int MOD = 1e9 + 7;
const int N = 3e5 + 5;

int a[N], sum[N];

signed main() {
  IOS;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int ans = 0;
  for (int i = 0; i < 60; i++) {
    int o = 0, z = 0;
    for (int j = 1; j <= n; j++) {
      if (a[j] & (1ll << i))
        o++;
      else
        z++;
    }
    ans += ((o * z) % MOD * ((1ll << i) % MOD)) % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p02838 (C++)

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

long a[300010], s[64][300010][2];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    for (int j = 0; j < 61; ++j) {
      int k = (a[i] >> j) & 1;
      if (k == 0) {
        s[j][i + 1][0] = s[j][i][0] + 1;
        s[j][i + 1][1] = s[j][i][1];
      } else {
        s[j][i + 1][0] = s[j][i][0];
        s[j][i + 1][1] = s[j][i][1] + 1;
      }
    }
  }

  const long mod = 1e9 + 7;
  long res = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 61; ++j) {
      long t = 0;
      int x = (a[i] >> j) & 1;
      t += s[j][n][!x] - s[j][i][!x];
      t %= mod;
      for (int k = 0; k < j; ++k)
        (t *= 2) %= mod;
      (res += t) %= mod;
    }
  }
  cout << res << endl;
}


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

long a[300010], s[64][300010][2];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    for (int j = 0; j < 61; ++j) {
      int k = (a[i] >> j) & 1;
      if (k == 0) {
        s[j][i + 1][0] = s[j][i][0] + 1;
        s[j][i + 1][1] = s[j][i][1];
      } else {
        s[j][i + 1][0] = s[j][i][0];
        s[j][i + 1][1] = s[j][i][1] + 1;
      }
    }
  }

  const long mod = 1e9 + 7;
  long res = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 61; ++j) {
      long t = 0;
      int x = (a[i] >> j) & 1;
      t += s[j][n][!x] - s[j][i][!x];
      t %= mod;
      (t *= (1L << j) % mod) %= mod;
      (res += t) %= mod;
    }
  }
  cout << res << endl;
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
int n;
int a[100001];
int cnt[201][2];
signed main() {
  scanf("%lld", &n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    for (int j = 0; j <= 60; j++)
      ans += 1ll * (1ll << j) % mod * cnt[j][((a[i] >> j) & 1) ^ 1] % mod,
          ans %= mod;
    for (int j = 0; j <= 60; j++)
      cnt[j][((a[i] >> j) & 1)]++;
  }
  printf("%lld", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
int n;
int a[1000001];
int cnt[201][2];
signed main() {
  scanf("%lld", &n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    for (int j = 0; j <= 60; j++)
      ans += 1ll * (1ll << j) % mod * cnt[j][((a[i] >> j) & 1) ^ 1] % mod,
          ans %= mod;
    for (int j = 0; j <= 60; j++)
      cnt[j][((a[i] >> j) & 1)]++;
  }
  printf("%lld", ans);
  return 0;
}
Problem p02838 (C++)

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

const long long MOD = 1000000007;

void solve(long long N, std::vector<long long> A) {
  const long long MOD = 1000000007;
  long long maxA = *max_element(A.begin(), A.end());
  long long k = floor(log2(maxA)) + 1;

  // cout << "k " << k << endl;

  long long res = 0;
  for (size_t j = 0; j < k; j++) {
    long long n_ones = 0;
    for (size_t i = 0; i < N; i++) {
      n_ones += (A[i] >> j) & (1);
    }
    long long res_k = n_ones * (N - n_ones);
    for (size_t l = 0; l < j; l++) {
      res_k = (2 * res_k) % MOD;
    }

    res += res_k;
  }

  cout << (res % MOD) << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}


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

const long long MOD = 1000000007;

void solve(long long N, std::vector<long long> A) {
  const long long MOD = 1000000007;
  long long maxA = *max_element(A.begin(), A.end());
  long long k = floor(log2(maxA)) + 1;

  // cout << "k " << k << endl;

  long long res = 0;
  for (size_t j = 0; j < 60; j++) {
    long long n_ones = 0;
    for (size_t i = 0; i < N; i++) {
      n_ones += (A[i] >> j) & (1);
    }
    long long res_k = n_ones * (N - n_ones);
    for (size_t l = 0; l < j; l++) {
      res_k = (2 * res_k) % MOD;
    }

    res += res_k;
  }

  cout << (res % MOD) << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
// IOI 2021

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p)                                                              \
  cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

////////////////////////////////////////////////////////////////////

const int N = 3e5 + 5, LOG = 6e1 + 1;

int A[LOG];

int main() {

  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    for (int j = 0; j < LOG; j++)
      A[j] += (x >> j) & 1;
  }
  int ans = 0;
  for (int i = 0; i < LOG; i++)
    ans = (ans + 1LL * A[i] * (n - A[i]) % MOD * (1LL << i) % MOD) % MOD;
  if (ans < 0)
    assert(0);
  cout << ans << endl;

  return 0;
}


Fixed submission:
// IOI 2021

#include <bits/stdc++.h>

using namespace std;

#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
#define debug(x) cerr << #x << ": " << x << endl
#define debugP(p)                                                              \
  cerr << #p << ": {" << p.first << ", " << p.second << '}' << endl
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

////////////////////////////////////////////////////////////////////

const int N = 3e5 + 5, LOG = 6e1 + 1;

int A[LOG];

int main() {

  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    ll x;
    cin >> x;
    for (int j = 0; j < LOG; j++)
      A[j] += (x >> j) & 1;
  }
  int ans = 0;
  for (int i = 0; i < LOG; i++)
    ans = (ans + (1LL * A[i] * (n - A[i]) % MOD) * ((1LL << i) % MOD) % MOD) %
          MOD;
  cout << ans << endl;

  return 0;
}
Problem p02838 (C++)

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

#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(a) int((a).size())
#define endl '\n'

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long ll;

const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;

ll v[maxn];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  int n;
  ll ans = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  for (int i = 0; i < 60; ++i) {
    ll cnt = 0;
    for (int j = 0; j < n; ++j) {
      if ((v[j] & (1LL << i)))
        cnt++;
    }
    ll one_bit = (cnt * (n - cnt)) % mod;
    ll to_dec = (((1LL << i) % mod) * one_bit) % mod;
    ans = (ans + to_dec) % mod;
  }
  cout << ans << endl;
  return 0;
}

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

#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define sz(a) int((a).size())
#define endl '\n'

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> ii;
typedef long long ll;

const int maxn = 3e5 + 10;
const int mod = 1e9 + 7;

ll v[maxn];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  int n;
  ll ans = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  for (int i = 0; i < 60; ++i) {
    ll cnt = 0;
    for (int j = 0; j < n; ++j) {
      if ((v[j] & (1LL << i)))
        cnt++;
    }
    ll one_bit = (cnt * (n - cnt)) % mod;
    ll to_dec = (((1LL << i) % mod) * one_bit) % mod;
    ans = (ans + to_dec) % mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 200100;
const ll MOD = 1e9 + 7;

int n;
ll arr[MAXN], ans;
ll dig[100];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &arr[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 61; j++)
      dig[j] += (bool)(arr[i] & (1ll << j));
  }
  for (int i = 0; i <= 61; i++)
    ans = (ans + (((dig[i] * (n - dig[i])) % MOD) * ((1ll << i) % MOD)) % MOD) %
          MOD;
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 300100;
const ll MOD = 1e9 + 7;

int n;
ll arr[MAXN], ans;
ll dig[100];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &arr[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 61; j++)
      dig[j] += (bool)(arr[i] & (1ll << j));
  }
  for (int i = 0; i <= 61; i++)
    ans = (ans + (((dig[i] * (n - dig[i])) % MOD) * ((1ll << i) % MOD)) % MOD) %
          MOD;
  printf("%lld\n", ans);
  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
long long sum = 0, a[3001], beki[61];
int A[300001][60] = {0}, B[300001][60] = {0};
int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    for (int j = 0; j < 60; j++) {
      A[i][j] = A[i - 1][j] + ((a[i] >> j) & 1);
      B[i][j] = B[i - 1][j] + 1 - ((a[i] >> j) & 1);
    }
  }
  beki[0] = 1;
  for (int i = 1; i <= 60; i++) {
    beki[i] = beki[i - 1] * 2;
    beki[i] %= 1000000007;
  }
  for (int i = 1; i <= N - 1; i++) {
    for (int j = 0; j < 60; j++) {
      if (((a[i] >> j) & 1) == 0)
        sum += ((A[N][j] - A[i][j]) * beki[j]) % 1000000007;
      else if (((a[i] >> j) & 1) > 0)
        sum += ((B[N][j] - B[i][j]) * beki[j]) % 1000000007;
      sum %= 1000000007;
    }
  }
  cout << sum;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
long long sum = 0, a[300001], beki[61];
int A[300001][60] = {0}, B[300001][60] = {0};
int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    for (int j = 0; j < 60; j++) {
      A[i][j] = A[i - 1][j] + ((a[i] >> j) & 1);
      B[i][j] = B[i - 1][j] + 1 - ((a[i] >> j) & 1);
    }
  }
  beki[0] = 1;
  for (int i = 1; i <= 60; i++) {
    beki[i] = beki[i - 1] * 2;
    beki[i] %= 1000000007;
  }
  for (int i = 1; i <= N - 1; i++) {
    for (int j = 0; j < 60; j++) {
      if (((a[i] >> j) & 1) == 0)
        sum += ((A[N][j] - A[i][j]) * beki[j]) % 1000000007;
      else if (((a[i] >> j) & 1) > 0)
        sum += ((B[N][j] - B[i][j]) * beki[j]) % 1000000007;
      sum %= 1000000007;
    }
  }
  cout << sum;
  return 0;
}
Problem p02838 (C++)

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define ALL(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e15;
const ll MOD = 1e9 + 7;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll mx = -1;
  REP(i, n) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }

  ll cnt = 0;
  while (mx > 0) {
    cnt++;
    mx /= 2;
  }

  vector<map<ll, ll>> mp(70);

  REP(j, n) {
    ll tmp = 0;
    REP(i, cnt) {
      ll b = a[j] % 2;
      mp[i][b]++;
      a[j] /= 2;
      tmp++;
    }
  }

  ll ans = 0;
  ll next = 0;
  ll i = 0;
  while (true) {
    ll c = mp[i][0] * mp[i][1];

    ans += mod_pow(2, i, MOD) * ((next + c) % 2);

    ans %= MOD;
    next = (next + c) / 2;
    if (next == 0)
      break;
    i++;
  }
  cout << ans << endl;
  return 0;
}

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define ALL(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e15;
const ll MOD = 1e9 + 7;

ll mod_pow(ll x, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll mx = -1;
  REP(i, n) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }

  ll cnt = 0;
  while (mx > 0) {
    cnt++;
    mx /= 2;
  }

  vector<map<ll, ll>> mp(1000);

  REP(j, n) {
    ll tmp = 0;
    REP(i, cnt) {
      ll b = a[j] % 2;
      mp[i][b]++;
      a[j] /= 2;
      tmp++;
    }
  }

  ll ans = 0;
  ll next = 0;
  ll i = 0;
  while (true) {
    ll c = mp[i][0] * mp[i][1];

    ans += mod_pow(2, i, MOD) * ((next + c) % 2);

    ans %= MOD;
    next = (next + c) / 2;
    if (next == 0)
      break;
    i++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02838 (C++)

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

#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 namespace std;

long long mod = 1000000000 + 7;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  rep(i, N) cin >> A[i];

  long long ans = 0;
  long long twoPow = 1;
  rep(i, 60) {
    long long count0 = 0;
    long long count1 = 0;
    rep(j, N) {
      if (A[j] & (1LL << i))
        count1++;
      else
        count0++;
    }
    ans += (count0 * count1) % mod * twoPow % mod;
    ans %= ans % mod;
    twoPow = (twoPow * 2) % mod;
  }
  cout << ans;
}

Fixed submission:
#include "bits/stdc++.h"

#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 namespace std;

long long mod = 1000000000 + 7;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  rep(i, N) cin >> A[i];

  long long ans = 0;
  long long twoPow = 1;
  rep(i, 60) {
    long long count0 = 0;
    long long count1 = 0;
    rep(j, N) {
      if (A[j] & (1LL << i))
        count1++;
      else
        count0++;
    }
    ans += (count0 * count1) % mod * twoPow % mod;
    ans %= mod;
    twoPow = (twoPow * 2) % mod;
  }
  cout << ans;
}
Problem p02838 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define all(x) x.begin(), x.end()

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<vector<ll>> sum(n + 1, vector<ll>(60, 0));
  rep(i, n) rep(j, 60) {
    if ((a[i] >> j) & 1)
      ++sum[i + 1][j];
    sum[i + 1][j] += sum[i][j];
  }
  /*rep(i, n)rep(j, 60)
  {
          sum[i + 1][j] += sum[i][j];
  }*/
  ll MOD = 1e9 + 7;
  ll ans = 0;
  rep(i, n) rep(j, 60) {
    ll num = j;
    ll tmp = powl(2LL, num);
    tmp %= MOD;
    ll x;
    if ((a[i] >> j) & 1)
      x = n - i - 1 - (sum[n][j] - sum[i + 1][j]);
    else
      x = sum[n][j] - sum[i + 1][j];
    ans += x * tmp;
    ans %= MOD;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
// #define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define all(x) x.begin(), x.end()

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<vector<ll>> sum(n + 1, vector<ll>(60, 0));
  rep(i, n) rep(j, 60) {
    if ((a[i] >> j) & 1)
      ++sum[i + 1][j];
    sum[i + 1][j] += sum[i][j];
  }
  ll MOD = 1e9 + 7;
  ll ans = 0;
  rep(i, n) rep(j, 60) {
    ll num = j;
    ll tmp = powl(2LL, num);
    tmp %= MOD;
    ll x;
    if ((a[i] >> j) & 1)
      x = n - i - 1 - (sum[n][j] - sum[i + 1][j]);
    else
      x = sum[n][j] - sum[i + 1][j];
    ans += x * tmp;
    ans %= MOD;
  }
  cout << ans << endl;
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOREACH(x, a) for (auto x : a)

/* exchange */
#define CHMIN(a, b) (a) = min((ll)(a), (ll)(b))
#define CHMAX(a, b) (a) = max((ll)(a), (ll)(b))

/* function */
#define IN(x) cin >> x
#define DEBUG(x) cerr << (x) << " "
#define LN() cerr << "\n"
#define PRINT(x) cout << (x) << endl
#define BR cout << endl

/* const */
const int ARRAY = 100005;
const int INF = 1001001001;          // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll N = 0;
ll A[30005];
ll di[60];
ll t;
ll ret = 0;

void input() {
  IN(N);
  REP(i, N) {
    IN(t);
    A[i] = t;
  }
}

void solve() {
  REP(j, 60) {
    ll x = 0;
    REP(i, N) { x += (A[i] >> j) & 1; }
    // DEBUG(x);
    ll p = x * (N - x) % MOD;
    REP(k, j) p = p * 2 % MOD;
    ret += p;
    ret %= MOD;
  }
  PRINT(ret);
}

int main(void) {
  input();
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORR(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOREACH(x, a) for (auto x : a)

/* exchange */
#define CHMIN(a, b) (a) = min((ll)(a), (ll)(b))
#define CHMAX(a, b) (a) = max((ll)(a), (ll)(b))

/* function */
#define IN(x) cin >> x
#define DEBUG(x) cerr << (x) << " "
#define LN() cerr << "\n"
#define PRINT(x) cout << (x) << endl
#define BR cout << endl

/* const */
const int ARRAY = 100005;
const int INF = 1001001001;          // 10^9
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;

ll N = 0;
ll A[300005];
ll di[60];
ll t;
ll ret = 0;

void input() {
  IN(N);
  REP(i, N) {
    IN(t);
    A[i] = t;
  }
}

void solve() {
  REP(j, 60) {
    ll x = 0;
    REP(i, N) { x += (A[i] >> j) & 1; }
    // DEBUG(x);
    ll p = x * (N - x) % MOD;
    REP(k, j) p = p * 2 % MOD;
    ret += p;
    ret %= MOD;
  }
  PRINT(ret);
}

int main(void) {
  input();
  solve();
}
Problem p02838 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define MOD 1000000007
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

int main() {
  int n;
  cin >> n;

  ll A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];

  int sum = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {

      sum += (A[i] ^ A[j]) % MOD;
      sum %= MOD;
    }
  }

  cout << sum << endl;

  return 0;
}

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

using namespace std;
typedef long long ll;
#define MOD 1000000007
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

int main() {
  int n;
  cin >> n;

  ll A[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];

  ll sum = 0;
  ll cnt[61];
  for (ll b = 0; b < 61; b++)
    cnt[b] = 0;
  for (ll i = 0; i < n; i++)
    for (ll b = 0; b < 61; b++)
      if (A[i] & (1ull << b))
        cnt[b]++;
  for (ll b = 0; b < 61; b++) {
    ll m = ((cnt[b] * (n - cnt[b]) % MOD) * ((1ull << b) % MOD)) % MOD;
    sum += m;
    sum %= MOD;
  }

  cout << sum << endl;

  return 0;
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
ll a[1 << 18];
ll sum[1 << 18][65];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ll tmp = a[i];
    for (int j = 0; j < 60; j++) {
      sum[i][j] = tmp & 1;
      tmp >>= 1;
    }
  }
  for (int i = n; i >= 0; i--) {
    for (int j = 0; j < 60; j++) {
      sum[i][j] += sum[i + 1][j];
    }
  }
  ll ans = 0;
  ll mod = 1e9 + 7;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 60; j++) {
      ans += (1LL << j) % mod *
             ((a[i] >> j) & 1 ? n - i - 1 - sum[i + 1][j] : sum[i + 1][j]) %
             mod;
      ans %= mod;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
ll a[1 << 19];
ll sum[1 << 19][65];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ll tmp = a[i];
    for (int j = 0; j < 60; j++) {
      sum[i][j] = tmp & 1;
      tmp >>= 1;
    }
  }
  for (int i = n; i >= 0; i--) {
    for (int j = 0; j < 60; j++) {
      sum[i][j] += sum[i + 1][j];
    }
  }
  ll ans = 0;
  ll mod = 1e9 + 7;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 60; j++) {
      ans += (1LL << j) % mod *
             ((a[i] >> j) & 1 ? n - i - 1 - sum[i + 1][j] : sum[i + 1][j]) %
             mod;
      ans %= mod;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02838 (C++)

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

using i64 = long long;
using u64 = unsigned long long;

#define dbg(x) cerr << "[" #x << " : " << x << "]\n"

const int mod = (int)1e9 + 7;

int main() {
  int n;
  scanf("%d", &n);
  i64 a[400000];
  vector<int> cnt(65, 0);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    for (i64 j = 0; j < 62; j++) {
      cnt[j] += ((a[i] & (1LL << j)) > 0);
    }
  }
  vector<i64> powers(65, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 62; j++) {
      if (a[i] & (1LL << j)) {
        cnt[j]--;
        powers[j] += n - i - 1 - cnt[j];
      } else {
        powers[j] += cnt[j];
      }
    }
  }
  i64 ans = 0;
  for (int i = 0; i < 61; i++) {
    i64 p = (i64)pow(2, i);
    p %= mod;
    ans = ans + (p * (powers[i]) % mod) % mod;
    assert(ans >= 0);
    ans %= mod;
  }
  printf("%lld\n", ans);
}


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

using i64 = long long;
using u64 = unsigned long long;

#define dbg(x) cerr << "[" #x << " : " << x << "]\n"

const int mod = (int)1e9 + 7;

int main() {
  int n;
  scanf("%d", &n);
  i64 a[400000];
  vector<int> cnt(65, 0);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    for (i64 j = 0; j < 62; j++) {
      cnt[j] += ((a[i] & (1LL << j)) > 0);
    }
  }
  vector<i64> powers(65, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 62; j++) {
      if (a[i] & (1LL << j)) {
        cnt[j]--;
        powers[j] += n - i - 1 - cnt[j];
      } else {
        powers[j] += cnt[j];
      }
    }
  }
  i64 ans = 0;
  for (int i = 0; i < 61; i++) {
    i64 tmp = powers[i];
    for (int j = 1; j <= i; j++) {
      tmp *= 2;
      tmp %= mod;
    }
    ans += tmp;
    ans %= mod;
  }
  printf("%lld\n", ans);
}
Problem p02838 (C++)

Buggy submission (status: Runtime Error):
///                                                     ببِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم
#include <bits/stdc++.h>
#define off exit(0)
#define cn continue
#define rt return
#define sc scanf
#define pr printf
#define pb push_back
#define pf push_front
#define p_f pop_front
#define p_b pop_back
#define ff first
#define ss second
#define vec vector
#define gcd __gcd
#define pi acos(-1)
#define ll long long
#define ld long double
#define N 2256
#define M 1000000007
#define I 1000000000000000000
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(a, b, c, d) for (int a = b; a <= c; a += d)
#define rvv(a, b, c, d) for (int a = b; a >= c; a -= d)
#define en printf("\n")
#define sqr(a) a *a
#define all(c) c.begin(), c.end()
#define srt(c) sort(all(c))
#define rev(c) reverse(all(c))
#define srtr(c) sort(c.rbegin(), c.rend())
#define numcounts __builtin_popcount
#define mxn(x, y, z) max(x, min(y, z))
#define mnx(x, y, z) min(x, max(y, z))
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
using namespace std;
ll ttt = 1;
ll n, a[N], ans, cnt;
void solve() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < 61; i++) {
    cnt = 0;
    for (ll j = 0; j < n; j++)
      if (a[j] & (1LL << i))
        cnt++;
    ans += (((cnt * (n - cnt)) % M) * ((1LL << i) % M)) % M;
    ans %= M;
  }
  cout << ans;
}
int main() {
  // sc( "%I64d", &ttt );
  for (int i = 0; i < ttt; i++)
    solve();
}


Fixed submission:
///                                                     ببِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم
#include <bits/stdc++.h>
#define off exit(0)
#define cn continue
#define rt return
#define sc scanf
#define pr printf
#define pb push_back
#define pf push_front
#define p_f pop_front
#define p_b pop_back
#define ff first
#define ss second
#define vec vector
#define gcd __gcd
#define pi acos(-1)
#define ll long long
#define ld long double
#define N 300256
#define M 1000000007
#define I 1000000000000000000
#define pdd pair<ld, ld>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(a, b, c, d) for (int a = b; a <= c; a += d)
#define rvv(a, b, c, d) for (int a = b; a >= c; a -= d)
#define en printf("\n")
#define sqr(a) a *a
#define all(c) c.begin(), c.end()
#define srt(c) sort(all(c))
#define rev(c) reverse(all(c))
#define srtr(c) sort(c.rbegin(), c.rend())
#define numcounts __builtin_popcount
#define mxn(x, y, z) max(x, min(y, z))
#define mnx(x, y, z) min(x, max(y, z))
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
using namespace std;
ll ttt = 1;
ll n, a[N], ans, cnt;
void solve() {
  cin >> n;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < 61; i++) {
    cnt = 0;
    for (ll j = 0; j < n; j++)
      if (a[j] & (1LL << i))
        cnt++;
    ans += (((cnt * (n - cnt)) % M) * ((1LL << i) % M)) % M;
    ans %= M;
  }
  cout << ans;
}
int main() {
  // sc( "%I64d", &ttt );
  for (int i = 0; i < ttt; i++)
    solve();
}
Problem p02839 (C++)

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

#define li long long int
#define rep(i, to) for (li i = 0; i < ((li)(to)); i++)
#define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++)

#define MOD 1000000007

li h, w;
li a[88][88];
li b[88][88];
bitset<80 * 160 * 2 + 1> dp[88][88];

int main(void) {
  li offset = 80 * 160;
  cin >> h >> w;
  rep(i, h) {
    rep(j, w) { cin >> a[i][j]; }
  }
  rep(i, h) {
    rep(j, w) { cin >> b[i][j]; }
  }

  li lim = 0;
  rep(i, h) {
    rep(j, w) {
      li geta = min(a[i][j], b[i][j]);
      a[i][j] -= geta;
      b[i][j] -= geta;
      lim += a[i][j];
      lim += b[i][j];
    }
  }
  dp[0][0][a[0][0] - b[0][0] + offset] = true;
  dp[0][0][-a[0][0] + b[0][0] + offset] = true;

  // first line
  repp(i, 1, w) {
    li val = abs(a[0][i] - b[0][i]);
    dp[0][i] |= dp[0][i - 1] << val;
    dp[0][i] |= dp[0][i - 1] >> val;
  }
  repp(i, 1, h) {
    rep(j, lim + 1) {
      li val = abs(a[i][0] - b[i][0]);
      dp[i][0] |= dp[i - 1][0] << val;
      dp[i][0] |= dp[i - 1][0] >> val;
    }
    repp(j, 1, w) {
      li now = abs(a[i][j] - b[i][j]);
      dp[i][j] |= dp[i - 1][j] << now;
      dp[i][j] |= dp[i - 1][j] >> now;
      dp[i][j] |= dp[i][j - 1] << now;
      dp[i][j] |= dp[i][j - 1] >> now;
    }
  }
  li res = 1 << 30;
  rep(i, 80 * 160 * 2 + 1) {
    if (dp[h - 1][w - 1][i]) {
      res = min(res, abs(offset - i));
    }
  }
  cout << res << endl;

  return 0;
}

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

#define li long long int
#define rep(i, to) for (li i = 0; i < ((li)(to)); i++)
#define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++)

#define MOD 1000000007

li h, w;
li a[88][88];
li b[88][88];
bitset<80 * 160 * 2 + 1> dp[88][88];

int main(void) {
  li offset = 80 * 160;
  cin >> h >> w;
  rep(i, h) {
    rep(j, w) { cin >> a[i][j]; }
  }
  rep(i, h) {
    rep(j, w) { cin >> b[i][j]; }
  }

  li lim = 0;
  rep(i, h) {
    rep(j, w) {
      li geta = min(a[i][j], b[i][j]);
      a[i][j] -= geta;
      b[i][j] -= geta;
      lim += a[i][j];
      lim += b[i][j];
    }
  }
  dp[0][0][a[0][0] - b[0][0] + offset] = true;
  dp[0][0][-a[0][0] + b[0][0] + offset] = true;

  // first line
  repp(i, 1, w) {
    li val = abs(a[0][i] - b[0][i]);
    dp[0][i] |= dp[0][i - 1] << val;
    dp[0][i] |= dp[0][i - 1] >> val;
  }
  repp(i, 1, h) {
    {
      li val = abs(a[i][0] - b[i][0]);
      dp[i][0] |= dp[i - 1][0] << val;
      dp[i][0] |= dp[i - 1][0] >> val;
    }
    repp(j, 1, w) {
      li now = abs(a[i][j] - b[i][j]);
      dp[i][j] |= dp[i - 1][j] << now;
      dp[i][j] |= dp[i - 1][j] >> now;
      dp[i][j] |= dp[i][j - 1] << now;
      dp[i][j] |= dp[i][j - 1] >> now;
    }
  }
  li res = 1 << 30;
  rep(i, 80 * 160 * 2 + 1) {
    if (dp[h - 1][w - 1][i]) {
      res = min(res, abs(offset - i));
    }
  }
  cout << res << endl;

  return 0;
}
Problem p02839 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int K = 15000;
const int N = 80;

int h, w;
int a[N][N], b[N][N];
bool dp[N][N][2 * K];

int put(int x) { return x + K; }

int get(int x) { return x - K; }

int main() {
  scanf("%d%d", &h, &w);
  FOR(i, h) FOR(j, w) scanf("%d", &a[i][j]);
  FOR(i, h) FOR(j, w) scanf("%d", &b[i][j]);

  dp[0][0][put(a[0][0] - b[0][0])] = dp[0][0][put(b[0][0] - a[0][0])] = true;
  FOR(i, h) FOR(j, w) {
    if (!i && !j)
      continue;
    int raz = a[i][j] - b[i][j];
    FOR(di, 2) FOR(dj, 2) {
      if (di == dj)
        continue;
      FOR(u, (2 * K)) {
        if (!dp[i - di][j - dj][u])
          continue;
        dp[i][j][u + raz] = dp[i][j][u - raz] = true;
      }
    }
  }

  int mini = 1e9;

  FOR(u, (2 * K)) {
    if (!dp[h - 1][w - 1][u])
      continue;
    mini = min(mini, abs(get(u)));
  }

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const int K = 15000;
const int N = 80;

int h, w;
int a[N][N], b[N][N];
bool dp[N][N][2 * K];

int put(int x) { return x + K; }

int get(int x) { return x - K; }

int main() {
  scanf("%d%d", &h, &w);
  FOR(i, h) FOR(j, w) scanf("%d", &a[i][j]);
  FOR(i, h) FOR(j, w) scanf("%d", &b[i][j]);

  dp[0][0][put(a[0][0] - b[0][0])] = dp[0][0][put(b[0][0] - a[0][0])] = true;
  FOR(i, h) FOR(j, w) {
    if (!i && !j)
      continue;
    int raz = a[i][j] - b[i][j];
    FOR(di, 2) FOR(dj, 2) {
      if (di == dj)
        continue;
      if (i - di < 0 || j - dj < 0)
        continue;
      FOR(u, (2 * K)) {
        if (!dp[i - di][j - dj][u])
          continue;
        dp[i][j][u + raz] = dp[i][j][u - raz] = true;
      }
    }
  }

  int mini = 1e9;

  FOR(u, (2 * K)) {
    if (!dp[h - 1][w - 1][u])
      continue;
    mini = min(mini, abs(get(u)));
  }

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

  return 0;
}
Problem p02839 (C++)

Buggy submission (status: 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++)
using namespace std;
long long INF = 1000000000000000000;
long long mod = 1000000007;
using namespace std;

int a[85][85];
int b[85][85];

int main() {

  int h, w;
  cin >> h >> w;

  rep2(i, h) {
    rep2(k, w) { cin >> a[i][k]; }
  }

  rep2(i, h) {
    rep2(k, w) { cin >> b[i][k]; }
  }
  long long size = 2 * 80 * 160 + 1;
  bool dp[size][h + 1][h + 1];

  rep(i, size) rep2(y, h) rep2(x, w) dp[i][y][x] = false;

  dp[abs(a[1][1] - b[1][1])][1][1] = true;
  rep2(y, h) {
    rep2(x, w) {

      for (int i = 0; i < size; i++) {
        if (!dp[i][y][x])
          continue;

        if (x + 1 <= w) {
          int p = abs(a[y][x + 1] - b[y][x + 1]);
          dp[abs(i - p)][y][x + 1] = true;
          dp[i + p][y][x + 1] = true;
        }
        if (y + 1 <= h) {
          int p = abs(a[y + 1][x] - b[y + 1][x]);
          dp[abs(i - p)][y + 1][x] = true;
          dp[i + p][y + 1][x] = true;
        }
      }
    }
  }

  for (int i = 0; i < size; i++) {
    if (dp[i][h][w]) {
      cout << i << endl;
      break;
    }
  }

  return 0;
}
/*

*/


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++)
using namespace std;
long long INF = 1000000000000000000;
long long mod = 1000000007;
using namespace std;

int a[85][85];
int b[85][85];

int main() {

  int h, w;
  cin >> h >> w;

  rep2(i, h) {
    rep2(k, w) { cin >> a[i][k]; }
  }

  rep2(i, h) {
    rep2(k, w) { cin >> b[i][k]; }
  }
  long long size = 2 * 80 * 160 + 1;
  bool dp[size][h + 1][w + 1];

  rep(i, size) rep2(y, h) rep2(x, w) dp[i][y][x] = false;

  dp[abs(a[1][1] - b[1][1])][1][1] = true;
  rep2(y, h) {
    rep2(x, w) {

      for (int i = 0; i < size; i++) {
        if (!dp[i][y][x])
          continue;

        if (x + 1 <= w) {
          int p = abs(a[y][x + 1] - b[y][x + 1]);
          dp[abs(i - p)][y][x + 1] = true;
          dp[i + p][y][x + 1] = true;
        }
        if (y + 1 <= h) {
          int p = abs(a[y + 1][x] - b[y + 1][x]);
          dp[abs(i - p)][y + 1][x] = true;
          dp[i + p][y + 1][x] = true;
        }
      }
    }
  }

  for (int i = 0; i < size; i++) {
    if (dp[i][h][w]) {
      cout << i << endl;
      break;
    }
  }

  return 0;
}
/*

*/
Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 10000000000000000

int main() {

  int H, W;
  cin >> H >> W;

  vector<vector<int>> A(H, vector<int>(W));
  vector<vector<int>> B(H, vector<int>(W));

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> B[i][j];
    }
  }

  vector<vector<bitset<14000>>> flag(H, vector<bitset<14000>>(W));

  flag[0][0][7000 + A[0][0] - B[0][0]] = true;
  flag[0][0][7000 - A[0][0] + B[0][0]] = true;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = 0; k < 14000; k++) {
        if (flag[i][j][k] == false)
          continue;
        if (i != H - 1) {
          int a = A[i + 1][j] - B[i + 1][j];
          int b = -a;
          if (a > 0) {
            flag[i + 1][j] |= (flag[i][j] << a);
          } else {
            flag[i + 1][j] |= (flag[i][j] >> (-a));
          }
          if (b > 0) {
            flag[i + 1][j] |= (flag[i][j] << b);
          } else {
            flag[i + 1][j] |= (flag[i][j] >> (-b));
          }
        }
        if (j != W - 1) {
          int a = A[i][j + 1] - B[i][j + 1];
          int b = -a;
          if (a > 0) {
            flag[i][j + 1] |= (flag[i][j] << a);
          } else {
            flag[i][j + 1] |= (flag[i][j] >> (-a));
          }
          if (b > 0) {
            flag[i][j + 1] |= (flag[i][j] << b);
          } else {
            flag[i][j + 1] |= (flag[i][j] >> (-b));
          }
        }
      }
    }
  }
  int ans = 10000000;
  for (int i = 0; i < 14000; i++) {
    if (flag[H - 1][W - 1][i]) {
      ans = min(ans, abs(7000 - i));
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x + modulo)) % modulo)
#define Inf 10000000000000000

int main() {

  int H, W;
  cin >> H >> W;

  vector<vector<int>> A(H, vector<int>(W));
  vector<vector<int>> B(H, vector<int>(W));

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> A[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> B[i][j];
    }
  }

  vector<vector<bitset<14000>>> flag(H, vector<bitset<14000>>(W));

  flag[0][0][7000 + A[0][0] - B[0][0]] = true;
  flag[0][0][7000 - A[0][0] + B[0][0]] = true;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (i != H - 1) {
        int a = abs(A[i + 1][j] - B[i + 1][j]);
        flag[i + 1][j] |= (flag[i][j] << a);
        flag[i + 1][j] |= (flag[i][j] >> a);
      }
      if (j != W - 1) {
        int a = abs(A[i][j + 1] - B[i][j + 1]);
        flag[i][j + 1] |= (flag[i][j] << a);
        flag[i][j + 1] |= (flag[i][j] >> a);
      }
    }
  }
  int ans = 10000000;
  for (int i = 0; i < 14000; i++) {
    if (flag[H - 1][W - 1][i]) {
      ans = min(ans, abs(7000 - i));
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02839 (C++)

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

using namespace std;

int n, m;
int a[101][101], b[101][101];
bitset<20001> f[81][81];

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> b[i][j];
  f[1][1].set(10000);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int v1 = a[i][j] - b[i][j];
      int v2 = b[i][j] - a[i][j];
      if (v1 >= 0) {
        f[i + 1][j] |= (f[i][j] << v1);
        f[i][j + 1] |= (f[i][j] << v1);
      } else {
        v1 = -v1;
        f[i + 1][j] |= (f[i][j] >> v1);
        f[i][j + 1] |= (f[i][j] >> v1);
      }
      if (v2 >= 0) {
        f[i + 1][j] |= (f[i][j] << v2);
        f[i][j + 1] |= (f[i][j] << v2);
      } else {
        v2 = -v2;
        f[i + 1][j] |= (f[i][j] >> v2);
        f[i][j + 1] |= (f[i][j] >> v2);
      }
    }
  int ans = 1e9;
  for (int i = 0; i <= 20000; i++)
    if (f[n][m].test(i)) {
      int v1 = i + a[n][m] - b[n][m];
      int v2 = i + b[n][m] - a[n][m];
      ans = min(ans, min(abs(v1 - 10000), abs(v2 - 10000)));
    }
  cout << ans;
}

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

using namespace std;

int n, m;
int a[101][101], b[101][101];
bitset<20001> f[101][101];

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> b[i][j];
  f[1][1].set(10000);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      int v1 = a[i][j] - b[i][j];
      int v2 = b[i][j] - a[i][j];
      if (v1 >= 0) {
        f[i + 1][j] |= (f[i][j] << v1);
        f[i][j + 1] |= (f[i][j] << v1);
      } else {
        v1 = -v1;
        f[i + 1][j] |= (f[i][j] >> v1);
        f[i][j + 1] |= (f[i][j] >> v1);
      }
      if (v2 >= 0) {
        f[i + 1][j] |= (f[i][j] << v2);
        f[i][j + 1] |= (f[i][j] << v2);
      } else {
        v2 = -v2;
        f[i + 1][j] |= (f[i][j] >> v2);
        f[i][j + 1] |= (f[i][j] >> v2);
      }
    }
  int ans = 1e9;
  for (int i = 0; i <= 20000; i++)
    if (f[n][m].test(i)) {
      int v1 = i + a[n][m] - b[n][m];
      int v2 = i + b[n][m] - a[n][m];
      ans = min(ans, min(abs(v1 - 10000), abs(v2 - 10000)));
    }
  cout << ans;
}
Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int z = 80 * 80 * 80 + 2;

bool dp[81][81][z];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;

  vector<vector<int>> v(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> v[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int a;
      cin >> a;
      v[i][j] = abs(a - v[i][j]);
    }
  }

  dp[0][0][v[0][0]] = true;
  for (int i = 1; i < m; i++) {
    for (int j = 0; j < z; j++) {
      int a = v[0][i];
      dp[0][i][j] = dp[0][i - 1][abs(j - a)] | dp[0][i - 1][j + a];
    }
  }

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < z; j++) {
      int a = v[i][0];
      dp[i][0][j] = dp[i - 1][0][abs(j - a)] | dp[i - 1][0][j + a];
    }
  }

  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      int a = v[i][j];
      for (int k = 0; k < z; k++) {
        dp[i][j][k] = dp[i - 1][j][abs(k - a)] | dp[i - 1][j][k + a] |
                      dp[i][j - 1][abs(k - a)] | dp[i][j - 1][k + a];
      }
    }
  }

  for (int i = 0; i < z; i++) {
    if (dp[n - 1][m - 1][i]) {
      cout << i << '\n';
      break;
    }
  }

  return 0;
}

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

using namespace std;

const int z = 80 * 80 * 2;

bool dp[81][81][z];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

  int n, m;
  cin >> n >> m;

  vector<vector<int>> v(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> v[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      int a;
      cin >> a;
      v[i][j] = abs(a - v[i][j]);
    }
  }

  dp[0][0][v[0][0]] = true;
  for (int i = 1; i < m; i++) {
    for (int j = 0; j < z; j++) {
      int a = v[0][i];
      dp[0][i][j] = dp[0][i - 1][abs(j - a)] | dp[0][i - 1][j + a];
    }
  }

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < z; j++) {
      int a = v[i][0];
      dp[i][0][j] = dp[i - 1][0][abs(j - a)] | dp[i - 1][0][j + a];
    }
  }

  for (int i = 1; i < n; i++) {
    for (int j = 1; j < m; j++) {
      int a = v[i][j];
      for (int k = 0; k < z; k++) {
        dp[i][j][k] = dp[i - 1][j][abs(k - a)] | dp[i - 1][j][k + a] |
                      dp[i][j - 1][abs(k - a)] | dp[i][j - 1][k + a];
      }
    }
  }

  for (int i = 0; i < z; i++) {
    if (dp[n - 1][m - 1][i]) {
      cout << i << '\n';
      break;
    }
  }

  return 0;
}