Problem p03215 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long int
using namespace std;
template <typename T, typename U> using P = pair<T, U>;
template <typename T> using V = vector<T>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> auto &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  bool a = 1;
  for (auto e : v) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "]";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const pair<T, U> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <typename T> auto &operator<<(ostream &s, const set<T> &st) {
  s << "{";
  bool a = 1;
  for (auto e : st) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const map<T, U> &m) {
  s << "{";
  bool a = 1;
  for (auto e : m) {
    s << (a ? "" : " ") << e.first << ":" << e.second;
    a = 0;
  }
  s << "}";
  return s;
}
#define DUMP(x) cout << #x << " = " << (x) << endl
#define COUT(x)                                                                \
  cerr << #x << " = ";                                                         \
  cout << (x) << endl

struct edge {
  int to, cost;
};

const int INF = 1e18;
const int MOD = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  int K;
  cin >> K;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<int> sum(n + 1);
  for (int i = 0; i < n; i++) {
    sum[i + 1] = sum[i] + a[i];
  }

  vector<int> b;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      b.push_back(sum[j + 1] - sum[i]);
    }
  }

  int ans = 0;
  for (int i = 12; i >= 0; i--) {
    // DUMP(i);
    sort(b.rbegin(), b.rend());
    // DUMP(b);
    if (b[K - 1] >> i & 1) {
      ans |= 1LL << i;
      // DUMP(ans);
      vector<int> bn;
      for (int j = 0; j < b.size() && b[j] >> i & 1; j++) {
        bn.push_back(b[j]);
      }
      swap(bn, b);
    } else {
      for (int j = 0; b[j] >> i & 1; j++) {
        b[j] &= ~(1LL << i);
      }
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long int
using namespace std;
template <typename T, typename U> using P = pair<T, U>;
template <typename T> using V = vector<T>;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> auto &operator<<(ostream &s, const vector<T> &v) {
  s << "[";
  bool a = 1;
  for (auto e : v) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "]";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const pair<T, U> &p) {
  s << "(" << p.first << "," << p.second << ")";
  return s;
}
template <typename T> auto &operator<<(ostream &s, const set<T> &st) {
  s << "{";
  bool a = 1;
  for (auto e : st) {
    s << (a ? "" : " ") << e;
    a = 0;
  }
  s << "}";
  return s;
}
template <typename T, typename U>
auto &operator<<(ostream &s, const map<T, U> &m) {
  s << "{";
  bool a = 1;
  for (auto e : m) {
    s << (a ? "" : " ") << e.first << ":" << e.second;
    a = 0;
  }
  s << "}";
  return s;
}
#define DUMP(x) cout << #x << " = " << (x) << endl
#define COUT(x)                                                                \
  cerr << #x << " = ";                                                         \
  cout << (x) << endl

struct edge {
  int to, cost;
};

const int INF = 1e18;
const int MOD = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  int K;
  cin >> K;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  vector<int> sum(n + 1);
  for (int i = 0; i < n; i++) {
    sum[i + 1] = sum[i] + a[i];
  }

  vector<int> b;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      b.push_back(sum[j + 1] - sum[i]);
    }
  }

  int ans = 0;
  for (int i = 40; i >= 0; i--) {
    // DUMP(i);
    sort(b.rbegin(), b.rend());
    // DUMP(b);
    if (b[K - 1] >> i & 1) {
      ans |= 1LL << i;
      // DUMP(ans);
      vector<int> bn;
      for (int j = 0; j < b.size() && b[j] >> i & 1; j++) {
        bn.push_back(b[j]);
      }
      swap(bn, b);
    } else {
      for (int j = 0; b[j] >> i & 1; j++) {
        b[j] &= ~(1LL << i);
      }
    }
  }
  cout << ans << endl;

  return 0;
}