• left를 maxv로 하지 않고 1로하면 에러가 난다.
  • 문제만 읽고서는 maxv를 반드시 사용해야할 필요는 모르겠는데 이로인해 에러가 난다. 
#include <iostream>
#include <algorithm>
#include <limits>
using namespace std;
int tb1[100001];
int sum = 0;
int n, m;
int maxv = 0;
//int maxidx;
int go(int k) {
       int temp = k;
       int cnt = 1;
       for (int i = 0; i < n; ++i) {
              int noo = temp - tb1[i];
              if (noo >= 0) {
                     temp = noo;
              }
              else {
                     temp = k - tb1[i];
                     cnt++;
              }
       }
       return cnt;
}
int main(void) {
       
       cin >> n >> m;
       for (int i = 0; i < n; ++i) {
              cin >> tb1[i];
              sum += tb1[i];
              maxv = max(maxv, tb1[i]);
       }
       
       int left = maxv;
       int right = sum;
       while (left <= right) {
              int mid = left + (right - left) / 2;
              //cout << mid << endl;
              if (go(mid) > m) {
                     left = mid + 1;
              }
              else {
                     right = mid - 1;
              }
       }
       
       cout << left << endl;
       return 0;
}


+ Recent posts