Problem p03215 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define INF 10000000000000009
#define INF 9223372036854775807
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define ORREP(i, n) for (int i = (n); i >= 1; --i)
#define ZREP(i, n) for (int i = 1; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define rollcall cout << "I'm Sucu." << endl;
#define YES(s) s ? cout << "YES" << endl : cout << "NO" << endl
#define Yes(s) s ? cout << "Yes" << endl : cout << "No" << endl
#define Taka(s) s ? cout << "Takahashi" << endl : cout << "Aoki" << endl
#define out(s, t, u) s ? cout << t << endl : cout << u << endl
#define int ll
#define Endl endl

signed main() {
  int N, K;
  int A[1004];
  int B[1004];
  int C[1004];
  cin >> N >> K;
  REP(i, N) {
    cin >> A[i];
    B[i + 1] = B[i] + A[i];
  }
  int c = 0;
  REP(i, N + 1) {
    REP(j, i) {
      C[c] = B[i] - B[j];
      c++;
    }
  }
  sort(C, C + c, greater<>());
  int L = (int)log2(C[K - 1]);
  vector<int> D;
  int E = (int)pow(2, L);

  REP(i, c) {
    if (E <= C[i]) {
      D.push_back(C[i]);
    } else {
      break;
    }
  }

  // cout << E << endl;

  REP(j, L) {
    E /= 2;
    int cou = 0;
    REP(i, D.size()) {
      if ((D[i] / E) % 2 == 1)
        cou++;
    }
    if (cou < K) {
      continue;
    }
    REP(i, D.size()) {
      if ((D[i] / E) % 2 == 0) {
        D.erase(D.begin() + i);
        i--;
      }
    }
  }
  int Ans = D[0];
  REP(i, D.size()) { Ans = (Ans & D[i]); }
  cout << Ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define INF 10000000000000009
#define INF 9223372036854775807
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define ORREP(i, n) for (int i = (n); i >= 1; --i)
#define ZREP(i, n) for (int i = 1; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define rollcall cout << "I'm Sucu." << endl;
#define YES(s) s ? cout << "YES" << endl : cout << "NO" << endl
#define Yes(s) s ? cout << "Yes" << endl : cout << "No" << endl
#define Taka(s) s ? cout << "Takahashi" << endl : cout << "Aoki" << endl
#define out(s, t, u) s ? cout << t << endl : cout << u << endl
#define int ll
#define Endl endl

signed main() {
  int N, K;
  int A[1004];
  int B[1004];
  int C[1114514];
  cin >> N >> K;
  REP(i, N) {
    cin >> A[i];
    B[i + 1] = B[i] + A[i];
  }
  int c = 0;
  REP(i, N + 1) {
    REP(j, i) {
      C[c] = B[i] - B[j];
      c++;
    }
  }
  sort(C, C + c, greater<>());
  int L = (int)log2(C[K - 1]);
  vector<int> D;
  int E = (int)pow(2, L);

  REP(i, c) {
    if (E <= C[i]) {
      D.push_back(C[i]);
    } else {
      break;
    }
  }

  // cout << E << endl;

  REP(j, L) {
    E /= 2;
    int cou = 0;
    REP(i, D.size()) {
      if ((D[i] / E) % 2 == 1)
        cou++;
    }
    if (cou < K) {
      continue;
    }
    REP(i, D.size()) {
      if ((D[i] / E) % 2 == 0) {
        D.erase(D.begin() + i);
        i--;
      }
    }
  }
  int Ans = D[0];
  REP(i, D.size()) { Ans = (Ans & D[i]); }
  cout << Ans << endl;

  return 0;
}