malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   void prepare() {}   const int maxn = 100005;   int n;   int a[maxn], b[maxn];   vector<int> res;   int q[maxn];   void go(int x) {     int l = 0, r = 0;     long long cur = a[0];     while (true) {       cur -= b[r];       (++r) %= n;       while (cur < 0) {         ((--l) += n) %= n;         cur -= b[l];         cur += a[l];         if (l == r) break;       }       if (l != r)         cur += a[r];       else         break;     }     assert(cur == 0);     int s = l;     do {       ((--l) += n) %= n;       cur -= b[l];       cur += a[l];       if (cur >= 0) res.push_back(x ? q[l] : l);     } while (l != s);   }   bool solve() {     int i;     scanf( %d , &n);     for (i = (0); i < (n); i++) scanf( %d , &a[i]);     for (i = (0); i < (n); i++) scanf( %d , &b[i]);     for (i = (0); i < (n); i++) q[i] = n - i - 1;     go(0);     reverse(a, a + n);     reverse(b, b + n - 1);     go(1);     sort(res.begin(), res.end());     res.resize(unique(res.begin(), res.end()) - res.begin());     printf( %d n , (int)(res).size());     for (i = (0); i < ((int)(res).size()); i++) printf( %d  , res[i] + 1);     return false;   }   int main() {     prepare();     while (solve())       ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const int imax = 2147483647;   const int imin = -2147483647;   const long long lmax = 9223372036854775807;   const long long lmin = -9223372036854775807;   long long power(long long a, long long b, long long m) {     long long p = 1;     while (b) {       if (b & 1) p = (p * a) % m;       b >>= 1;       a = (a * a) % m;     }     return (p % m);   }   int main() {     int n;     cin >> n;     int arr[100005];     int distinct[100005] = {0}, mp[100005] = {0};     for (int i = 0; i < n; i++) cin >> arr[i];     for (int i = n - 1; i >= 0; i--) {       if (i == n - 1) {         mp[arr[i]] = 1;         distinct[i] = 1;       } else {         if (mp[arr[i]] == 0) {           mp[arr[i]] = 1;           distinct[i] = 1 + distinct[i + 1];         } else           distinct[i] = distinct[i + 1];       }     }     memset(mp, 0, sizeof(mp));     long long ans = 0;     for (int i = 0; i < n - 1; i++) {       if (mp[arr[i]] == 0) {         mp[arr[i]] = 1;         ans += (long long)distinct[i + 1];       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }   void solve(int test) {     int n, k;     cin >> n >> k;     vector<pair<pair<int, int>, int>> v(n);     int i, j, l;     for (i = (0); i < (n); ++i) cin >> v[i].first.first >> v[i].first.second;     for (i = (0); i < (n); ++i)       if (v[i].first.first == 0 && v[i].first.second == 0) break;     if (i == n) throw 0;     if (i != 0) swap(v[0], v[i]);     for (i = (1); i < (n); ++i) {       v[i].second = gcd(abs(v[i].first.first), abs(v[i].first.second));       v[i].first.first /= v[i].second;       v[i].first.second /= v[i].second;     }     sort(v.begin() + 1, v.end());     int prev = 1;     vector<double> all(1, 0.0), neg;     for (i = (2); i < (n + 1); ++i)       if (i == n || v[prev].first != v[i].first) {         long long x = v[prev].first.first;         long long y = v[prev].first.second;         double len = sqrt(x * x + y * y + 0.0);         vector<double> w;         for (j = (prev); j < (i); ++j) w.push_back(len * v[j].second);         reverse((w).begin(), (w).end());         int mid = min((k + 1) / 2, int((w).size()));         for (j = (0); j < (mid); ++j) all.push_back(w[j] * (k - 1 - 2 * j));         for (j = (mid); j < (int((w).size())); ++j) neg.push_back(w[j]);         prev = i;       }     sort((all).begin(), (all).end());     sort((neg).begin(), (neg).end());     if (int((all).size()) < k) {       int cnt = k - int((all).size());       int mid = (k + 1) / 2;       for (i = (0); i < (cnt); ++i)         all.push_back(neg[i] * (k - 1 - 2 * (mid + cnt - 1 - i)));       sort((all).begin(), (all).end());     }     reverse((all).begin(), (all).end());     double res = 0;     for (i = (0); i < (k); ++i) res += all[i];     cout.precision(11);     cout << res << endl;   }   int main() {     int T = 1, t;     for (t = (0); t < (T); ++t) solve(t);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 50, M = 55;   int B, bl[N];   vector<int> bcc[N];   inline void update(int &a, int b) { a = max(a, b); }   int n, m, d;   int dp[N * M];   char op[N][M];   vector<int> G[N], G2[N], G3[N];   void add(int u, int v, vector<int> G[]) { G[u].push_back(v); }   bool vis[N][M];   bool tv[N];   int val[N * M];   int c[N][M], tot;   vector<int> path;   void dfs(int u, int j, vector<int> G[], int col) {     vis[u][j] = 1;     c[u][j] = col;     if (op[u][j] ==  1  && !tv[u]) {       tv[u] = 1;       ++val[col];       path.push_back(u);     }     for (int v : G[u]) {       if (!vis[v][(j + 1) % d]) dfs(v, (j + 1) % d, G, col);     }   }   void doit(int bid) {     for (int bx : bcc[bid])       for (int j = 0; j < d; ++j)         if (!vis[bx][j]) {           path.clear();           dfs(bx, j, G2, ++tot);           for (int x : path) tv[x] = 0;         }   }   int topo(vector<int> G[]) {     for (int i = 0; i < B; ++i) {       for (int bx : bcc[i])         for (int j = 0; j < d; ++j) {           int uc = c[bx][j], u = bx;           for (int v : G[u]) {             int vc = c[v][(j + d - 1) % d];             update(dp[vc], dp[uc] + val[uc]);           }         }     }     int cc = c[0][0];     return dp[cc] + val[cc];   }   pair<int, int> e[N];   namespace tarjan {   int low[N], dfn[N], clk;   void init(int n) {     B = clk = 0;     memset(dfn, 0, sizeof(int) * (n + 1));     for (int i = 0; i < n; ++i) bcc[i].clear();   }   void tarjan(int u) {     static int st[N], p;     static bool in[N];     dfn[u] = low[u] = ++clk;     st[p++] = u;     in[u] = true;     for (int &v : G[u]) {       if (!dfn[v]) {         tarjan(v);         low[u] = min(low[u], low[v]);       } else if (in[v])         low[u] = min(low[u], dfn[v]);     }     if (dfn[u] == low[u]) {       while (1) {         int x = st[--p];         in[x] = false;         bl[x] = B;         bcc[B].push_back(x);         if (x == u) break;       }       ++B;     }   }   void go(int n) {     init(n);     for (int i = 0; i < n; ++i) {       if (!dfn[i]) tarjan(i);     }   }   }  // namespace tarjan   int main(int argc, char const *argv[]) {     scanf( %d%d%d , &n, &m, &d);     for (int i = 0; i < m; ++i) {       scanf( %d%d , &e[i].first, &e[i].second);       --e[i].first;       --e[i].second;       add(e[i].first, e[i].second, G);     }     for (int i = 0; i < n; ++i) scanf( %s , op[i]);     tarjan::go(n);     for (int i = 0; i < m; ++i) {       int u = e[i].first, v = e[i].second;       if (bl[u] == bl[v])         add(u, v, G2);       else         add(v, u, G3);     }     for (int i = 0; i < B; ++i) doit(i);     int ans = topo(G3);     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int power(long long int x, unsigned long long int y) {     long long int res = 1;     while (y > 0) {       if (y & 1) {         res = res * x;       }       y = y >> 1;       x = x * x;     }     return res;   }   long long int powermod(long long int x, unsigned long long int y,                          long long int p) {     long long int res = 1;     x = x % p;     while (y > 0) {       if (y & 1) {         res = (res * x) % p;       }       y = y >> 1;       x = (x * x) % p;     }     return res;   }   int32_t main() {     long long int n, m;     cin >> n >> m;     vector<long long int> f(m + n);     f[0] = 1, f[1] = 1;     for (long long int i = (long long int)(2); i < (long long int)(m + n); i++) {       f[i] = f[i - 1] + f[i - 2];       f[i] %= 1000000007;     }     long long int ans = 2 * f[n];     ans %= 1000000007;     ans += 2 * f[m];     ans %= 1000000007;     ans -= 2;     if (ans < 0) ans += 1000000007;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   int B = 400;   using namespace std;   const long long inf = 1e8;   int MOD = 1e9 + 7;   const int mxn = 300010;   const int N = 1000005;   const double pi = 3.14159;   int dx[] = {0, 1, 0, -1, 1, -1, 1, -1};   int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};   int n, d, k;   vector<int> g[mxn];   vector<pair<int, int> > q[mxn];   long long add[mxn];   long long ans[mxn];   void dfs(int v, int p = -1, int d = 1, long long cur = 0) {     for (auto i : q[v]) {       int d_ = i.first, val = i.second;       cur += (long long)val;       int tot = min(d + d_ + 1, mxn);       add[tot] -= (long long)val;     }     cur += add[d];     ans[v] = cur;     for (auto i : g[v]) {       if (i == p) continue;       dfs(i, v, d + 1, cur);     }     for (auto i : q[v]) {       int d_ = i.first, val = i.second;       int tot = min(d + d_ + 1, mxn);       add[tot] += (long long)val;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n;     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       g[u].push_back(v), g[v].push_back(u);     }     int qn;     cin >> qn;     while (qn--) {       int n, d, val;       cin >> n >> d >> val;       q[n].push_back({d, val});     }     dfs(1);     for (int i = 1; i <= n; i++) cout << ans[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     t = 1;     for (int r = 0; r < t; r++) {       long long n, m, i, x, y, z, sum = 0;       cin >> n >> m;       vector<string> v(51);       char a =  a , b =  A ;       for (i = 0; i <= 50; i++) {         if (b >  Z ) b =  A ;         v[i].push_back(b);         if (i >= 26) {           v[i].push_back(a);           a++;         }         b++;       }       x = 0, y = 0;       vector<string> ans;       string temp;       while (y < m - 1) {         ans.push_back(v[y]);         y++;       }       x = y;       for (i = 0; i < (n - m + 1); i++, x++) {         cin >> temp;         if (temp ==  NO ) {           ans.push_back(ans[x - (m - 1)]);         } else {           ans.push_back(v[y]);           y++;         }       }       for (i = 0; i < n; i++) cout << ans[i] <<    ;       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int i, j, k;     int n, m, idx;     string s, s2;     cin >> s;     n = s.length();     int cnt = 0;     for (i = 0; i < n; i++) {       if (s[i] ==  a ) cnt++;     }     k = 0;     idx = 0;     for (i = 0; i < n; i++) {       if (k == cnt) break;       if (s[i] ==  a )         k++, idx = i + 1;       else         s2.push_back(s[i]);     }     k = n - idx;     k -= s2.length();     if (k % 2 == 0) {       k /= 2;       j = idx;       idx += k;       while (k > 0) {         s2.push_back(s[j]);         j++;         k--;       }       k = s.length() - 1;       for (i = s2.length() - 1; i >= 0; i--) {         if (s2[i] != s[k]) {           cout <<  :( ;           return 0;         }         k--;       }       for (i = 0; i < idx; i++) cout << s[i];     } else       cout <<  :( ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int p1[400010], p2[400010], p3[400010], p0[400010];   int cnt1, cnt2, cnt3, cnt0;   inline bool cmp(const int a, const int b) { return a > b; }   int main() {     int n;     cin >> n;     for (int i = 1, a, b, va; i <= n; i++) {       scanf( %1d%1d %d , &a, &b, &va);       if (a + b == 0)         p0[++cnt0] = va;       else if (a + b == 2)         p3[++cnt3] = va;       else if (a == 1)         p1[++cnt1] = va;       else         p2[++cnt2] = va;     }     sort(p1 + 1, p1 + 1 + cnt1, cmp);     sort(p2 + 1, p2 + 1 + cnt2, cmp);     long long ans = 0;     int fl = 0;     if (cnt2 < cnt1) swap(cnt2, cnt1), fl = 1;     for (int i = 1; i <= cnt1; i++) {       ans += p2[i] + p1[i];     }     if (fl)       for (int i = cnt1 + 1; i <= cnt2; i++) p0[++cnt0] = p1[i];     else       for (int i = cnt1 + 1; i <= cnt2; i++) p0[++cnt0] = p2[i];     sort(p0 + 1, p0 + 1 + cnt0, cmp);     for (int i = 1; i <= cnt3; i++) {       ans += p0[i] + p3[i];     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, y, flag = 0, count, z, x;     cin >> y;     if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0) {       x = 1;     } else {       x = 2;     }     z = y + 1;     if (x == 1)       count = 3;     else       count = 2;     while (flag != 1) {       if (count == 1) {         if (z % 400 == 0 || z % 4 == 0 && z % 100 != 0) {           a = 1;         } else           a = 2;         if (a == x) {           flag = 1;           break;         }       }       if (z % 400 == 0 || z % 4 == 0 && z % 100 != 0) {         if (count == 7) {           count = 2;         } else if (count == 6)           count = 1;         else           count += 2;       } else {         if (count == 7)           count = 1;         else           count += 1;       }       z++;     }     cout << z;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n = 0, m = 0, k;   vector<string> a;   int CountSpiders(int j) {     int sum = 0;     int jR = j, jL = j;     for (int i = 1; i < n; ++i) {       if (!(i % 2) && a[i][j] ==  U ) ++sum;       if (jR > 0) {         --jR;         if (a[i][jR] ==  R ) ++sum;       }       if (jL < m - 1) {         ++jL;         if (a[i][jL] ==  L ) ++sum;       }     }     return sum;   }   int main() {     cin >> n >> m >> k;     cin.ignore(100,   n );     a.resize(n);     for (int i = 0; i < n; ++i) getline(cin, a[i]);     for (int j = 0; j < m; ++j) {       cout << CountSpiders(j) <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   int xx1, xx2, yy1, yy2;   int x, y, r, ans;   char v[30000000];   int zhuan(int x, int y) { return (x + 1000) * 10000 + y + 1000; }   void swap(int &a, int &b) {     int h = a;     a = b;     b = h;   }   bool dis(int xx, int yy) {     return (xx - x) * (xx - x) + (yy - y) * (yy - y) <= r * r;   }   void fun(int x1, int y1, int x2, int y2) {     if (x1 > x2) swap(x1, x2);     if (y1 > y2) swap(y1, y2);     for (int i = x1; i <= x2; i++)       for (int j = y1; j <= y2; j++) {         int k = zhuan(i, j);         if (v[k] == 0 && dis(i, j)) {           v[k] = 1;           ans++;         }       }   }   int main() {     while (scanf( %d %d %d %d , &xx1, &yy2, &xx2, &yy1) != EOF) {       if (xx1 > xx2) swap(xx1, xx2);       if (yy1 > yy2) swap(yy1, yy2);       int n, i, j, k, w;       memset(v, 0, sizeof(v));       scanf( %d , &n);       ans = 0;       for (i = 1; i <= n; i++) {         scanf( %d %d %d , &x, &y, &r);         fun(xx1, yy1, xx1, yy2);         fun(xx1, yy2, xx2, yy2);         fun(xx2, yy2, xx2, yy1);         fun(xx2, yy1, xx1, yy1);       }       w = (xx2 - xx1 + 1) * 2 + (yy2 - yy1 + 1) * 2 - 4;       if (xx2 == xx1 && yy1 == yy2) w = 1;       printf( %d n , w - ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int n = s.length();     int ans = 0;     int num = 0;     for (int i = 0; i < n; i++) {       if ((s[i] -  0 ) % 3 == 0) {         ans++;         num = 0;       } else {         num++;         if (num == 2) {           if ((s[i - 1] -  0  + s[i] -  0 ) % 3 == 0) {             ans++;             num = 0;           }         }         if (num == 3) {           ans++;           num = 0;         }       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long t, n, i, b[1010], ta, tb, k1, a[1010], tem, x[1010], y[1010], k2, tom;   vector<long long> v[1010];   void dfs(long long aa, long long bb) {     b[aa] = bb;     long long ii;     for (ii = 0; ii < v[aa].size(); ii++)       if (!b[v[aa][ii]]) dfs(v[aa][ii], bb);   }   int main() {     cin >> t;     while (t--) {       cin >> n;       for (i = 1; i <= n; i++) v[i].clear();       for (i = 1; i <= n; i++) b[i] = 0;       for (i = 1; i < n; i++) {         cin >> ta >> tb;         v[ta].push_back(tb);         v[tb].push_back(ta);       }       for (i = 1; i <= n; i++) b[i] = 0;       for (i = 1; i <= n; i++) x[i] = 0;       for (i = 1; i <= n; i++) y[i] = 0;       cin >> k1;       for (i = 1; i <= k1; i++) {         cin >> a[i];         b[a[i]] = a[i];         x[a[i]] = 1;       }       for (i = 1; i <= k1; i++) dfs(a[i], a[i]);       cin >> k2;       for (i = 1; i <= k2; i++) {         cin >> ta;         y[ta] = 1;       }       cout <<  B   << ta << endl;       cin >> tem;       if (x[tem])         cout <<  C   << tem << endl;       else {         cout <<  A   << b[tem] << endl;         cin >> tom;         if (y[tom])           cout <<  C   << b[tem] << endl;         else           cout <<  C   << -1 << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s =  qwertyuiopasdfghjkl;zxcvbnm,./ ;     char lr;     cin >> lr;     string message;     cin >> message;     for (int i = 0; i < message.length(); i++) {       int it = s.find(message[i]);       if (lr ==  R ) {         cout << s[it - 1];       } else {         cout << s[it + 1];       }     }   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e6 + 3;   long long n, k, x;   pair<long long, long long> ans;   long long powmod(long long a, long long b, long long c) {     long long ans = 1;     while (b > 0) {       if (b & 1) ans = ans * a % c;       a = a * a % c;       b /= 2;     }     return ans;   }   pair<long long, long long> work(long long n, long long k) {     if (k == 0) {       return make_pair(0, 1);     }     long long dq = powmod(2, n, mod), now = 1;     for (int i = 1; i < 2 * mod; i += 2) {       now = now * (dq - i + 2 * mod) % mod;     }     long long ans;     ans = powmod(now, k / (2 * mod), mod);     for (int i = 1; i <= (k % (2 * mod)); i += 2)       ans = ans * (dq - i + 2 * mod) % mod;     pair<long long, long long> cur = work(n - 1, k / 2);     ans = ans * cur.second % mod;     return make_pair((k / 2 + cur.first) % (mod - 1), ans);   }   int main() {     cin >> n >> k;     x = 0;     while ((1LL << x) < k) x++;     if (n < x) {       cout <<  1 1  << endl;       return 0;     }     pair<long long, long long> ans = work(n, k - 1);     long long fz, fm;     fz = ans.second;     fm = (n % (mod - 1)) * ((k - 1) % (mod - 1)) % (mod - 1);     fm = (fm - ans.first + (mod - 1)) % (mod - 1);     fm = powmod(2, fm, mod);     fz = (fm - fz + mod) % mod;     cout << fz <<     << fm << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 40;   const long long INFLL = 1e18;   long long dp[N][3][2][2];   signed main() {     srand(time(NULL));     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     long long r1, r2, r3, d;     cin >> n >> r1 >> r2 >> r3 >> d;     vector<long long> a(n);     for (int(i) = 0; (i) != (n); (i)++) cin >> a[i];     for (int(i) = 0; (i) != (n); (i)++) {       for (int(j) = 0; (j) != (3); (j)++) {         for (int(k) = 0; (k) != (2); (k)++) {           dp[i][j][k][0] = dp[i][j][k][1] = INFLL;         }       }     }     dp[0][2][0][0] = 0;     for (int i = 0; i < n; i++) {       for (int j = 0; j < 30; j++) {         dp[i][2][0][0] = min(dp[i][2][0][0], dp[i][2][1][0] + 2 * d + r1);         dp[i][2][0][0] = min(dp[i][2][0][0], dp[i][2][1][1] + 2 * d + r1);         dp[i][1][0][0] = min(dp[i][1][0][0], dp[i][1][1][0] + 2 * d + r1);         dp[i][1][0][0] = min(dp[i][1][0][0], dp[i][1][1][1] + 2 * d + r1);         dp[i][0][0][0] = min(dp[i][0][0][0], dp[i][0][1][0] + 2 * d + r1);         dp[i][0][0][0] = min(dp[i][0][0][0], dp[i][0][1][1] + 2 * d + r1);         dp[i][0][0][0] = min(dp[i][0][0][0], dp[i][2][0][0] + a[i] * r1 + r3);         dp[i][0][1][0] = min(dp[i][0][1][0], dp[i][2][1][0] + a[i] * r1 + r3);         dp[i][1][0][1] =             min(dp[i][1][0][1], dp[i][2][0][0] + min(r2, a[i] * r1 + r1));         dp[i][1][1][1] =             min(dp[i][1][1][1], dp[i][2][1][0] + min(r2, a[i] * r1 + r1));         dp[i][0][0][0] = min(dp[i][0][0][0], dp[i][1][0][0] + r1);         dp[i][0][1][0] = min(dp[i][0][1][0], dp[i][1][1][0] + r1);         dp[i + 1][2][0][0] = min(dp[i + 1][2][0][0], dp[i][0][0][0] + d);         dp[i + 1][2][0][0] = min(dp[i + 1][2][0][0], dp[i][0][0][1] + d);         dp[i + 1][2][1][0] = min(dp[i + 1][2][1][0], dp[i][1][0][0] + d);         dp[i + 1][2][1][0] = min(dp[i + 1][2][1][0], dp[i][1][0][1] + d);       }     }     cout << min(dp[n - 1][0][0][0], dp[n - 1][0][1][0] + d + r1);   }
#include <bits/stdc++.h>   using namespace std;   typedef pair<int, int> pii;   template <typename T>   inline void read(T &x) {     int ch = getchar();     x = 0;     bool f = false;     double d = 1;     for (; ch !=  -  && (ch <  0  || ch >  9 ); ch = getchar())       ;     if (ch ==  - ) {       f = true;       ch = getchar();     }     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     if (ch ==  . )       for (ch = getchar(); ch >=  0  && ch <=  9 ; ch = getchar())         x += (ch -  0 ) * (d *= 0.1);     if (f) x = -x;   }   template <typename T>   inline bool chkmax(T &a, const T &b) {     if (a < b) return a = b, 1;     return 0;   }   template <typename T>   inline bool chkmin(T &a, const T &b) {     if (a > b) return a = b, 1;     return 0;   }   const int K = 81, N = 170, M = N << 4, INF = 0x3f3f3f3f;   int n, k, S, T, a[K], c[K], head[N], to[M], nxt[M], cap[M], w[M], tmp[K];   inline void add(int a, int b, int c, int d) {     static int cnt = 1;     to[++cnt] = b;     nxt[cnt] = head[a];     head[a] = cnt;     cap[cnt] = c;     w[cnt] = d;     to[++cnt] = a;     nxt[cnt] = head[b];     head[b] = cnt;     cap[cnt] = 0;     w[cnt] = -d;   }   int dis[N], inc[N], q[N], f, r, pre[N], ans;   bool inq[N];   inline void AMOD(int &x) {     ++x;     if (x >= N) x = 0;   }   inline bool spfa() {     memset(inq, 0, sizeof inq);     memset(dis, 0x3f, sizeof dis);     f = r = 0;     q[r++] = S;     inc[S] = INF;     dis[S] = 0;     inq[S] = true;     while (f != r) {       int u = q[f];       AMOD(f);       inq[u] = false;       for (register int i = head[u]; i; i = nxt[i])         if (cap[i] && chkmin(dis[to[i]], dis[u] + w[i])) {           inc[to[i]] = min(inc[u], cap[i]);           pre[to[i]] = i;           if (!inq[to[i]]) {             inq[to[i]] = true;             q[r] = to[i];             AMOD(r);           }         }     }     return dis[T] < INF;   }   int main() {     read(n);     read(k);     for (register int i = 1; i <= n; ++i) read(a[i]);     for (register int i = 1; i <= n; ++i) read(c[i]);     T = 1 + (S = n << 1 | 1);     for (register int i = 1; i <= n; ++i) {       add(S, i, 1, c[a[i]]);       add(i, i + n, 1, 0);       add(i + n, T, 1, 0);       if (i < n) add(i, i + 1, k - 1, 0);       if (tmp[a[i]]) add(i - 1, tmp[a[i]] + n, 1, -c[a[i]]);       tmp[a[i]] = i;     }     while (spfa()) {       ans += inc[T] * dis[T];       for (register int u = T; u; u = to[pre[u] ^ 1]) {         cap[pre[u]] -= inc[T];         cap[pre[u] ^ 1] += inc[T];       }     }     printf( %d , ans);   }