Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
  int n;
  int k;

  cin >> n >> k;

  int *w = (int *)malloc(n * sizeof(int));

  int max_w = 0;
  int sum_w = 0;
  int min_p = 0;
  int max_p = 0;
  int p = 0;
  if (n < 1) {
    return 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> w[i];
    sum_w += w[i];
    if (w[i] > max_w) {
      max_w = w[i];
    }
  }
  //????????????????°???????
  min_p = max(max_w, sum_w / k);
  //?????????????????§??????
  max_p = (max_w * n) / k;
  int result_min_p = max_p;
  int this_p;
  //????????¢?´¢
  while (max_p > min_p) {
    int current_w = 0;
    int current_k = 1;
    this_p = 0;
    //???????????????
    p = (min_p + max_p) >> 1;
    for (int i = 0; i <= n; ++i) {
      // p????¶?????????????????????????
      if (current_w > p) {
        --i;
        if (this_p < current_w - w[i])
          this_p = current_w - w[i];
        ++current_k;
        current_w = 0;
      }
      //??????????????°???k????¶?????????¨??????p???????????´??°??????????????´???
      if (current_k > k) {
        i = -1;
        current_w = 0;
        current_k = 1;
        min_p = p + 1;
        p = (min_p + max_p) >> 1;
        this_p = 0;
        continue;
      }
      current_w += w[i];
    }
    if (this_p < current_w - w[n])
      this_p = current_w - w[n];
    // for?????????????????????????????????????????°???k??\\????????¨???
    if (this_p < result_min_p)
      result_min_p = this_p;
    max_p = p;
  }
  // p??????
  cout << result_min_p << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
  int n;
  int k;

  cin >> n >> k;

  int *w = (int *)malloc(n * sizeof(int));

  int max_w = 0;
  int sum_w = 0;
  int min_p = 0;
  int max_p = 0;
  int p = 0;
  if (n < 1) {
    return 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> w[i];
    sum_w += w[i];
    if (w[i] > max_w) {
      max_w = w[i];
    }
  }
  //????????????????°???????
  min_p = max(max_w, sum_w / k);
  //?????????????????§??????
  max_p = (max_w * n) / k;
  int result_min_p = max_p;
  //????????¢?´¢
  while (max_p - min_p > 0) {
    int current_w = 0;
    int current_k = 1;
    //???????????????
    p = (min_p + max_p) >> 1;
    for (int i = 0; i <= n; ++i) {
      // p????¶?????????????????????????
      if (current_w > p) {
        --i;
        ++current_k;
        current_w = 0;
      }
      //??????????????°???k????¶?????????¨??????p???????????´??°??????????????´???
      if (current_k > k) {
        i = -1;
        current_k = 1;
        min_p = p + 1;
        p = (min_p + max_p) >> 1;
        continue;
      }
      //??????????????°???k????±????????????¨??????p???????????´??°??????????????´???
      if (i == n && current_k < k) {
        i = -1;
        current_w = 0;
        current_k = 1;
        max_p = p;
        p = (min_p + max_p) >> 1;
        continue;
      }
      current_w += w[i];
    }
    // for?????????????????????????????????????????°???k?????¨???
    if (p < result_min_p)
      result_min_p = p;
    max_p = p;
  }
  // p??????
  cout << result_min_p << endl;
  return 0;
}