#include <iostream> #include <vector> #include <numeric> #include <limits> #include <cstring> using namespace std; const int MAXN = 100005; bool divide(int a[], int n, int m, int tag) { for (int i = 0; i < n; ++i) { if (a[i] > tag) { return false; } } int sum = a[0]; int cnt = 1; for (int i = 1; i < n; ++i) { if (sum + a[i] > tag) { sum = a[i]; ++cnt; if (cnt > m) { return false; } } else { sum += a[i]; } } return true; } int a[MAXN]; int main(int argc, char** argv) { int n, m; while (cin >> n >> m) { int sum = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; sum += a[i]; } int num; if (sum % m == 0) { num = sum / m; } else { num = sum / m + 1; } int res = -1; for (int i = num; i <= sum; ++i) { if (divide(a, n, m, i)) { res = i; break; } } cout << res << endl; } return 0; }
|