Problem p03584 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define MAX(a, b) ((ll)(a) > (ll)(b)) ? (ll)(a) : (ll)(b)
#define MIN(a, b) ((ll)(a) > (ll)(b)) ? (ll)(b) : (ll)(a)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vint;
typedef vector<string> vstr;
typedef pair<ll, ll> pint;
unsigned long long inf = (1LL << 62);
ll mod = 1000000007;
pint dx[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

ll min(ll a, ll b) {
  if (a < b)
    return a;

  return b;
}

ll N, K;
vector<ll> A, B;

vector<ll> a;
ll b[30];
int main() {

  cin >> N >> K;

  ll u, v;

  rep(i, N) {
    cin >> v >> u;

    A.push_back(v);
    B.push_back(u);
  }

  rep(i, 30) {
    if ((K & (ll)pow(2.0, i)) != 0) {
      a.push_back(i);
      b[i] = 1;
    }
  }
  ll ans = 0;

  reverse(a.begin(), a.end());

  ll finalans = 0;
  rep(i, a.size() + 1) {
    rep(j, N) {

      bool flag = true;

      if (i == a.size()) {
        for (int k = 0; k <= 30; k++) {
          if (b[k] == 0) {
            if (A[j] > K || ((A[j] & (ll)pow(2.0, k)) != 0)) {
              flag = false;
              break;
            }
          } else if (A[j] > K) {
            flag = false;
            break;
          }
        }

        if (flag == true) {
          ans += B[j];
        }

      }

      else if (i > 0) {
        for (int k = a[i]; k <= a[0]; k++) {

          if (b[k] == 0 || k == a[i]) {
            if (A[j] > K || ((A[j] & (ll)pow(2.0, k)) != 0)) {
              flag = false;
              break;
            }
          } else if (A[j] > K) {
            flag = false;
            break;
          }
        }

        if (flag == true) {
          ans += B[j];
        }

      }

      else if (i == 0 && a.size() != 0) {
        if (A[j] <= K && ((A[j] & (ll)pow(2.0, a[i])) == 0))
          ans += B[j];
      }
    }

    finalans = MAX(finalans, ans);
    ans = 0;
  }

  cout << finalans << endl;

  return 0;
}

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define MAX(a, b) ((ll)(a) > (ll)(b)) ? (ll)(a) : (ll)(b)
#define MIN(a, b) ((ll)(a) > (ll)(b)) ? (ll)(b) : (ll)(a)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vint;
typedef vector<string> vstr;
typedef pair<ll, ll> pint;
unsigned long long inf = (1LL << 62);
ll mod = 1000000007;
pint dx[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

ll min(ll a, ll b) {
  if (a < b)
    return a;

  return b;
}

ll N, K;
vector<ll> A, B;

vector<ll> a;
ll b[30];
int main() {

  cin >> N >> K;

  ll u, v;

  rep(i, N) {
    cin >> v >> u;

    A.push_back(v);
    B.push_back(u);
  }

  rep(i, 30) {
    if ((K & (ll)pow(2.0, i)) != 0) {
      a.push_back(i);
      b[i] = 1;
    }
  }
  ll ans = 0;

  reverse(a.begin(), a.end());

  ll finalans = 0;
  rep(i, a.size() + 1) {
    rep(j, N) {

      bool flag = true;

      if (i == a.size()) {
        for (int k = 0; k < 30; k++) {
          if (b[k] == 0) {
            if (A[j] > K || ((A[j] & (ll)pow(2.0, k)) != 0)) {
              flag = false;
              break;
            }
          } else if (A[j] > K) {
            flag = false;
            break;
          }
        }

        if (flag == true) {
          ans += B[j];
        }

      }

      else if (i > 0) {
        for (int k = a[i]; k <= a[0]; k++) {

          if (b[k] == 0 || k == a[i]) {
            if (A[j] > K || ((A[j] & (ll)pow(2.0, k)) != 0)) {
              flag = false;
              break;
            }
          } else if (A[j] > K) {
            flag = false;
            break;
          }
        }

        if (flag == true) {
          ans += B[j];
        }

      }

      else if (i == 0 && a.size() != 0) {
        if (A[j] <= K && ((A[j] & (ll)pow(2.0, a[i])) == 0))
          ans += B[j];
      }
    }

    finalans = MAX(finalans, ans);
    ans = 0;
  }

  cout << finalans << endl;

  return 0;
}