// #define _GLIBCXX_DEBUG // for STL debug (optional) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long int; using int64 = long long int; template void chmax(T &a, T b) {a = max(a, b);} template void chmin(T &a, T b) {a = min(a, b);} template void chadd(T &a, T b) {a = a + b;} int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; const int INF = 1LL << 29; const ll LONGINF = 1LL << 60; const ll MOD = 1000000007LL; ll dp1[100010][3], dp2[250]; int inc[85][3], nxt[85][3], pre[85][85][3], suf[85][85][3]; int main() { int N, K; scanf("%d%d", &N, &K); vector S(N); vector C(N); for(int i=0; i> S[i], scanf("%lld", &C[i]); const string pat = "JOI"; vector< vector< vector > > pos(N, vector< vector >(3)); for(int i=0; i ava) { int np = p + ava; chmin(dp1[np][x], dp1[p][x] + C[i]); } else { ava = min(ava, rem); int cur = pos[i][x][ava - 1]; if(x == 2) { chmin(dp1[K][x], dp1[p][x] + C[i]); } else { int nx = x + 1; int c = upper_bound(pos[i][nx].begin(), pos[i][nx].end(), cur) - pos[i][nx].begin(); int cnt = (int)pos[i][nx].size() - c; // fprintf(stderr, "i = %d: p = %d, x = %d, cnt = %d\n",i, p, x, cnt); chmin(dp1[cnt][nx], dp1[p][x] + C[i]); } } } } } ans = dp1[K][2]; } if(ans == LONGINF) cout << -1 << endl; else cout << ans << endl; return 0; }