#include"bits/stdc++.h" using namespace std; #define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++) #define rep(i,n) REP((i),0,(n)) using ll = long long; using pii = pair; using pll = pair; using tp3 = tuple; using Mat = vector>; constexpr int INF = 1 << 28; constexpr ll INFL = 1ll << 60; constexpr int dh[4] = { 0,1,0,-1 }; constexpr int dw[4] = { -1,0,1,0 }; bool isin(const int H, const int W, const int h, const int w) { return 0 <= h && h < H && 0 <= w && w < W; } // ============ template finished ============ ll all(const vector& A) { return accumulate(A.begin(), A.end(), 0ll); } vector make_acc(vector A) { const int N = A.size(); rep(i, N - 1) { A[i + 1] += A[i]; } return A; } ll get(const vector& acc, ll idx) { return idx == -1 ? 0 : acc[idx]; } void update(ll& bestIdx, ll& bestSpan, const ll idx, const ll span) { if (span < bestSpan) { bestIdx = idx; bestSpan = span; } } ll dist(ll from, ll to) { assert(from <= to); from--; return to - from; } ll need_span(const vector& acc, const ll idx, const ll val) { const ll N = acc.size(); auto itr = lower_bound(acc.begin(), acc.end(), val); if (itr != acc.end()) { return dist(idx, itr - acc.begin()); } else { ll need = val - acc.back(); itr = lower_bound(acc.begin(), acc.end(), need); return dist(idx, N - 1) + dist(0, itr - acc.begin()); } } pll shortest_span(const vector& acc, const ll M) { const ll N = acc.size(); ll bestIdx = -1, bestSpan = INF; rep(left, N) { const ll span = need_span(acc, left, get(acc, left - 1) + M); update(bestIdx, bestSpan, left, span); } return make_pair(bestIdx, bestSpan); } bool canCycle(const vector& acc, ll idx, const ll K, const ll M) { const ll N = acc.size(); ll step = 0; rep(_, K) { ll span = need_span(acc, idx, get(acc, idx - 1) + M); step += span; (idx += span) %= N; } return step <= N; } bool judge(const vector& A, const ll K, const ll M) { const ll N = A.size(); auto acc = make_acc(A); const auto[startIdx, span] = shortest_span(acc, M); assert(span < N / K + 1); for (int i = 0, idx = startIdx; i < span; i++, (++idx) %= N) { if (canCycle(acc, idx, K, M)) { return true; } } return false; } int main() { ll N, K; cin >> N >> K; vector A(N); rep(i, N)cin >> A[i]; if (K == 1) { cout << all(A) << endl; return 0; } ll safe = 0, out = all(A) / K + 1; while (abs(safe - out) > 1) { ll mid = (safe + out) / 2; (judge(A, K, mid) ? safe : out) = mid; } cout << safe << endl; return 0; }