prithivMLmods/Coder-Stat huggingface dataset:
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
 * ALDS1_4_C.cpp
 *
 *  Created on: Apr 26, 2018
 *      Author: 13743
 */

#include <cstdio>

using namespace std;

const int SIZE = 1045527;
int table[SIZE];

int getKey(char *s) {
  int key = 0;
  int exp = 1;
  for (int i = 0; s[i] != '\0'; i++) {
    switch (s[i]) {
    case 'A':
      key += 4 * exp;
      break;
    case 'C':
      key += 3 * exp;
      break;
    case 'G':
      key += 2 * exp;
      break;
    default:
      key += 1 * exp;
      break;
    }
    exp *= 5;
  }
  return key;
}

void insert(char *s) {
  int key = getKey(s);
  int hash = key % SIZE;
  while (true) {
    if (table[hash] == 0 || table[hash] == key) {
      table[hash] = key;
      break;
    }
    hash = (hash + (1 + (key % (SIZE - 2)))) % SIZE;
  }
}

bool find(char *s) {
  int key = getKey(s);
  int hash = key % SIZE;
  int i = 0;
  while (i++ < SIZE) {
    if (table[hash] == key) {
      return true;
    }
    hash = (hash + (1 + (key % (SIZE - 2)))) % SIZE;
  }
  return false;
}

int main() {
  int n;
  scanf("%d", &n);
  char d[10];
  char s[13];
  for (int i = 0; i < n; i++) {
    scanf("%s %s", d, s);
    ;
    if (d[0] == 'i') {
      insert(s);
    } else {
      if (find(s))
        printf("yes\n");
      else
        printf("no\n");
    }
  }
}


Fixed submission:
/*
 * ALDS1_4_C.cpp
 *
 *  Created on: Apr 26, 2018
 *      Author: 13743
 */

#include <cstdio>

using namespace std;

const int SIZE = 1045527;
int table[SIZE];

int getKey(char *s) {
  int key = 0;
  int exp = 1;
  for (int i = 0; s[i] != '\0'; i++) {
    switch (s[i]) {
    case 'A':
      key += 4 * exp;
      break;
    case 'C':
      key += 3 * exp;
      break;
    case 'G':
      key += 2 * exp;
      break;
    default:
      key += 1 * exp;
      break;
    }
    exp *= 5;
  }
  return key;
}

void insert(char *s) {
  int key = getKey(s);
  int hash = key % SIZE;
  while (true) {
    if (table[hash] == 0 || table[hash] == key) {
      table[hash] = key;
      break;
    }
    hash = (hash + (1 + (key % (SIZE - 2)))) % SIZE;
  }
}

bool find(char *s) {
  int key = getKey(s);
  int hash = key % SIZE;
  int i = 0;
  while (i++ < SIZE) {
    if (table[hash] == key) {
      return true;
    } else if (table[hash] == 0) {
      return false;
    }
    hash = (hash + (1 + (key % (SIZE - 2)))) % SIZE;
  }
  return false;
}

int main() {
  int n;
  scanf("%d", &n);
  char d[10];
  char s[13];
  for (int i = 0; i < n; i++) {
    scanf("%s %s", d, s);
    ;
    if (d[0] == 'i') {
      insert(s);
    } else {
      if (find(s))
        printf("yes\n");
      else
        printf("no\n");
    }
  }
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <string>
#define M 5070721
#define NIL (-1)
#define L 14
using namespace std;
typedef long long ll;
char H[M][L];

int getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
  else
    return 0;
}

ll getKey(char str[]) {
  ll sum = 0, p = 1, i;
  int len = strlen(str);
  for (i = 0; i < len; i++) {
    sum += p * (getChar(str[i]));
  }
  return sum;
}

int h1(int key) { return key % M; }

int h2(int key) { return 1 + (key % (M - 1)); }

int find(char str[]) {
  ll key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
  return 0;
}

int insert(char str[]) {
  ll key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 0;
    }
  }
  return 0;
}

int main(void) {
  int n;
  char str[L], com[9];
  for (int i = 0; i < M; i++) {
    H[i][0] = '\0';
  }
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s %s", com, str);
    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <string>
#define M 5070721
#define NIL (-1)
#define L 14
using namespace std;
typedef long long ll;
char H[M][L];

int getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
  else
    return 0;
}

ll getKey(char str[]) {
  ll sum = 0, p = 1, i;
  int len = strlen(str);
  for (i = 0; i < len; i++) {
    sum += p * (getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key) { return key % M; }

int h2(int key) { return 1 + (key % (M - 1)); }

int find(char str[]) {
  ll key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
  return 0;
}

int insert(char str[]) {
  ll key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 0;
    }
  }
  return 0;
}

