malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 100009;   int first[maxn], du[maxn], pre[maxn][2];   struct edg {     int next, to;   } e[maxn << 1];   struct node {     int x, opt;   };   bool vis[maxn][2], ins[maxn], ok[maxn];   queue<node> q;   int n, m, s, e_sum;   bool flag;   inline int read() {     int x = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       x = x * 10 + ch -  0 ;       ch = getchar();     }     return x * f;   }   void add_edg(int x, int y) {     e_sum++;     e[e_sum].next = first[x];     first[x] = e_sum;     e[e_sum].to = y;   }   void print(int x, int opt) {     if (x == s && opt == 0) {       printf( %d  , x);       return;     }     print(pre[x][opt], opt ^ 1);     printf( %d  , x);   }   void dfs(int x) {     ok[x] = ins[x] = 1;     for (int i = first[x]; i; i = e[i].next) {       int w = e[i].to;       if (ins[w]) flag = 1;       if (!ok[w]) dfs(w);     }     ins[x] = 0;   }   int main() {     n = read();     m = read();     for (int i = 1; i <= n; i++) {       du[i] = read();       for (int j = 1; j <= du[i]; j++) {         int x = read();         add_edg(i, x);       }     }     s = read();     q.push((node){s, 0});     vis[s][0] = 1;     while (!q.empty()) {       int x = q.front().x, opt = q.front().opt;       q.pop();       for (int i = first[x]; i; i = e[i].next) {         int w = e[i].to;         if (!vis[w][opt ^ 1]) {           vis[w][opt ^ 1] = 1;           pre[w][opt ^ 1] = x;           q.push((node){w, opt ^ 1});         }       }     }     for (int i = 1; i <= n; i++)       if (du[i] == 0 && vis[i][1]) {         puts( Win );         print(i, 1);         return 0;       }     dfs(s);     if (flag)       puts( Draw );     else       puts( Lose );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m, l, r, x, mid;     cin >> n >> m;     int a[n][2];     for (int i = 0; i < n; i++) cin >> a[i][0] >> a[i][1];     int b[m];     for (int i = 0; i < m; i++) cin >> b[i];     int music[n];     for (int i = 0; i < n; i++) music[i] = 0;     music[0] = a[0][0] * a[0][1];     for (int i = 1; i < n; i++) music[i] = music[i - 1] + a[i][0] * a[i][1];     for (int i = 0; i < m; i++) {       x = b[i];       l = -1;       r = n - 1;       while (r - l > 1) {         mid = (l + r) / 2;         if (x <= music[mid])           r = mid;         else           l = mid;       }       cout << r + 1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int n, a[N], d[N], b[21][N], a2[25], ans[N];   void dg(int x) {     if (x > 20) {       if (d[0] < ans[0])         for (int i = 0; i <= d[0]; i++) ans[i] = d[i];       return;     }     int pos = 0;     for (int i = 1; i <= n; i++) pos |= a[i] & a2[x];     if (!pos)       dg(x + 1);     else {       int n0 = n;       for (int i = 1; i <= n0; i++) b[x][i] = a[i];       d[++d[0]] = a2[x];       for (int i = 1; i <= n0; i++)         if (b[x][i] & a2[x])           a[i] = b[x][i] - a2[x];         else           a[i] = b[x][i];       sort(a + 1, a + n0 + 1);       n = 0;       for (int i = 1; i <= n0; i++)         if (i == 1 || a[i] != a[i - 1]) a[++n] = a[i];       dg(x + 1);       d[d[0]] = -a2[x];       for (int i = 1; i <= n0; i++)         if (b[x][i] & a2[x])           a[i] = b[x][i] + a2[x];         else           a[i] = b[x][i];       n = 0;       for (int i = 1; i <= n0; i++)         if (i == 1 || a[i] != a[i - 1]) a[++n] = a[i];       dg(x + 1);       d[0]--;     }   }   int main() {     a2[0] = 1;     for (int i = 1; i <= 20; i++) a2[i] = a2[i - 1] * 2;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     ans[0] = 30;     dg(0);     printf( %d n , ans[0]);     for (int i = 1; i <= ans[0]; i++) printf( %d  , ans[i]);   }
#include <bits/stdc++.h>   using namespace std;   int a[1010][1010];   int mapa[1010][1010];   int n, m;   int TIME = 0;   bool ok(int val) {     for (int i = 0; i < n; i++) {       vector<int> row;       for (int j = 0; j < m; j++) {         if (a[i][j] >= val) row.push_back(j);       }       for (int first = 0; first < row.size(); first++)         for (int second = first + 1; second < row.size(); second++) {           if (mapa[row[first]][row[second]] == TIME) return true;           mapa[row[first]][row[second]] = TIME;         }     }     return false;   }   int solve(int lo, int hi) {     if (lo == hi) return lo;     int mid = (lo + hi + 1) / 2;     TIME++;     if (ok(mid)) return solve(mid, hi);     return solve(lo, mid - 1);   }   int main() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) scanf( %d , &a[i][j]);     printf( %d , solve(0, 1000100100));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long mypow(long long x, int y) {     long long an = 1;     while (y) {       if (y & 1) {         an *= x;         an %= 1000000007;       }       x *= x;       x %= 1000000007;       y >>= 1;     }     return an;   }   int d[1000001];   int f(int k, int w) {     memset(d, 0, sizeof(d));     d[k] = w;     long long an = mypow(k, w);     long long tmp = an;     int now = k;     for (int i = k - 1; i; i--) {       d[i] += 2;       tmp *= mypow(i, 2);       tmp %= 1000000007;       while (d[now] == 1) now--;       d[now]--;       tmp *= mypow(now, 1000000007 - 2);       tmp %= 1000000007;       an += tmp;       if (an >= 1000000007) an -= 1000000007;     }     return an;   }   int main() {     int i, j, k, w;     scanf( %d%d , &k, &w);     int an = f(k, w + 1) - f(k, w);     if (an < 0) an += 1000000007;     an += mypow(k, w);     if (an >= 1000000007) an -= 1000000007;     printf( %d n , an);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     int x, id;   };   node a[1005];   bool cmp(node a, node b) { return a.x < b.x; }   int main() {     int n, k;     int i;     scanf( %d%d , &n, &k);     for (i = 1; i <= n; i++) {       scanf( %d , &a[i].x);       a[i].id = i;     }     sort(a + 1, a + 1 + n, cmp);     int sum = 0;     for (i = 1; i <= n; i++) {       sum += a[i].x;       if (sum > k) break;     }     printf( %d n , i - 1);     for (i = i - 1; i > 0; i--) {       printf( %d  , a[i].id);     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   #pragma GCC optimization( unroll-loops )   using namespace std;   long long int n, k, ans = 0;   long long int arr[500005];   void solve() {     cin >> n >> k;     map<long long int, long long int> mpp, tot, till;     for (long long int i = 1; i <= n; i++) {       cin >> arr[i];       tot[arr[i]]++;     }     for (long long int i = 1; i <= n; i++) {       till[arr[i]]++;       if ((arr[i] % (k)) == 0) {         long long int a = arr[i] / k, b = arr[i] * k;         ans = ans + (mpp[a]) * (tot[b] - till[b]);       }       mpp[arr[i]]++;     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     {       solve();       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int c = 0;     int u =  a ;     for (int i = 0; i < s.length(); i++) {       if (c >= 26) break;       if (s[i] <= u) {         s[i] = u;         c++;         u++;         if (u >  z ) break;       }     }     if (c >= 26)       cout << s;     else       cout <<  -1 ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e+5;   char str[2 * maxn + 5];   int main() {     int cishu;     cin >> cishu;     while (cishu--) {       int n;       cin >> n;       for (int a = 0; a < n - 1; a++) cin >> str[a];       str[n - 1] =  0 ;       int tmp = n;       for (int i = 0; i < n; i++) {         if (str[i] ==  < ) {           int amt = 2;           for (int a = i + 1; a < n; a++) {             if (str[a] ==  < )               amt++;             else               break;           }           int print = tmp - amt + 1;           tmp = print - 1;           for (int b = 0; b < amt; b++)             printf( %d%c , print++, i + b == n - 1 ?   n  :    );           i = i + amt - 1;         } else           printf( %d%c , tmp--, i == n - 1 ?   n  :    );       }       tmp = 1;       for (int i = 0; i < n; i++) {         if (str[i] ==  > ) {           int amt = 2;           for (int a = i + 1; a < n; a++) {             if (str[a] ==  > )               amt++;             else               break;           }           int print = tmp + amt - 1;           tmp = print + 1;           for (int b = 0; b < amt; b++)             printf( %d%c , print--, i + b == n - 1 ?   n  :    );           i = i + amt - 1;         } else           printf( %d%c , tmp++, i == n - 1 ?   n  :    );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());   template <typename T>   T ceil(T a, T b) {     return (a + b - 1) / b;   }   template <typename T>   T binpow(T a, T b) {     T res = 1;     while (b > 0) {       if (b & 1) res = res * a;       a = a * a;       b >>= 1;     }     return res;   }   void solve() {     long long n, c = 0;     string s, t;     cin >> n;     cin >> s >> t;     if (n == 1) {       if (s == t) {         cout << 0;       } else {         cout << 1;       }       return;     }     vector<long long> a, b;     for (long long i = 1; i < n; i++) {       if (s[i] != t[i] && s[i - 1] != t[i - 1]) {         pair<char, char> a, b;         a = {s[i], t[i]};         b = {s[i - 1], t[i - 1]};         if (a != b) {           swap(s[i - 1], s[i]);           c++;         }       }     }     for (long long i = 0; i < n; i++) {       c += (s[i] != t[i]);     }     cout << c <<   n ;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long t = 1;     for (long long i = 1; i < t + 1; i++) {       solve();     }     cerr <<   nTime elapsed:   << 1000 * clock() / CLOCKS_PER_SEC <<  ms n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   class Data {    public:     int l, r, num;     Data() {}     Data(int a, int b, int c) {       l = a;       r = b;       num = c;     }     bool operator<(const Data& a) const { return l < a.r; }   };   vector<Data> a;   int main() {     int L, b, f, n, q, k;     cin >> L >> b >> f >> n;     for (int(qwe) = 0; (qwe) < (n); ++(qwe)) {       cin >> q >> k;       if (q == 2) {         for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i)           if (i->num == k) {             a.erase(i);             break;           }       } else {         bool ok = 0;         int l = 0;         int r = L - k;         if (!a.empty()) r = a[0].l - k - f;         if (l <= r) {           a.insert(a.begin(), Data(l, l + k, qwe + 1));           cout << l << endl;           continue;         }         for (int i = 0; i < ((int)(a).size()) - 1; ++i)           if (a[i].r + b <= a[i + 1].l - f - k) {             cout << a[i].r + b << endl;             a.insert(a.begin() + i + 1,                      Data(a[i].r + b, a[i].r + b + k, qwe + 1));             ok = 1;             break;           }         if (ok) continue;         r = L - k;         l = 0;         if (!a.empty()) l = a.back().r + b;         if (l <= r) {           a.push_back(Data(l, l + k, qwe + 1));           cout << l << endl;         } else           cout <<  -1 n ;       }     }     return 0;   }
#include <bits/stdc++.h>   int main() {     std::string str;     std::getline(std::cin, str);     int number = 0;     for (int j = 0, i = 0; j < str.size(); ++j) {       int n = 0;       while (j < str.size() && isdigit(str.at(j))) n = n * 10 + str.at(j++) - 48;       number = number + n * i++;       if (j >= str.size()) break;     }     std::cout << number << std::endl;   }
#include <bits/stdc++.h>   using namespace std;   char c;   int s = 0, k = 0, n = 0, x, f = 0;   ;   int main() {     while (cin.get(c)) {       if (c ==   n ) break;       switch (s) {         case 0: {           if (c >=  a  && c <=  z  || c >=  A  && c <=  Z  || c ==  _  ||               c >=  0  && c <=  9 ) {             s = 1;             k++;           } else {             cout <<  NO ;             return 0;           }           break;         }         case 1: {           if ((c >=  a  && c <=  z  || c >=  A  && c <=  Z  || c ==  _  ||                c >=  0  && c <=  9 ) &&               k < 16)             k++;           else if (c ==  @ ) {             s = 2;             k = 0;             f = 1;           } else {             cout <<  NO ;             return 0;           }           break;         }         case 2: {           if ((c >=  a  && c <=  z  || c >=  A  && c <=  Z  || c ==  _  ||                c >=  0  && c <=  9 ) &&               k < 16 && n < 32) {             k++;             n++;             f = 0;           } else if (c ==  .  && f == 0) {             k = 0;             n++;             f = 1;           } else if (c ==  /  && f == 0) {             k = 0;             f = 1;             s = 3;           } else {             cout <<  NO ;             return 0;           }           break;         }         case 3: {           if (c >=  a  && c <=  z  || c >=  A  && c <=  Z  || c ==  _  ||               c >=  0  && c <=  9 ) {             k++;             f = 0;           } else if (c ==  /  && f == 0) {             k = 0;             f = 1;           } else {             cout <<  NO ;             return 0;           }           break;         }       }     }     if (f == 1)       cout <<  NO ;     else       cout <<  YES ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       double a, b, c, d, k;       cin >> a >> b >> c >> d >> k;       double pen = a / c;       double pencil = b / d;       pen = ceil(pen);       pencil = ceil(pencil);       if ((pen + pencil) <= k) {         double diff = k - pen - pencil;         cout << pen + diff <<     << pencil <<   n ;       } else         cout << -1 <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1000023;   long long int power(long long int a, long long int b) {     long long int ans = 1;     while (b) {       if (b & 1) ans = (ans * a);       b /= 2;       a = (a * a);     }     return ans;   }   bool sortbysec(const pair<long long int, long long int> &a,                  const pair<long long int, long long int> &b) {     if (a.first > b.first)       return 1;     else if (a.first == b.first)       return (a.second < b.second);     else       return 0;   }   long long int hash1[N];   long long int p_pow[N];   string str;   long long int n;   void cal_pow() {     long long int p = 31;     for (long long int i = 0; i < N; i++) {       if (i != 0)         p_pow[i] = (p_pow[i - 1] * p) % 1000000007;       else         p_pow[i] = 1;     }   }   void calc() {     hash1[0] = str[0] -  a  + 1;     for (long long int i = 1; i <= str.size() - 1; i++) {       hash1[i] = (hash1[i - 1] + (p_pow[i] * (str[i] -  a  + 1)) % 1000000007) %                  1000000007;     }   }   long long int check(long long int val, long long int len) {     for (long long int i = 1; i < n - len; i++) {       long long int temp =           (p_pow[n - 1 - i] * (hash1[i + len - 1] - hash1[i - 1] + 1000000007) %            1000000007) %           1000000007;       if (temp == val) return 1;     }     return 0;   }   void solve() {     cin >> str;     cal_pow();     calc();     vector<pair<long long int, long long int>> v;     n = str.size();     for (long long int i = 0; i < n - 1; i++) {       long long int f = (p_pow[n - 1] * hash1[i]) % 1000000007;       long long int s =           (p_pow[i] * (hash1[n - 1] - hash1[n - i - 2] + 1000000007) %            1000000007) %           1000000007;       if (f == s) v.push_back({i + 1, f});     }     if (!v.size()) {       cout <<  Just a legend  << endl;       return;     }     sort((v).begin(), (v).end());     v.push_back({n + 3, 1});     long long int l = 0, r = v.size() - 1;     if (!check(v[0].second, v[0].first)) {       cout <<  Just a legend  << endl;       return;     }     while ((l + 1) < r) {       long long int mid = (l + r) / 2;       if (check(v[mid].second, v[mid].first))         l = mid;       else         r = mid;     }     long long int idx = 0, len = v[l].first;     while (len--) {       cout << str[idx];       idx++;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int Test;     Test = 1;     long long int t = 0;     while (t++ < Test) {       solve();     }     cerr <<  Time :   << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC          <<  ms n ;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pll = pair<ll, ll>;   bool cmpx(const pll& a, const pll& b) { return a.first < b.first; }   bool cmpy(const pll& a, const pll& b) { return a.second < b.second; }   ll sq(const ll& first) { return first * first; }   ll dis(const pll& a, const pll& b) {     return sq(a.first - b.first) + sq(a.second - b.second);   }   vector<pll> px, py;   ll closest_pair(int l, int r) {     if (r - l <= 1) return 1e18;     int m = (l + r) / 2;     ll d = min(closest_pair(l, m), closest_pair(m, r));     inplace_merge(py.begin() + l, py.begin() + m, py.begin() + r, cmpy);     vector<pll> strip;     for (int i = l; i < r; i++)       if (sq(py[i].first - px[m].first) < d) strip.push_back(py[i]);     for (int i = 0; i < strip.size(); i++)       for (int j = i + 1;            j < strip.size() && sq(strip[j].second - strip[i].second) < d; j++)         d = min(d, dis(strip[i], strip[j]));     return d;   }   void solve() {     int n;     cin >> n;     ll cur = 0, first;     for (int i = 0; i < n; i++) {       cin >> first;       cur += first;       px.emplace_back(i, cur);     }     sort(px.begin(), px.end(), cmpx);     py = px;     cout << closest_pair(0, n) <<   n ;   }   int main() {     ios_base::sync_with_stdio(0), cin.tie(0);     int owo = 1;     while (owo--) solve();   }
#include <bits/stdc++.h>   using namespace std;   const long long int INF = 1000000007;   const int N = 1000000 + 7;   long long int trans[N];   long long int ca[N];   long long int maxb[N];   int main() {     std::ios::sync_with_stdio(false);     cin.tie(NULL);     int n;     long long int d;     cin >> n >> d;     for (int i = 0; i < n; i++) cin >> trans[i];     ca[0] = trans[0];     for (int i = 1; i < n; i++) ca[i] = ca[i - 1] + trans[i];     maxb[n] = LLONG_MIN / 2;     for (int i = n - 1; i >= 0; i--) maxb[i] = max(ca[i], maxb[i + 1]);     long long int currentAdd = 0;     int ans = 0;     for (int i = 0; i < n; i++) {       ca[i] += currentAdd;       if (ca[i] > d) {         cout << -1 << endl;         return 0;       }       if (trans[i] == 0) {         if (ca[i] < 0) {           long long int all = d - (maxb[i + 1] + currentAdd);           if (ca[i] + all > d) {             all = d - ca[i];           }           ca[i] += all;           currentAdd += all;           if (ca[i] < 0) {             cout << -1 << endl;             return 0;           }           ans++;         }       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   const int mod = 1000000007;   int dp[4][200005];   int main() {     long long a, b;     scanf( %I64d %I64d , &a, &b);     long long t = sqrt(2 * (a + b));     while ((t * t + t > 2 * (a + b)) || ((t * t + 3 * t + 2) <= 2 * (a + b))) {       --t;     }     long long sum = t * (t + 1) / 2;     dp[0][0] = 1;     dp[1][0] = 1;     dp[1][1] = 1;     int cur = 0;     for (int i = 1; i <= t; ++i) {       long long temp = i * (i + 1) / 2;       for (int j = 0; j <= temp && j <= a; ++j) {         cur = i % 3;         if (j == 0) {           dp[cur][j] = 1;           continue;         }         int k = cur - 1;         if (k < 0) k = k + 3;         dp[cur][j] = dp[k][j];         if (j >= i) dp[cur][j] += dp[k][j - i];         if (dp[cur][j] >= mod) dp[cur][j] %= mod;       }     }     long long result = 0;     long long temp = sum - b;     if (temp < 0) temp = 0;     t = t % 3;     for (int i = (int)temp; i <= a; ++i) {       result += dp[t][i];       result %= mod;     }     printf( %I64d n , result);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char buf[100005];   int cnt[10];   int cnt2[10];   int main() {     scanf( %s , buf);     int N = strlen(buf);     for (int i = 0; i < (N); ++i) cnt[buf[i] -  0 ]++;     for (int i = 0; i < (10); ++i) cnt2[i] = cnt[i];     int best = 0, bestres = cnt[0];     for (int d = 1; d <= 9; ++d) {       if (!cnt[d] || !cnt2[10 - d]) continue;       cnt[d]--;       cnt2[10 - d]--;       int temp = 1;       for (int i = 0; i < (10); ++i) temp += min(cnt[i], cnt2[9 - i]);       temp += max(0, min(cnt[0] - cnt2[9], cnt2[0] - cnt[9]));       cnt[d]++;       cnt2[10 - d]++;       if (temp > bestres) {         bestres = temp;         best = d;       }     }     if (best == 0) {       for (int i = 0; i < (2); ++i) {         for (int j = 9; j >= 0; j--)           for (int k = 0; k < (cnt[j]); ++k) printf( %d , j);         printf(  n );       }     } else {       cnt[best]--;       cnt2[10 - best]--;       int ilezer = max(0, min(cnt[0] - cnt2[9], cnt2[0] - cnt[9]));       for (int j = 0; j < (10); ++j) {         int ile = cnt[j] - min(cnt[j], cnt2[9 - j]);         if (j == 0) ile -= ilezer;         for (int k = 0; k < (ile); ++k) printf( %d , j);       }       for (int j = 0; j < (10); ++j) {         for (int k = 0; k < (min(cnt[j], cnt2[9 - j])); ++k) printf( %d , j);       }       printf( %d , best);       for (int k = 0; k < (ilezer); ++k) printf( 0 );       printf(  n );       for (int j = 0; j < (10); ++j) {         int ile = cnt2[j] - min(cnt2[j], cnt[9 - j]);         if (j == 0) ile -= ilezer;         for (int k = 0; k < (ile); ++k) printf( %d , j);       }       for (int j = 9; j >= 0; j--) {         for (int k = 0; k < (min(cnt2[j], cnt[9 - j])); ++k) printf( %d , j);       }       printf( %d , 10 - best);       for (int k = 0; k < (ilezer); ++k) printf( 0 );       printf(  n );     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s[1005], ss[1005];     cin >> s[0] >> ss[0];     int n;     cin >> n;     for (int i = 1; i <= n; i++) cin >> s[i] >> ss[i];     for (int i = 0; i <= n; i++) {       if (i == 0) {         cout << s[0] <<     << ss[0] << endl;         continue;       } else if (s[i] == s[i - 1]) {         s[i] = ss[i];         ss[i] = ss[i - 1];         cout << s[i] <<     << ss[i] << endl;       } else if (s[i] == ss[i - 1]) {         s[i] = s[i - 1];         cout << s[i] <<     << ss[i] << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, k;   int a[100];   int ans;   int main() {     cin >> n >> m >> k;     for (int i = 0; i < k; i++) {       cin >> a[i];       a[i]--;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         int x;         cin >> x;         x--;         int pos = 0;         while (a[pos] != x) {           pos++;         }         ans += pos + 1;         for (int i = pos; i > 0; i--) {           a[i] = a[i - 1];         }         a[0] = x;       }     }     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     long long n, k;     cin >> n >> k;     long long ans = 0;     for (int i = (int)0; i < (int)n; ++i) {       long long l, r;       cin >> l >> r;       ans += r - l + 1;     }     int ted = 0;     while (ans % k) {       ted++;       ans++;     }     cout << ted << endl;   }