alexjercan/bugnet huggingface dataset:
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>
using namespace std;

#define LIM 519430400
#define LEN 13

char H[LIM][LEN];

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

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

int h1(long long key) { return key % LIM; }
int h2(long long key) { return 1 + (key % (LIM - 1)); }
long long hashValue(long long key, int i) { return h1(key) + i * h2(key); }

int insert(char str[]) {
  long long key = getKey(str);
  for (int i = 0; i < LIM; i++) {
    long long h = hashValue(key, i);
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 1;
    }
  }
  return 0;
}
int find(char str[]) {
  long long key = getKey(str);
  for (int i = 0; i < LIM; i++) {
    long long h = hashValue(key, i);
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
  return 0;
}

int main() {
  int n;
  char com[6], str[12];
  scanf("%d", &n);

  while (scanf("%s %s", com, str) != EOF) {
    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>
using namespace std;

#define LIM 519430400
#define LEN 13

char H[LIM][LEN];

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

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

int h1(long long key) { return key % LIM; }
int h2(long long key) { return 1 + (key % (LIM - 1)); }
long long hashValue(long long key, int i) { return h1(key) + i * h2(key); }

int insert(char str[]) {
  long long key = getKey(str);
  for (int i = 0; i < LIM; i++) {
    long long h = hashValue(key, i);
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 1;
    }
  }
  return 0;
}
int find(char str[]) {
  long long key = getKey(str);
  for (int i = 0; i < LIM; i++) {
    long long h = hashValue(key, i);
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
  return 0;
}

int main() {
  int n;
  char com[6], str[12];
  scanf("%d", &n);

  while (scanf("%s %s", com, str) != EOF) {
    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }

  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  set<string> st;
  set<string>::iterator it;
  string s1, s2;
  int n;
  cin >> n;

  while (n--) {
    cin >> s1 >> s2;
    if (s1 == "insert") {
      st.insert(s2);
    } else {
      it = st.find(s2);
      if (it != st.end()) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
  set<string> st;
  set<string>::iterator it;
  string s1, s2;
  int n;
  cin >> n;

  while (n--) {
    cin >> s1 >> s2;
    if (s1 == "insert") {
      st.insert(s2);
    } else {
      it = st.find(s2);
      if (it != st.end()) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }

  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm> //std::find
#include <functional>
#include <iostream>
#include <list>
#include <set>
#include <string>
using namespace std;

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int n;
  cin >> n;
  std::string command;
  std::string str;
  // list<string> dictionary;
  std::set<string> dictionary;
  // list<string> ans;
  for (int i = 0; i < n; i++) {
    cin >> command;
    if (command == "insert") {
      cin >> str;

      dictionary.insert(str);
      // dictionary.sort();
    }
    if (command == "find") {
      cin >> str;
      // list<string>::iterator itr = std::find(dictionary.begin(),
      // dictionary.end(), str); dictionary.find(str);
      set<string>::iterator itr = dictionary.find(str);

      if (itr != dictionary.end()) {
        cout << "yes" << endl;
        // ans.insert(ans.begin(), "no")
      } else
        cout << "no" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm> //std::find
#include <functional>
#include <iostream>
#include <list>
#include <string>
using namespace std;

int main(void) {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int n;
  cin >> n;
  std::string command;
  std::string str;
  list<string> dictionary;
  list<string> ans;

  // list<string> ans;
  for (int i = 0; i < n; i++) {
    cin >> command;
    if (command == "insert") {
      cin >> str;

      dictionary.insert(dictionary.begin(), str);
      dictionary.sort();
    }
    if (command == "find") {
      cin >> str;

      list<string>::iterator ita = std::find(ans.begin(), ans.end(), str);
      if (ita != dictionary.end()) {
        list<string>::iterator itr =
            std::find(dictionary.begin(), dictionary.end(), str);
        if (itr != dictionary.end()) {
          cout << "yes" << endl;
          ans.insert(ans.begin(), str);
          // ans.insert(ans.begin(), "no")
        } else
          cout << "no" << endl;
      }
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

class cDict {
  string com, op;
  set<string> sd;
  set<string>::iterator it;

  void insert(string &);
  void find(string &);

public:
  cDict() {}

  ~cDict() {}

  void command(string &);
};

void cDict::insert(string &s) { sd.insert(s); }

void cDict::find(string &s) {
  it = sd.find(s);
  if (it != sd.end()) {
    cout << "yes" << endl;
  } else {
    cout << "no" << endl;
  }
}

void cDict::command(string &s) {
  istringstream iss(s);
  iss >> com >> op;
  if (com == "insert") {
    insert(op);
  } else if (com == "find") {
    find(op);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  string str;
  cin >> n;
  cin.ignore();

  cDict cd;

  for (int i = 0; i < n; ++i) {
    getline(cin, str);
    cd.command(str);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iostream>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

class cDict {
  string com, op;
  vector<string> vd;

  void insert(string &);
  string find(string &);

public:
  cDict() {}

  void command(string &);
};

void cDict::insert(string &s) { vd.push_back(s); }

string cDict::find(string &s) {
  if (std::find(vd.begin(), vd.end(), s) != vd.end())
    return string("yes");
  else
    return string("no");
}

void cDict::command(string &s) {
  istringstream iss(s);
  iss >> com >> op;
  if (com == "insert") {
    insert(op);
  } else if (com == "find") {
    cout << find(op) << endl;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  string str;
  cin >> n;
  cin.ignore();

  cDict cd;

  for (int i = 0; i < n; ++i) {
    getline(cin, str);
    cd.command(str);
  }

  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
using namespace std;

#define PB push_back

int main(int argc, char *argv[]) {
  std::ios_base::sync_with_stdio(false);

  int n;
  string str;
  set<string> sstr[4];

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;

    if (str[0] == 'i') {
      cin >> str;
      switch (str[0]) {
      case 'A':
        sstr[0].insert(str);
        break;
      case 'C':
        sstr[1].insert(str);
        break;
      case 'G':
        sstr[2].insert(str);
        break;
      case 'T':
        sstr[3].insert(str);
      }
    } else {
      cin >> str;
      switch (str[0]) {
      case 'A':
        if (sstr[0].find(str) != sstr[0].end())
          cout << "yes" << endl;
        else
          cout << "no" << endl;
        break;
      case 'C':
        if (sstr[1].find(str) != sstr[1].end())
          cout << "yes" << endl;
        else
          cout << "no" << endl;
        break;
      case 'G':
        if (sstr[2].find(str) != sstr[2].end())
          cout << "yes" << endl;
        else
          cout << "no" << endl;
        break;
      case 'T':
        if (sstr[3].find(str) != sstr[3].end())
          cout << "yes" << endl;
        else
          cout << "no" << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define PB push_back

int main(int argc, char *argv[]) {
  std::ios_base::sync_with_stdio(false);

  int n, ind;
  string str;
  vector<vector<string>> vvstr(25);

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str[0] == 'i') {
      cin >> str;
      vvstr[str[0] - 'A'].PB(str);
    } else {
      cin >> str;
      ind = str[0] - 'A';
      if (find(vvstr[ind].begin(), vvstr[ind].end(), str) != vvstr[ind].end())
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#include <array>
#include <iostream>
#include <string>

#define ALPHA 26

#define INPUT_LEN 12
#define INPUT_SIZE 2
#define DATA_SIZE (1 << (INPUT_LEN * INPUT_SIZE))

bool cache[INPUT_LEN + 1][DATA_SIZE];

int encode(const std::string &dna, const std::array<int, ALPHA> dna_map) {
  const int length = dna.length();

  int code = 0;
  for (int i = length - 1; i >= 0; i--) {
    code <<= INPUT_SIZE;
    code |= dna_map[dna[i] - 'A'];
  }

  return code;
}

int main(int argc, char *argv[]) {
  std::array<int, ALPHA> dna_map;
  dna_map.fill(-1);
  dna_map['A' - 'A'] = 0;
  dna_map['C' - 'A'] = 1;
  dna_map['G' - 'A'] = 2;
  dna_map['T' - 'A'] = 3;

  int n;
  std::cin >> n;

  for (int i = 0; i < n; i++) {
    std::string ops, dna;
    std::cin >> ops >> dna;

    const int code = encode(dna, dna_map);
    const int length = dna.length();

    if (ops == "insert") {
      cache[length][code] = true;
    } else {
      std::cout << (cache[length][code] ? "yes" : "no") << std::endl;
    }
  }

  return 0;
}

Error: -11

Fixed submission:

#include <array>
#include <iostream>
#include <string>

#define ALPHA 26

int encode(const std::string &dna, const std::array<int, ALPHA> dna_map) {
  const int length = dna.length();

  int code = 0;
  for (int i = length - 1; i >= 0; i--) {
    code <<= 4;
    code |= dna_map[dna[i] - 'A'];
  }

  return code;
}

#define INPUT_LEN 12
#define INPUT_SIZE 2
#define DATA_SIZE (1 << (INPUT_LEN * INPUT_SIZE))

bool cache[INPUT_LEN + 1][DATA_SIZE];

int main(int argc, char *argv[]) {
  std::array<int, ALPHA> dna_map;
  dna_map.fill(-1);
  dna_map['A' - 'A'] = 1;
  dna_map['C' - 'A'] = 2;
  dna_map['G' - 'A'] = 3;
  dna_map['T' - 'A'] = 4;

  int n;
  std::cin >> n;

  for (int i = 0; i < n; i++) {
    std::string ops, dna;
    std::cin >> ops >> dna;

    const int code = encode(dna, dna_map);
    const int length = dna.length();

    if (ops == "insert") {
      cache[length][code] = true;
    } else {
      std::cout << (cache[length][code] ? "yes" : "no") << std::endl;
    }
  }

  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

int main(void) {
  int n, t;
  int sum = 0;
  std::cin >> n >> t;
  std::vector<int> arr(n);
  for (auto &a : arr) {
    std::cin >> a;
    sum += a;
  }
  sum /= t;
  int max = *std::max_element(arr.begin(), arr.end());
  if (sum > max)
    max = sum;
  while (true) {
    int sum = 0;
    int c = 1;
    for (auto a : arr) {
      sum += a;
      if (sum > max) {
        c++;
        if (c > t)
          break;
        sum = a;
      }
    }
    if (c > t) {
      max++;
      continue;
    } else
      break;
  }

  std::cout << max << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

int main(void) {
  int n, t;
  std::cin >> n >> t;
  std::vector<int> arr(n);
  for (auto &a : arr)
    std::cin >> a;
  int max = *std::max_element(arr.begin(), arr.end());

  while (true) {
    int sum = 0;
    int c = 1;
    for (auto a : arr) {
      sum += a;
      if (sum > max) {
        c++;
        if (c > t)
          break;
        sum = a;
      }
    }
    if (c > t) {
      max++;
      continue;
    } else
      break;
  }

  std::cout << max << std::endl;

  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Runtime Error


#include <iostream>

using namespace std;

#define MAX 100000
int n, k;
long long w[MAX];

int check(long long p) {
  int i = 0;
  long long s = 0;

  for (int j = 0; j < k; j++) {

    s = 0;

    while (p >= w[i] + s) {
      s += w[i];

      i++;
      if (i == n) {
        return n;
      }
    }
  }

  return i;
}

//????????????????????§????????????????°?????????????
int minKw() {
  long long left = 0;
  long long right = 100000 * 10000;
  long long mid;
  int v;

  while ((right - left) > 1) {
    mid = (left + right) / 2;
    v = check(mid);

    if (v >= n) {
      right = mid;
    } else {
      left = mid;
    }
  }

  return right;
}

int main(void) {

  cin >> n >> k;
  //	insertionSortInput(w, n);

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

  cout << minKw() << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

#define W_MAX 10000
#define N_MAX 100000
#define K_MAX 100000

int check(long long p, long long w[], int n, int k) {
  int i = 0;

  for (int j = 0; j < k; j++) {

    long long s = 0;

    while (p >= w[i] + s) {
      s += w[i];

      i++;
      if (i == n) {
        return n;
      }
    }
  }

  return i;
}

//????????????????????§????????????????°?????????????
int minKw(long long w[], int n, int k) {
  long long left = 0;
  long long right = 100000 * 10000;
  long long mid;

  while ((right - left) > 1) {
    mid = (left + right) / 2;
    int v = check(mid, w, n, k);

    if (v >= n) {
      right = mid;
    } else {
      left = mid;
    }
  }

  return right;
}

int main(void) {
  long long w[W_MAX] = {0}; //??????????????????
  int n;                    //???????????°
  int k;                    //?????????????????°

  cin >> n >> k;
  //	insertionSortInput(w, n);

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

  long long ans = minKw(w, n, k);

  cout << ans << endl;

  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <ios>
#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;
int g_minCand = 0;

int GetMax(int a, int b) { return (a > b ? a : b); }

int partition(const vector<int> &vN, int m, int i, int p, int k) {
  if (p == k - 1) {
    int a = 0;
    for (int j = i; j < vN.size(); ++j)
      a += vN[j];
    return GetMax(m, a);
  }

  int q, a = 0;
  for (q = i; q <= vN.size() - k + p + 1; ++q) {
    if (a + vN[q] > g_minCand)
      break;
    a += vN[q];
  }

  int r = GetMax(m, a);
  return partition(vN, r, q, p + 1, k);
}

int main() {
  ios::sync_with_stdio(false);
  int n, k, i;
  cin >> n >> k;
  vector<int> vN(n, 0);
  int s = 0;
  int maxV = INT_MIN;
  for (i = 0; i < n; ++i) {
    cin >> vN[i];
    s += vN[i];
    if (vN[i] > maxV)
      maxV = vN[i];
  }
  g_minCand = ((double)s / (double)k + 0.9999);
  g_minCand = GetMax(g_minCand, maxV);

  while (g_minCand != partition(vN, 0, 0, 0, k))
    ++g_minCand;

  cout << g_minCand << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <ios>
#include <iostream>
#include <limits.h>
#include <vector>
using namespace std;
int g_ave = 0;
int g_sup = 0;

int GetMax(int a, int b) { return (a > b ? a : b); }

int partition(const vector<int> &vN, int m, int i, int p, int k) {
  if (p == k - 1) {
    int a = 0;
    for (int j = i; j < vN.size(); ++j)
      a += vN[j];
    return GetMax(m, a);
  }

  int q, a = 0;
  for (q = i; q <= vN.size() - k + p + 1; ++q) {
    if (a + vN[q] > g_ave)
      break;
    a += vN[q];
  }

  int t = INT_MAX;
  int r1, r2;
  for (int j = q; j <= vN.size() - k + p + 1; ++j) {
    int b = a;
    for (int s = q; s < j; ++s)
      b += vN[s];
    r1 = GetMax(m, b);
    if (r1 >= g_sup)
      return t;
    r2 = partition(vN, r1, j, p + 1, k);
    if (r2 < t)
      t = r2;
    if (r1 == r2)
      break;
  }

  return t;
}

int main() {
  ios::sync_with_stdio(false);
  int n, k, i;
  cin >> n >> k;
  vector<int> vN(n, 0);
  int s = 0;
  int maxV = INT_MIN;
  for (i = 0; i < n; ++i) {
    cin >> vN[i];
    s += vN[i];
    if (vN[i] > maxV)
      maxV = vN[i];
  }
  g_ave = ((double)s / (double)k + 0.9999);
  g_sup = g_ave + maxV;

  int m = partition(vN, 0, 0, 0, k);
  cout << m << "\\n";

  return 0;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool truck(long int M, int n, int k, int bag[]);

int main() {
  int n, k, bag[100000 + 10];
  cin >> n >> k;
  for (int j = 0; j < n; j++) {
    cin >> bag[j];
  }
  long int l = 0;
  long int h = pow(2, 31);
  bool test;
  while (l + 1 < h) {
    long int m = (l + h) / 2;
    test = truck(m, n, k, bag);
    if (test)
      h = m;
    else
      l = m;
  }
  //    cout << h << endl;
  printf("%ld\\n", h);
}

bool truck(long int M, int n, int k, int bag[]) {
  int wgt = 0, num = 1;
  for (int i = 0; i < n; i++) {
    if (wgt + bag[i] <= M)
      wgt += bag[i];
    else {
      num++;
      if (bag[i] <= M)
        wgt = bag[i];
      else {
        num = 100000 + 10;
        break;
      }
    }
  }
  //    if(num<=k) cout << 1 << endl;
  //    else cout << 0 << endl;
  return num <= k;
}

Error: TLE

Fixed submission:

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

bool truck(int M, int n, int k, int bag[]);

int main() {
  int n, k, bag[100000 + 10];
  cin >> n >> k;
  for (int j = 0; j < n; j++) {
    cin >> bag[j];
  }
  int l = 0;
  double h = pow(2, 37);
  bool test;
  while (l + 1 < h) {
    int m = (l + h) / 2;
    test = truck(m, n, k, bag);
    if (test)
      h = m;
    else
      l = m;
  }
  cout << h << endl;
}

bool truck(int M, int n, int k, int bag[]) {
  int wgt = 0, num = 1;
  for (int i = 0; i < n; i++) {
    if (wgt + bag[i] <= M)
      wgt += bag[i];
    else {
      num++;
      if (bag[i] <= M)
        wgt = bag[i];
      else {
        num = 100000 + 10;
        break;
      }
    }
  }

  return num <= k;
}
Problem p02270 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
bool can(int max, int num2, vector<int> a, int num1) {
  vector<int> m2;
  m2.push_back(0);
  for (int i = 0; i < num1; i++) {
    if (a[i] + *(m2.end() - 1) <= max)
      *(m2.end() - 1) += a[i];
    else
      m2.push_back(a[i]);
  }
  return (m2.size() <= num2);
}
int main() {
  int num1, num2, tmp, max_;
  cin >> num1 >> num2;
  vector<int> m, m3, copy;
  for (int i = 0; i < num1; i++) {
    cin >> tmp;
    m.push_back(tmp);
  }
  copy = m;
  sort(copy.begin(), copy.end());
  if (copy[num1 - 1] > accumulate(m.begin(), m.end(), 0) / num2) {
    max_ = copy[num1 - 1];
    if (can(max_, num2, m, num1)) {
      cout << max_ << endl;
      return 0;
    }
  } else
    max_ = accumulate(m.begin(), m.end(), 0) / num2;
  // cout<<"max_"<<max_<<endl;
  // cout<<"start:"<<start<<"end:"<<end<<endl;
  for (int i = max_; i <= max_ + copy[num1 - 1]; i++)
    if (can(i, num2, m, num1)) {
      cout << i << endl;
      return 0;
    }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
bool can(int max, int num2, vector<int> a, int num1) {
  vector<int> m2;
  for (int i = 0; i < num1;) {
    for (int j = i; j < num1;) {
      int compare =
          accumulate(a.begin() + i, a.begin() + j, 0) + *(a.begin() + j);
      if (j == num1 - 1 && compare <= max) {
        m2.push_back(compare);
        i = num1;
        break;
      } else if (compare <= max) {
        j++;
      } else {
        m2.push_back(compare - a[j]);
        i = j;
        break;
      }
    }
  }
  if (m2.size() <= num2)
    return 1;
  else
    return 0;
}
int main() {
  int num1, num2, tmp, max_;
  cin >> num1 >> num2;
  vector<int> m, m3, copy;
  for (int i = 0; i < num1; i++) {
    cin >> tmp;
    m.push_back(tmp);
  }
  copy = m;
  sort(copy.begin(), copy.end());
  if (copy[num1 - 1] > accumulate(m.begin(), m.end(), 0) / num2) {
    max_ = copy[num1 - 1];
    if (can(max_, num2, m, num1)) {
      cout << max_ << endl;
      return 0;
    }
  } else
    max_ = accumulate(m.begin(), m.end(), 0) / num2;
  // cout<<"max_"<<max_<<endl;
  // cout<<"start:"<<start<<"end:"<<end<<endl;
  for (int i = max_; i <= max_ + copy[num1 - 1]; i++)
    if (can(i, num2, m, num1)) {
      cout << i << endl;
      return 0;
    }
}