int main(void) {
  int n;
  char str[L], com[9];
  for (int i = 0; i < M; i++) {
    H[i][0] = '\0';
  }
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s %s", com, str);
    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <vector>

#define M 10000000

long long StrToID(std::string str) {
  long long id = 0;
  for (int i = 0; i < str.length(); ++i) {
    switch (str[i]) {
    case 'A':
      id = 1 + id * 10;
      break;
    case 'C':
      id = 2 + id * 10;
      break;
    case 'G':
      id = 3 + id * 10;
      break;
    case 'T':
      id = 4 + id * 10;
      break;
    default:
      break;
    }
  }
  return id;
}

bool includeNum(long long fig, std::vector<long long> S) {
  for (int i = 0; i < S.size(); ++i) {
    if (S[i] == fig)
      return true;
  }
  return false;
}

int main(int argc, char const *argv[]) {
  std::vector<std::vector<long long>> Dictionary(M);
  int N, key;
  long long id;
  std::string cmd, str;
  std::cin >> N;

  for (int i = 0; i < N; ++i) {
    std::cin >> cmd >> str;
    id = StrToID(str);
    key = id % M;
    if (cmd == "insert") {
      if (!includeNum(id, Dictionary[key]))
        Dictionary[key].push_back(id);
    } else if (cmd == "find") {
      if (includeNum(id, Dictionary[key])) {
        std::cout << "yes" << std::endl;
      } else {
        std::cout << "no" << std::endl;
      }
    } else {
      // exception
    }
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

#define M 1000003

long long StrToID(std::string str) {
  long long id = 0;
  for (int i = 0; i < str.length(); ++i) {
    switch (str[i]) {
    case 'A':
      id = 1 + id * 10;
      break;
    case 'C':
      id = 2 + id * 10;
      break;
    case 'G':
      id = 3 + id * 10;
      break;
    case 'T':
      id = 4 + id * 10;
      break;
    default:
      break;
    }
  }
  return id;
}

bool includeNum(long long fig, std::vector<long long> S) {
  for (int i = 0; i < S.size(); ++i) {
    if (S[i] == fig)
      return true;
  }
  return false;
}

int main(int argc, char const *argv[]) {
  std::vector<std::vector<long long>> Dictionary(M);
  int N, key;
  long long id;
  std::string cmd, str;
  std::cin >> N;

  for (int i = 0; i < N; ++i) {
    std::cin >> cmd >> str;
    id = StrToID(str);
    key = id % M;
    if (cmd == "insert") {
      if (!includeNum(id, Dictionary[key]))
        Dictionary[key].push_back(id);
    } else if (cmd == "find") {
      if (includeNum(id, Dictionary[key])) {
        std::cout << "yes" << std::endl;
      } else {
        std::cout << "no" << std::endl;
      }
    } else {
      // exception
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var)                                                            \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Dict {
  Dict() : buf_(initial_buf_size, list<string>()) {}

  void Insert(const string &s) {
    auto h = StrHash(s) % buf_.size();
    auto &slot = buf_[h];
    if (!FindInternal(slot, s)) {
      slot.push_front(s);
    }
  }

  bool Find(const string &s) const {
    auto h = StrHash(s) % buf_.size();
    auto &slot = buf_[h];
    return FindInternal(slot, s);
  }

private:
  constexpr static auto initial_buf_size = 1024;

  bool FindInternal(const list<string> &slot, const string &s) const {
    return find_if(slot.begin(), slot.end(), [&](const string &s0) {
             return !s0.compare(s);
           }) != slot.end();
  }

  static size_t StrHash(const string &s) {
    size_t hash = 17;
    for (auto &&c : s) {
      hash += (hash * 23) + static_cast<size_t>(c) * 7;
    }
    return hash;
  }

  vector<list<string>> buf_;
};

int main() {
  int n;
  cin >> n;
  Dict dic;
  for (int i = 0; i < n; ++i) {
    string cmd;
    string str;
    cin >> cmd >> str;
    if (!cmd.compare("insert")) {
      dic.Insert(str);
    } else {
      cout << (dic.Find(str) ? "yes" : "no") << endl;
    }
  }
}

Fixed submission:
#ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var)                                                            \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Dict {
  Dict() : buf_(initial_buf_size, list<string>()) {}

  void Insert(const string &s) {
    auto h = StrHash(s) % buf_.size();
    auto &slot = buf_[h];
    if (!FindInternal(slot, s)) {
      slot.push_front(s);
    }
  }

  bool Find(const string &s) const {
    auto h = StrHash(s) % buf_.size();
    auto &slot = buf_[h];
    return FindInternal(slot, s);
  }

private:
  constexpr static auto initial_buf_size = 1024 * 1024;

  bool FindInternal(const list<string> &slot, const string &s) const {
    return find_if(slot.begin(), slot.end(), [&](const string &s0) {
             return !s0.compare(s);
           }) != slot.end();
  }

  static size_t StrHash(const string &s) {
    size_t hash = 17;
    for (auto &&c : s) {
      hash += (hash * 23) + static_cast<size_t>(c) * 7;
    }
    return hash;
  }

  vector<list<string>> buf_;
};

int main() {
  int n;
  cin >> n;
  Dict dic;
  for (int i = 0; i < n; ++i) {
    string cmd;
    string str;
    cin >> cmd >> str;
    if (!cmd.compare("insert")) {
      dic.Insert(str);
    } else {
      cout << (dic.Find(str) ? "yes" : "no") << endl;
    }
  }
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;

string PopFront(string X) {
  string Y;
  for (int i = 1; i < X.size(); i++) {
    Y.push_back(X[i]);
  }
  return Y;
}

bool operator<(string &A, string &B) {
  int a = 0;
  int b = 0;
  while (!A.empty() && !B.empty()) {
    if ('A[0]' < 'B[0]') {
      a = 1;
      break;
    } else if ('A[0]' > 'B[0]') {
      b = 1;
      break;
    }
    A = PopFront(A);
    B = PopFront(B);
  }
  if (!A.empty()) {
    a = 1;
  } else if (!B.empty()) {
    b = 1;
  }
  return a < b;
}

void Judge(set<string> &RWord, string &Command, string &Word) {
  if (Command == "insert") {
    RWord.insert(Word);
  } else {
    if (binary_search(RWord.begin(), RWord.end(), Word)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  set<string> RWord;
  for (int i = 0; i < n; i++) {
    string Command;
    string Word;
    cin >> Command;
    cin >> Word;
    Judge(RWord, Command, Word);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;

string PopFront(string X) {
  string Y;
  for (int i = 1; i < X.size(); i++) {
    Y.push_back(X[i]);
  }
  return Y;
}

bool operator<(string &A, string &B) {
  int a = 0;
  int b = 0;
  while (!A.empty() && !B.empty()) {
    if ('A[0]' < 'B[0]') {
      a = 1;
      break;
    } else if ('A[0]' > 'B[0]') {
      b = 1;
      break;
    }
    A = PopFront(A);
    B = PopFront(B);
  }
  if (!A.empty()) {
    a = 1;
  } else if (!B.empty()) {
    b = 1;
  }
  return a < b;
}

void Judge(set<string> &RWord, string &Command, string &Word) {
  if (Command == "insert") {
    RWord.insert(Word);
  } else {
    set<string>::iterator it = RWord.find(Word);
    if (it != RWord.end()) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  set<string> RWord;
  for (int i = 0; i < n; i++) {
    string Command;
    string Word;
    cin >> Command;
    cin >> Word;
    Judge(RWord, Command, Word);
  }
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right > left) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right - left > 1) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right > left) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right - 1 > left) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right > left) {
    mid = (left + right) / 2 + 0.5;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

int check(llong P) {
  int i = 0;
  for (int j = 0; j < k; j++) {
    llong s = 0;
    while (s + T[i] <= P) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int solve() {
  llong left = 0;
  llong right = 100000 * 10000;
  llong mid;
  while (right - 1 > left) {
    mid = (left + right) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }

  return right;
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> T[i];
  llong ans = solve();
  cout << ans << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

// ------ main ------ //

int n, k, w[100000];

bool check(long long value) {
  long long v = 0;

  int c = 0;

  for (int i = 0; i < n; i++) {
    if (c >= k) {
      return false;
    }

    if (v + w[i] > value) {
      v = 0;

      c++;
      i--;
    } else {
      v += w[i];
    }
  }

  if (c > n) {
    return false;
  }

  return true;
}

int main() {
  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    cin >> w[i];
  }

  long long l = 0;
  long long r = (1LL << 60);
  long long m;

  while (r - l > 0) {
    m = (l + r) / 2;

    if (check(m) == true) {
      r = m;
    } else {
      l = m;
    }
  }

  cout << r << endl;

  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

// ------ main ------ //

int n, k, w[100000];

bool check(long long value) {
  long long v = 0;

  int c = 0;

  for (int i = 0; i < n; i++) {
    if (c >= k) {
      return false;
    }

    if (v + w[i] > value) {
      v = 0;

      c++;
      i--;
    } else {
      v += w[i];
    }
  }

  if (c > n) {
    return false;
  }

  return true;
}

int main() {
  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    cin >> w[i];
  }

  long long l = 0;
  long long r = (1LL << 60);
  long long m;

  while (r - l > 1) {
    m = (l + r) / 2;

    if (check(m) == true) {
      r = m;
    } else {
      l = m;
    }
  }

  cout << r << endl;

  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int w[100000];
int n, k;

int count(long long P) {
  int count = 0, i;
  long long Pn;
  for (i = k; i > 0; i--) {
    Pn = P;
    while (Pn - w[count] >= 0) {
      Pn -= w[count];
      count++;
      if (count == n)
        break;
    }
  }
  return count;
}

int main() {
  int i;
  long long max = 0, right, left, mid;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> w[i];
    if (max < w[i])
      max = w[i];
  }
  max *= n / k + 1;
  left = 0;
  right = max + 1;
  while (left < right) {
    mid = (left + right) / 2;
    if (count(mid) == n)
      right = mid;
    else {
      if (count(mid + 1) == n) {
        mid++;
        break;
      } else
        left = mid + 1;
    }
  }
  cout << mid << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int w[100000];
int n, k;

int count(long long P) {
  int count = 0, i;
  long long Pn;
  for (i = k; i > 0; i--) {
    Pn = P;
    while (Pn - w[count] >= 0) {
      Pn -= w[count];
      count++;
      if (count == n)
        return count;
    }
  }
  return count;
}

int main() {
  int i;
  long long max = 0, right, left, mid;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> w[i];
    if (max < w[i])
      max = w[i];
  }
  max *= n / k + 1;
  left = 0;
  right = max + 1;
  while (left < right) {
    mid = (left + right) / 2;
    if (count(mid) == n)
      right = mid;
    else {
      if (count(mid + 1) == n) {
        mid++;
        break;
      } else
        left = mid + 1;
    }
  }
  cout << mid << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
using namespace std;

int P(long long p, long long T[], int n, int k) {
  int i = 0, j;
  long long s;
  for (j = 0; j <= k - 1; j++) {
    s = 0;
    while (s + T[i] <= p) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int main() {
  int i;
  int n, k;
  long long T[100000];
  long long left = 0;
  long long right = 100000 * 10000;
  long long mid;
  int v;

  cin >> n;
  cin >> k;
  for (i = 0; i <= n - 1; i++)
    cin >> T[i];
  while (right - left > 1) {
    mid = (right - left) / 2;
    v = P(mid, T, n, k);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  cout << right << endl;
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;

int P(long long p, long long T[], int n, int k) {
  int i = 0, j;
  long long s;
  for (j = 0; j <= k - 1; j++) {
    s = 0;
    while (s + T[i] <= p) {
      s += T[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int main() {
  int i;
  int n, k;
  long long T[100000];
  long long left = 0;
  long long right = 100000 * 10000;
  long long mid;
  int v;

  cin >> n;
  cin >> k;
  for (i = 0; i <= n - 1; i++)
    cin >> T[i];
  while (right - left > 1) {
    mid = (right + left) / 2;
    v = P(mid, T, n, k);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  cout << right << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int n, k, w[100000], max = 1, sum = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    if (max < w[i])
      max = w[i];
    sum += w[i];
  }
  int ave = sum / n;
  int t;
  // tは平均値と要素の最大値のうちの大きいほう
  if (ave < max)
    t = max;
  else
    t = ave;
  int P = t;
  while (1) {
    // Pは許容量、numは必要なトラック数
    int s = 0, num = 1;
    for (int j = 0; j < n; j++) {
      s += w[j];
      // sが許容量を超えたらnumを増やし、sの初期値を積めなかった荷物にする
      if (s > P) {
        num++;
        s = w[j];
      }
    }
    // もしトラックの数が足りたら
    if (num <= k) {
      cout << P << endl;
      break;
    }
    // 足りなければ
    else {
      P++;
    }
  }
}


Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n, k, w[100000], max = 1, sum = 0;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> w[i];
    if (max < w[i])
      max = w[i];
    sum += w[i];
  }
  int ave = sum / k;
  int t;
  // tは平均値と要素の最大値のうちの大きいほう
  if (ave < max)
    t = max;
  else
    t = ave;
  int P = t;
  while (1) {
    // Pは許容量、numは必要なトラック数
    int s = 0, num = 1;
    for (int j = 0; j < n; j++) {
      s += w[j];
      // sが許容量を超えたらnumを増やし、sの初期値を積めなかった荷物にする
      if (s > P) {
        num++;
        s = w[j];
      }
    }
    // もしトラックの数が足りたら
    if (num <= k) {
      cout << P << endl;
      break;
    }
    // 足りなければ
    else {
      P++;
    }
  }
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;

int w[100000], n, k;
int cntp(int p) {
  int cnt = 1, wgh = 0;
  for (int i = 0; i < n;) {
    if (wgh + w[i] <= p) {
      wgh += w[i++];
    } else {
      if (wgh > 0)
        cnt++;
      else
        return k + 1;
      wgh = 0;
    }
  }
  return cnt;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; scanf("%d", w + i++))
    ;
  int l = 0, r = 100000, m;
  while (l < r) {
    m = (l + r) / 2; // printf("%d %d %d %d\n",l,m,r,cntp(m));
    if (cntp(m) > k)
      l = m + 1;
    else
      r = m;
  }
  for (m -= 20;; m++) {
    // printf("%d %d\n",m,cntp(m));
    if (cntp(m) <= k)
      break;
  }
  printf("%d\n", m);
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;

int w[100000], n, k;
int cntp(int p) {
  int cnt = 1, wgh = 0;
  for (int i = 0; i < n;) {
    if (wgh + w[i] <= p) {
      wgh += w[i++];
    } else {
      if (wgh > 0)
        cnt++;
      else
        return k + 1;
      wgh = 0;
    }
  }
  return cnt;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; scanf("%d", w + i++))
    ;
  int l = 0, r = 1000000000, m;
  while (l < r) {
    m = (l + r) / 2; // printf("%d %d %d %d\n",l,m,r,cntp(m));
    if (cntp(m) > k)
      l = m + 1;
    else
      r = m;
  }
  for (m -= 20;; m++) {
    // printf("%d %d\n",m,cntp(m));
    if (cntp(m) <= k)
      break;
  }
  printf("%d\n", m);
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

int main() {
  int n, k;
  int total = 0;
  vector<int> w;
  cin >> n >> k;
  REP(i, n) {
    int x;
    cin >> x;
    w.push_back(x);
    total += x;
  }
  int p = 1;
  while (1) {
    int num = 0;
    int track[k];
    REP(i, k) {
      track[i] = 0;
      while (num < n && i < k && track[i] + w[num] <= p) {
        track[i] += w[num];
        num++;
      }
    }
    if (num == n)
      break;
    p++;
  }
  cout << p << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define square(x) (x) * (x)
#define cube(x) (x) * (x) * (x)

const int INF = 1e9;

using namespace std;

int main() {
  int n, k;
  int total = 0;
  vector<int> w;
  cin >> n >> k;
  REP(i, n) {
    int x;
    cin >> x;
    w.push_back(x);
    total += x;
  }
  int p = total / k;
  while (1) {
    int num = 0;
    int track[k];
    REP(i, k) {
      track[i] = 0;
      while (num < n && i < k && track[i] + w[num] <= p) {
        track[i] += w[num];
        num++;
      }
    }
    if (num == n)
      break;
    p++;
  }
  cout << p << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int num_truck(const vector<int> &w, int P) {
  int num = 1;
  int p = P;
  for (int i = 0; i < w.size(); i++) {
    if (w[i] > P)
      return -1; //??????????????´???
    if (p >= w[i]) {
      p -= w[i];
      continue;
    }
    num++;
    p = P - w[i];
  }
  return num;
}
int calc_P(const vector<int> &w, int k, int min, int max) {
  int mid = (min + max) / 2;
  int num = num_truck(w, mid);
  if (num > k)
    return calc_P(w, k, mid + 1, max);
  if (num < k)
    return calc_P(w, k, min, mid - 1);
  if (num == num_truck(w, mid - 1))
    return calc_P(w, k, min, mid - 1);
  return mid;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> w(n);
  for (auto &i : w)
    cin >> i;
  int min_p = *max_element(w.begin(), w.end());
  int max_p = accumulate(w.begin(), w.end(), 0);
  cout << calc_P(w, k, min_p, max_p) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int num_truck(const vector<int> &w, int P) {
  int num = 1;
  int p = P;
  for (int i = 0; i < w.size(); i++) {
    if (w[i] > P)
      return -1; //??????????????´???
    if (p >= w[i]) {
      p -= w[i];
      continue;
    }
    num++;
    p = P - w[i];
  }
  return num;
}
int calc_P(const vector<int> &w, int k, int min, int max) {
  // cout << min << " " << max << endl;
  if (min > max)
    return min;
  int mid = (min + max) / 2;
  int num = num_truck(w, mid);
  if (num > k)
    return calc_P(w, k, mid + 1, max);
  if (num < k)
    return calc_P(w, k, min, mid - 1);
  if (num == num_truck(w, mid - 1))
    return calc_P(w, k, min, mid - 1);
  return mid;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> w(n);
  for (auto &i : w)
    cin >> i;
  int min_p = *max_element(w.begin(), w.end());
  int max_p = accumulate(w.begin(), w.end(), 0);
  cout << calc_P(w, k, min_p, max_p) << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <vector>

using namespace std;

static int f(const int *ws, int n, int P, int k) {
  int v = 0;
  int c = 0;
  int ntrack = 0;
  for (int i = 0; i < n; ++i) {
    if (c > 0 && P > 0 && c + ws[i] > P) {
      c = 0;
      ntrack++;
    }
    if (ntrack == k) {
      break;
    }

    if (c + ws[i] <= P) {
      c += ws[i];
      v++;
    }
  }
  return v;
}

int main() {
  int n, k;

  scanf("%d %d", &n, &k);

  // vector<int> ws(n);
  int ws[100000];
  int max = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &ws[i]);
    max += ws[i];
  }

  int min = 1;
  int mid = (min + max) / 2;
  while (min <= max) {
    if (f(ws, n, mid, k) >= n) {
      max = mid;
    } else { // v < n
      min = mid + 1;
    }
    mid = (min + max) / 2;
  }

  printf("%d\n", max);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <vector>

using namespace std;

static int f(const int *ws, int n, int P, int k) {
  int v = 0;
  int c = 0;
  int ntrack = 0;
  for (int i = 0; i < n; ++i) {
    if (c > 0 && P > 0 && c + ws[i] > P) {
      c = 0;
      ntrack++;
    }
    if (ntrack == k) {
      break;
    }

    if (c + ws[i] <= P) {
      c += ws[i];
      v++;
    }
  }
  return v;
}

int main() {
  int n, k;

  scanf("%d %d", &n, &k);

  // vector<int> ws(n);
  int ws[100000];
  int max = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &ws[i]);
    max += ws[i];
  }

  int min = 1;
  int mid = (min + max) / 2;
  while (min < max) {
    if (f(ws, n, mid, k) >= n) {
      max = mid;
    } else { // v < n
      min = mid + 1;
    }
    mid = (min + max) / 2;
  }

  printf("%d\n", max);
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
#define MAX 100000
typedef long long ll;

int n, k;
ll T[MAX];
int check(ll p) {
  int i = 0;
  for (int j = 0; j < k; ++j) {
    ll s = 0;
    while (s + T[i] <= p) {
      s += T[i];
      i++;
      if (i == n)
        return i;
    }
  }
  return i;
}
int slove() {
  ll left = 0;
  ll right = 100000 * 10000;
  while (left < right) {
    ll mid = left + (right - left) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main(int argc, char const *argv[]) {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> T[i];
  }
  ll ans = slove();
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
#define MAX 100000
typedef long long ll;

int n, k;
ll T[MAX];
int check(ll p) {
  int i = 0;
  for (int j = 0; j < k; ++j) {
    ll s = 0;
    while (s + T[i] <= p) {
      s += T[i];
      i++;
      if (i == n)
        return i;
    }
  }
  return i;
}
int slove() {
  ll left = 0;
  ll right = 100000 * 10000;
  while (left + 1 < right) {
    ll mid = left + (right - left) / 2;
    int v = check(mid);
    if (v >= n)
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main(int argc, char const *argv[]) {
  cin >> n >> k;
  for (int i = 0; i < n; ++i) {
    cin >> T[i];
  }
  ll ans = slove();
  cout << ans << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int n, k;
int A[100010];

bool f(ll p) {
  ll temp = p;
  int x = 0;
  for (int i = 0; i < k; i++) {
    while (temp >= A[x]) {
      temp -= A[x];
      x++;
    }
    temp = p;
  }
  if (x >= n)
    return true;
  else
    return false;
}

ll binary_find(ll left, ll right) {
  ll mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    if (f(mid))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  ll p, sum;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
  }
  p = binary_find(0, sum);
  cout << p << endl;
  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

int n, k;
ll A[100010];

bool f(ll p) {
  ll temp = p;
  int x = 0;
  for (int i = 0; i < k; i++) {
    while (temp >= A[x]) {
      temp -= A[x];
      x++;
    }
    temp = p;
  }
  if (x >= n)
    return true;
  else
    return false;
}

ll binary_find(ll left, ll right) {
  ll mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    if (f(mid))
      right = mid;
    else
      left = mid;
  }
  return right;
}

int main() {
  ll p, sum;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    sum += A[i];
  }
  p = binary_find(0, sum);
  cout << p << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define dbg(n) cerr << "//" << #n << ":" << n << endl
typedef pair<ll, ll> LLARR;
#define INF 999999999

int w[100003];
int n, k;

ll check(ll p) {
  ll ret = 0;
  ll tmp = 0;
  ll i = 0;
  REP(j, k) {
    ll s = 0;
    while (s + w[i] <= p) {
      s += w[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> k;
  REP(i, n) { cin >> w[i]; }

  ll ma = 10000 * 100000;
  ll l = 0;
  ll r = ma;
  ll mid = 0;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (check(mid) >= n) {
      r = mid;
    } else {
      l = mid;
    }
  }
  clog << "hoge" << endl;
  cout << r << endl;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define dbg(n) cerr << "//" << #n << ":" << n << endl
typedef pair<ll, ll> LLARR;
#define INF 999999999

int w[100003];
int n, k;

ll check(ll p) {
  ll ret = 0;
  ll tmp = 0;
  ll i = 0;
  REP(j, k) {
    ll s = 0;
    while (s + w[i] <= p) {
      s += w[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> k;
  REP(i, n) { cin >> w[i]; }

  ll ma = 10000 * 100000;
  ll l = 0;
  ll r = ma;
  ll mid = 0;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (check(mid) >= n) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define dbg(n) cerr << "//" << #n << ":" << n << endl
typedef pair<ll, ll> LLARR;
#define INF 999999999

int w[100003];
int n, k;

ll check(ll p) {
  ll ret = 0;
  ll tmp = 0;
  ll i = 0;
  REP(j, k) {
    ll s = 0;
    while (s + w[i] <= p) {
      s += w[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> k;
  REP(i, n) { cin >> w[i]; }
  dbg("kita");
  ll ma = 10000 * 100000;
  ll l = 0;
  ll r = ma;
  ll mid = 0;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (check(mid) >= n) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << endl;
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define dbg(n) cerr << "//" << #n << ":" << n << endl
typedef pair<ll, ll> LLARR;
#define INF 999999999

int w[100003];
int n, k;

ll check(ll p) {
  ll ret = 0;
  ll tmp = 0;
  ll i = 0;
  REP(j, k) {
    ll s = 0;
    while (s + w[i] <= p) {
      s += w[i];
      i++;
      if (i == n)
        return n;
    }
  }
  return i;
}

int main() {
  ios::sync_with_stdio(false);

  cin >> n >> k;
  REP(i, n) { cin >> w[i]; }

  ll ma = 10000 * 100000;
  ll l = 0;
  ll r = ma;
  ll mid = 0;
  while (l + 1 < r) {
    mid = (l + r) / 2;
    if (check(mid) >= n) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

vector<int> Input(const int n) {
  vector<int> W;
  for (int i = 0; i < n; i++) {
    int w;
    cin >> w;
    W.push_back(w);
  }
  return W;
}

bool IsAllLoaded(int P, vector<int> &W, int k) {
  int TrackIndex = 0;
  int CurrentSum = 0;
  int i = 0;
  for (i = 0; i < W.size(); i++) {
    if (W[i] > P) {
      return false;
    }
    if (CurrentSum + W[i] > P) {
      TrackIndex++;
      if (TrackIndex == k) {
        return false;
      }
      CurrentSum = 0;
    }
    CurrentSum += W[i];
  }
  return true;
}

int Calc(vector<int> W, int k) {
  int Sum = accumulate(W.begin(), W.end(), 0);
  int Ave = Sum / W.size();
  int e = Sum;
  int s = Ave;
  int m = (e + s) / 2;
  //	cout << s << ' ' << m << ' ' << e << endl;

  while (IsAllLoaded(m, W, k) || !IsAllLoaded(m + 1, W, k)) {
    if (IsAllLoaded(m, W, k) && IsAllLoaded(m + 1, W, k)) {
      e = m;
      m = (s + m) / 2;
    } else if (!IsAllLoaded(m, W, k) && !IsAllLoaded(m + 1, W, k)) {
      s = m;
      m = (e + m) / 2;
    }
    //		cout << s << ' ' << m << ' ' << e << endl;
  }
  return m + 1;
}

int main() {
  int n, k; // n:???????????° k:?????????????????°
  cin >> n >> k;
  vector<int> W = Input(n);
  int P = Calc(
      W,
      k); // P:??????????????????????????????????????????????????§????????????????°????
  cout << P << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

vector<int> Input(const int n) {
  vector<int> W;
  for (int i = 0; i < n; i++) {
    int w;
    cin >> w;
    W.push_back(w);
  }
  return W;
}

bool IsAllLoaded(int P, vector<int> &W, int k) {
  int TrackIndex = 0;
  int CurrentSum = 0;
  int i = 0;
  for (i = 0; i < W.size(); i++) {
    if (W[i] > P) {
      return false;
    }
    if (CurrentSum + W[i] > P) {
      TrackIndex++;
      if (TrackIndex == k) {
        return false;
      }
      CurrentSum = 0;
    }
    CurrentSum += W[i];
  }
  return true;
}

int Calc(vector<int> W, int k) {
  int Sum = accumulate(W.begin(), W.end(), 0);
  int Ave = Sum / W.size();
  int e = Sum;
  int s = Ave;
  int m = (e + s) / 2;
  //	cout << s << ' ' << m << ' ' << e << endl;

  while (IsAllLoaded(m, W, k) || !IsAllLoaded(m + 1, W, k)) {
    if (IsAllLoaded(m, W, k) && IsAllLoaded(m + 1, W, k)) {
      e = m;
      m = (s + m) / 2;
      if (s == e)
        return m;
    } else {
      s = m;
      m = (e + m) / 2;
    }
    //		cout << s << ' ' << m << ' ' << e << endl;
  }
  return m + 1;
}

int main() {
  int n, k; // n:???????????° k:?????????????????°
  cin >> n >> k;
  vector<int> W = Input(n);
  int P = Calc(
      W,
      k); // P:??????????????????????????????????????????????????§????????????????°????
  cout << P << endl;
  return 0;
}
Problem p02270 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)

const int MAX = 100000;

int function(int P, int k, int w[], int m) {
  int j = 0;
  int c = 0;
  int nk = 0;
  int np = 0;
  while (nk < k) {
    for (int i = j; i < m; i++, c++) {
      np += w[i];
      if (np > P) {
        np = 0;
        j = i;
        break;
      }
    }
    nk++;
  }
  return c;
}

int binarySearch(int key, int k, int w[], int m) {
  int left = 0;
  int right = MAX * 10000;
  int mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    if (function(mid, k, w, m) < key)
      left = mid;
    else
      right = mid;
  }
  return right;
}

int main() {
  int n, k;
  cin >> n >> k;
  int w[n];
  REP(i, n)
  cin >> w[i];
  cout << binarySearch(n, k, w, n) << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)

const int MAX = 100000;

int function(int P, int k, int w[], int m) {
  int j = 0;
  int c = 0;
  int nk = 0;
  int np = 0;
  while (nk < k) {
    for (int i = j; i < m; i++, c++) {
      np += w[i];
      if (np > P) {
        np = 0;
        j = i;
        break;
      }
      if (i == m - 1)
        return m;
    }
    nk++;
  }
  return c;
}

int binarySearch(int key, int k, int w[], int m) {
  int left = 0;
  int right = MAX * 10000;
  int mid;
  while (left + 1 < right) {
    mid = (left + right) / 2;
    if (function(mid, k, w, m) < key)
      left = mid;
    else
      right = mid;
  }
  return right;
}

int main() {
  int n, k;
  cin >> n >> k;
  int w[n];
  REP(i, n)
  cin >> w[i];
  cout << binarySearch(n, k, w, n) << endl;
}
Problem p02270 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int w[10003];
  REP(i, n) { cin >> w[i]; }
  int l = 0, r = 1e9 + 1;
  while (abs(r - l) > 1) {
    int p = (r + l) / 2;
    int j = 0;
    REP(i, k) {
      int tmpp = 0;
      while (tmpp + w[j] <= p) {
        tmpp += w[j];
        j++;
        if (j == n)
          break;
      }
      if (j == n)
        break;
    }
    if (j == n)
      r = p;
    else
      l = p;
  }
  cout << r << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int w[100003];
  REP(i, n) { cin >> w[i]; }
  int l = 0, r = 1e9 + 1;
  while (abs(r - l) > 1) {
    int p = (r + l) / 2;
    int j = 0;
    REP(i, k) {
      int tmpp = 0;
      while (tmpp + w[j] <= p) {
        tmpp += w[j];
        j++;
        if (j == n)
          break;
      }
      if (j == n)
        break;
    }
    if (j == n)
      r = p;
    else
      l = p;
  }
  cout << r << endl;
  return 0;
}
Problem p02271 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

bool can_make(vector<int> &A, int m, vector<vector<int>> dp, size_t i = 0) {
  if (i == A.size())
    return !m;
  if (!m)
    return (dp[m][i] = 1);
  if (m < 0)
    return false;
  if (~dp[m][i])
    return dp[m][i];

  if (m >= A[i])
    if (can_make(A, m - A[i], dp, i + 1))
      return (dp[m][i] = 1);

  return (dp[m][i] = can_make(A, m, dp, i + 1));
}

int main() {
  size_t n;
  scanf("%zu", &n);

  vector<int> A(n);
  for (size_t i = 0; i < n; ++i)
    scanf("%d", &A[i]);

  size_t q;
  scanf("%zu", &q);

  for (size_t i = 0; i < q; ++i) {
    int m;
    scanf("%d", &m);

    vector<vector<int>> dp(m + 1, vector<int>(A.size(), -1));
    printf("%s\n", can_make(A, m, dp) ? "yes" : "no");
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

bool can_make(vector<int> &A, int m, vector<vector<int>> &dp, size_t i = 0) {
  if (i == A.size())
    return !m;
  if (!m)
    return (dp[m][i] = 1);
  if (m < 0)
    return false;
  if (~dp[m][i])
    return dp[m][i];

  if (m >= A[i])
    if (can_make(A, m - A[i], dp, i + 1))
      return (dp[m][i] = 1);

  return (dp[m][i] = can_make(A, m, dp, i + 1));
}

int main() {
  size_t n;
  scanf("%zu", &n);

  vector<int> A(n);
  for (size_t i = 0; i < n; ++i)
    scanf("%d", &A[i]);

  size_t q;
  scanf("%zu", &q);

  for (size_t i = 0; i < q; ++i) {
    int m;
    scanf("%d", &m);

    vector<vector<int>> dp(m + 1, vector<int>(A.size(), -1));
    printf("%s\n", can_make(A, m, dp) ? "yes" : "no");
  }

  return 0;
}