Problem p02598 (C++)

Buggy submission (status: Runtime Error):

#define INF 10000000000
#define MOD 1000000007

#if 1 // use_templates

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define rep(i, up) for (ll i = 0; i < (up); ++i)
#define repp(i, up) for (ll i = 0; i <= (up); ++i)
#define rrep(i, lo, up) for (ll i = (lo); i < (up); ++i)
#define rrepp(i, lo, up) for (ll i = (lo); i <= (up); ++i)
#define eep(i, lo, up) for ((i) = (lo); (i) < (up); ++(i))
#define eepp(i, lo, up) for ((i) = (lo); (i) <= (up); ++(i))
#define feach(it, vec) for (auto it = (vec).begin(); it != (vec).end(); ++it)

ll gcd(ll a, ll b);

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll r;
  while (r = a % b) {
    a = b;
    b = r;
  }
  return b;
}

#endif // end of use_templates

ll sum(ll mid2, const vector<ll> &vec) {
  ll r = 0;
  rep(i, vec.size()) { r += (vec[i] * 2 + mid2 - 1) / mid2; }
  return r;
}

int main() {
  ll n, k;
  cin >> n >> k;
  k += n;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  ll left2 = 0 * 2, right2 = 1000000001 * 2;
  while (right2 - left2 > 1) {
    ll mid2 = (left2 + right2) / 2;
    ll s = sum(mid2, a);
    if (s > k) {
      left2 = mid2;
    } else {
      right2 = mid2;
    }
  }
  if (sum(left2, a) <= k) {
    cout << (left2 + 1) / 2 << endl;
  } else {
    cout << (right2 + 1) / 2 << endl;
  }
  return 0;
}


Fixed submission:

#define INF 10000000000
#define MOD 1000000007

#if 1 // use_templates

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define rep(i, up) for (ll i = 0; i < (up); ++i)
#define repp(i, up) for (ll i = 0; i <= (up); ++i)
#define rrep(i, lo, up) for (ll i = (lo); i < (up); ++i)
#define rrepp(i, lo, up) for (ll i = (lo); i <= (up); ++i)
#define eep(i, lo, up) for ((i) = (lo); (i) < (up); ++(i))
#define eepp(i, lo, up) for ((i) = (lo); (i) <= (up); ++(i))
#define feach(it, vec) for (auto it = (vec).begin(); it != (vec).end(); ++it)

ll gcd(ll a, ll b);

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll r;
  while (r = a % b) {
    a = b;
    b = r;
  }
  return b;
}

#endif // end of use_templates

ll sum(ll mid2, const vector<ll> &vec) {
  ll r = 0;
  rep(i, vec.size()) { r += (vec[i] * 2 + mid2 - 1) / mid2; }
  return r;
}

int main() {
  ll n, k;
  cin >> n >> k;
  k += n;
  vector<ll> a(n);
  rep(i, n) { cin >> a[i]; }
  ll left2 = 0 * 2, right2 = 1000000001 * 2;
  while (right2 - left2 > 1) {
    ll mid2 = (left2 + right2) / 2;
    ll s = sum(mid2, a);
    if (s > k) {
      left2 = mid2;
    } else {
      right2 = mid2;
    }
  }
  if (left2 == 0) {
    cout << "1" << endl;
  } else if (sum(left2, a) <= k) {
    cout << (left2 + 1) / 2 << endl;
  } else {
    cout << (right2 + 1) / 2 << endl;
  }
  return 0;
}