Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define ALL(c) (c).begin(), (c).end()
#define PB push_back
#define IN(x, c) (find(c.begin(), c.end(), x) != (c).end())
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define INIT(a, v) memset(a, v, sizeof(a))
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
template <class A, class B> A cvt(B x) {
  stringstream ss;
  ss << x;
  A y;
  ss >> y;
  return y;
}

#define SPC << " " <<
#define DEBUG(x)                                                               \
  cerr << #x << " = ";                                                         \
  cerr << x << endl;
#define DEBUG_ITER(x)                                                          \
  cerr << #x << " = ";                                                         \
  for (auto _ : x)                                                             \
    cerr << _ << ' ';                                                          \
  cerr << endl;

typedef pair<int, int> PII;
typedef long long int64;
typedef vector<int> VI;

int n, k;
int a[200000];

int main() {
  // freopen("test.in","r",stdin);
  scanf("%d %d", &n, &k);
  REP(i, n) scanf("%d", &a[i]);
  int cant = 0, can = 1e9;
  REP(it, 200) {
    int l = (can + cant) / 2;
    int64 x = 0;
    REP(i, n) {
      x += a[i] / l;
      if (a[i] % l == 0)
        x--;
    }
    if (x > k)
      cant = l;
    else
      can = l;
  }
  printf("%d\n", can);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define ALL(c) (c).begin(), (c).end()
#define PB push_back
#define IN(x, c) (find(c.begin(), c.end(), x) != (c).end())
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define INIT(a, v) memset(a, v, sizeof(a))
#define SORT_UNIQUE(c)                                                         \
  (sort(c.begin(), c.end()),                                                   \
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
template <class A, class B> A cvt(B x) {
  stringstream ss;
  ss << x;
  A y;
  ss >> y;
  return y;
}

#define SPC << " " <<
#define DEBUG(x)                                                               \
  cerr << #x << " = ";                                                         \
  cerr << x << endl;
#define DEBUG_ITER(x)                                                          \
  cerr << #x << " = ";                                                         \
  for (auto _ : x)                                                             \
    cerr << _ << ' ';                                                          \
  cerr << endl;

typedef pair<int, int> PII;
typedef long long int64;
typedef vector<int> VI;

int n, k;
int a[200000];

int main() {
  // freopen("test.in","r",stdin);
  scanf("%d %d", &n, &k);
  REP(i, n) scanf("%d", &a[i]);
  int cant = 0, can = 1e9;
  while (can - cant > 1) {
    int l = (can + cant) / 2;
    int64 x = 0;
    REP(i, n) {
      x += a[i] / l;
      if (a[i] % l == 0)
        x--;
    }
    if (x > k)
      cant = l;
    else
      can = l;
  }
  printf("%d\n", can);
  return 0;
}