Problem p03584 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define INF 1e9
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
#define pii pair<int, int>
#define sz(s) s.size()
#define all(s) (s.begin(), s.end())
inline void Fail() {
  printf("0");
  exit(0);
}

const int maxn = 100005;
const int maxm = 31;
int n, k, cur;
ll ans;
int a[maxn], b[maxn];
vector<int> pos;

void solve(int i) {
  cur += (1 << pos[i]);
  ll now = 0;
  rep(j, n) {
    if (a[j] & (1 << pos[i]))
      a[j] ^= (1 << pos[i]);
    if (a[j] > (i == pos.size() - 1 ? 0 : (1 << pos[i + 1])))
      continue;
    now += b[j];
  }
  ans = max(ans, now);
}

int main() {
  scanf("%d%d", &n, &k);
  rep(i, n) scanf("%d%d", &a[i], &b[i]);
  for (int i = maxm - 1; i >= 0; i--)
    if (k & (1 << i))
      pos.push_back(i);
  ll now = 0;
  rep(i, n) {
    if (a[i] >= (1 << pos[0]))
      continue;
    now += b[i];
  }
  ans = now;
  rep(i, pos.size()) solve(i);
  printf("%lld", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <new>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define INF 1e9
#define rep(i, n) for (int(i) = 0; (i) < n; i++)
#define REP(i, n) for (int(i) = 1; (i) <= n; i++)
#define mk(a, b) make_pair(a, b)
#define fi first
#define se second
#define pii pair<int, int>
#define sz(s) s.size()
#define all(s) (s.begin(), s.end())
inline void Fail() {
  printf("0");
  exit(0);
}

const int maxn = 100005;
const int maxm = 31;
int n, k, cur;
ll ans;
int a[maxn], b[maxn];
vector<int> pos;

void solve(int i) {
  cur += (1 << pos[i]);
  ll now = 0;
  rep(j, n) {
    if (a[j] & (1 << pos[i]))
      a[j] ^= (1 << pos[i]);
    if (a[j] > (i == pos.size() - 1 ? 0 : (1 << pos[i + 1])))
      continue;
    now += b[j];
  }
  ans = max(ans, now);
}

int main() {
  scanf("%d%d", &n, &k);
  rep(i, n) scanf("%d%d", &a[i], &b[i]);
  if (!k) {
    rep(j, n) if (a[j] == 0) ans += b[j];
    printf("%lld", ans);
    return 0;
  }
  for (int i = maxm - 1; i >= 0; i--)
    if (k & (1 << i))
      pos.push_back(i);
  ll now = 0;
  rep(i, n) {
    if (a[i] >= (1 << pos[0]))
      continue;
    now += b[i];
  }
  ans = now;
  rep(i, pos.size()) solve(i);
  printf("%lld", ans);
  return 0;
}