malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   char c;   string s;   set<char> q;   int i, sum;   int main() {     getline(cin, s);     for (i = 0; i < s.size(); i++)       if (isalpha(s[i])) q.insert(s[i]);     cout << q.size() << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<string> mas;   int n, m;   void input() {     cin >> n >> m;     mas.clear();     mas.resize(n);     for (int i = 0; i < n; ++i) cin >> mas[i];   }   void solve() {     string ans = mas[0];     for (int i = 0; i < m; ++i) {       char save = ans[i];       for (char d =  a ; d <=  z ; d++) {         ans[i] = d;         bool f = 1;         for (int j = 0; j < n; ++j) {           int err = 0;           for (int c = 0; c < m; ++c) {             if (ans[c] != mas[j][c]) err++;           }           if (err > 1) {             f = 0;             break;           }         }         if (f) {           cout << ans <<   n ;           return;         }       }       ans[i] = save;     }     cout <<  -1 n ;   }   int main() {     int Q;     cin >> Q;     while (Q--) {       input();       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> roda(int f, vector<int> valores) {     vector<int> temp(valores);     if (f == 0) {       temp[17] = valores[5];       temp[18] = valores[6];       temp[21] = valores[17];       temp[22] = valores[18];       temp[13] = valores[21];       temp[14] = valores[22];       temp[5] = valores[13];       temp[6] = valores[14];     } else if (f == 1) {       temp[3] = valores[17];       temp[4] = valores[19];       temp[16] = valores[3];       temp[14] = valores[4];       temp[10] = valores[16];       temp[9] = valores[14];       temp[17] = valores[10];       temp[19] = valores[9];     } else if (f == 2) {       temp[20] = valores[7];       temp[19] = valores[8];       temp[24] = valores[20];       temp[23] = valores[19];       temp[15] = valores[24];       temp[16] = valores[23];       temp[7] = valores[15];       temp[8] = valores[16];     } else if (f == 3) {       temp[1] = valores[24];       temp[3] = valores[22];       temp[5] = valores[1];       temp[7] = valores[3];       temp[9] = valores[5];       temp[11] = valores[7];       temp[24] = valores[9];       temp[22] = valores[11];     } else if (f == 4) {       temp[2] = valores[23];       temp[4] = valores[21];       temp[6] = valores[6];       temp[8] = valores[8];       temp[10] = valores[10];       temp[12] = valores[12];       temp[23] = valores[23];       temp[21] = valores[21];     } else if (f == 5) {       temp[1] = valores[15];       temp[2] = valores[13];       temp[18] = valores[1];       temp[20] = valores[2];       temp[12] = valores[18];       temp[11] = valores[20];       temp[15] = valores[12];       temp[13] = valores[11];     }     return temp;   }   bool checa(vector<int> valores) {     for (int i = 0; i < 6; i++) {       int cor = valores[1 + i * 4];       for (int j = 1; j < 4; j++)         if (valores[1 + i * 4 + j] != cor) return false;     }     return true;   }   int main() {     vector<int> valores(25);     int i;     for (i = 1; i < 25; i++) cin >> valores[i];     int resp = 0;     for (i = 0; i < 6; i++) {       resp = resp || checa(roda(i, valores));       resp = resp || checa(roda(i, roda(i, roda(i, valores))));     }     if (resp)       puts( YES );     else       puts( NO );   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1e5 + 10;   const int p = 1e9 + 7;   struct edge {     int l, r, uk;   };   edge a[MAXN];   int dp[4 * MAXN];   int b[MAXN];   int n, m;   void update(int k) {     dp[k] = (dp[k * 2] + dp[k * 2 + 1]) % p;     if (k > 1) update(k / 2);   }   long long getsum(int v, int ll, int rr, int l, int r) {     if (ll > r || rr < l) return 0;     if (ll >= l && rr <= r) return dp[v];     return (getsum(v * 2, ll, (ll + rr) / 2, l, r) +             getsum(v * 2 + 1, (ll + rr) / 2 + 1, rr, l, r)) %            p;   }   bool operator<(edge a, edge b) { return a.r < b.r; }   int binr(int l, int r, int q) {     while (l < r - 1) {       if (b[(l + r) / 2] < q)         l = (l + r) / 2;       else         r = (l + r) / 2;     }     return r;   }   int main() {     int i, size = 1, uk, l;     cin >> n >> m;     while (size < m) {       size *= 2;     }     for (i = 0; i < m; i++) {       cin >> a[i].l >> a[i].r;     }     sort(a, a + m);     b[0] = 0;     b[1] = a[0].r;     uk = 2;     for (i = 1; i < m; i++) {       if (a[i - 1].r != a[i].r) {         b[uk] = a[i].r;         uk++;       }       a[i].uk = uk;     }     dp[size] = 1;     update(size / 2);     int uk1 = 1;     for (i = 0; i < m; i++) {       l = binr(-1, uk - 1, a[i].l);       l++;       if (i != 0 && a[i - 1].r != a[i].r) uk1++;       dp[size + uk1] = (dp[size + uk1] + getsum(1, 1, size, l, uk1)) % p;       update((size + uk1) / 2);     }     uk--;     if (a[m - 1].r != n)       cout << 0;     else       cout << dp[size + uk1];     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[200];   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     int res = 0, ans = 0;     a[0] = -10;     for (int i = 1; i <= n; i++) {       if (a[i] == a[i - 1] + 1)         res++;       else         res = 0;       ans = max(res, ans);     }     ans = max(ans - 1, 0);     int f1 = 0;     for (int i = 1; i <= n + 1; i++) {       if (a[i] == i) {         f1++;       }     }     int f2 = 0;     for (int i = n; i >= 0; i--) {       if (a[i] == (1000 - n + i)) {         f2++;       }     }     if (f1 >= n || f2 >= n) {       f1 = n;       f2 = n;     }     ans = max(ans, max(f1, f2) - 1);     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, m;     cin >> n >> m;     vector<long int> a[n + 1], b[n + 1];     vector<pair<int, int> > v;     for (int i = 0; i <= n; i++) a[i].resize(m + 1), b[i].resize(m + 1);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) cin >> a[i][j];     }     for (int i = 1; i <= n - 1; i++) {       for (int j = 1; j <= m - 1; j++) {         if (a[i][j + 1] == 1 && a[i + 1][j] == 1 && a[i + 1][j + 1] == 1 &&             a[i][j] == 1) {           v.push_back(make_pair(i, j));           b[i][j + 1] = 1;           b[i][j] = 1;           b[i + 1][j] = 1;           b[i + 1][j + 1] = 1;         }       }     }     int ct = 1;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (a[i][j] != b[i][j]) ct = 0;       }     }     if (ct == 0)       cout << -1;     else {       cout << v.size() <<   n ;       for (int i = 0; i < v.size(); i++)         cout << v[i].first <<     << v[i].second <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long i, j, n, a, b, m, k, y, z, cos, pos, color;     long long r[5005], c[5005], x[100009];     cin >> n >> m >> k;     for (i = 1; i <= k; i++) {       cin >> cos >> pos >> color;       x[i] = color;       if (cos == 1)         r[pos] = i;       else         c[pos] = i;     }     for (i = 1; i <= n; i++) {       for (j = 1; j <= m; j++) {         y = max(r[i], c[j]);         cout << x[y] <<    ;       }       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, swaps = 0;     vector<int> seq;     cin >> n;     seq.resize(n * 2);     for (int i = 0; i < n * 2; i++) cin >> seq[i];     for (int i = 0; i < 2 * n - 1; i += 2) {       int j = i + 1;       while (seq[j] != seq[i]) j++;       for (int k = j; k > i + 1; k--) {         swap(seq[k - 1], seq[k]);         swaps++;       }     }     cout << swaps << endl;   }
#include <bits/stdc++.h>   using namespace std;   char s[2000][2000];   int n, m;   bool h[300][300];   bool v[300][300];   const long long mod = 1000000007;   void get(int x, int y, bool& h, bool& v) {     int cnt = 0;     for (int i = 0; i < 3; i++)       for (int j = 0; j < 3; j++) {         if (s[x + i][y + j] ==  O ) cnt++;       }     if (cnt == 5 || cnt == 4 || cnt == 1 || cnt == 0) {       v = h = true;       return;     }     if (cnt == 2 || cnt == 3) {       h = s[x][y] ==  O ;       v = !h;       return;     }     h = (s[x][y + 1] ==  O );     v = !h;   }   long long calc1(int y) {     if (n % 2 != 0) return 0;     for (int i = 0; i < n; i++)       if (!v[i][y]) return 0;     return 1;   }   long long tmp[300];   long long dp[300];   long long calc2(int y) {     if (y + 1 >= m) return 0;     for (int i = 0; i <= n; i++) tmp[i] = 0;     tmp[0] = 1;     for (int i = 0; i < n; i++) {       if (h[i][y] && h[i][y + 1]) {         tmp[i + 1] += tmp[i];         tmp[i + 1] %= mod;       }       if (i < n - 1 && v[i][y] && v[i + 1][y] && v[i + 1][y + 1] && v[i][y + 1]) {         tmp[i + 2] += tmp[i];         tmp[i + 2] %= mod;       }     }     if (calc1(y) && calc1(y + 1)) tmp[n] = (tmp[n] - 1 + mod) % mod;     return tmp[n];   }   int main() {     scanf( %d %d n , &n, &m);     for (int i = 0; i < 4 * n + 1; i++) {       gets(s[i]);     }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) get(4 * i + 1, 4 * j + 1, h[i][j], v[i][j]);     dp[0] = 1;     for (int i = 0; i < m; i++) {       dp[i + 1] += dp[i] * calc1(i);       dp[i + 2] += dp[i] * calc2(i);       dp[i + 1] %= mod;       dp[i + 2] %= mod;     }     cout << dp[m] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3e5 + 5;   long long a[N];   long long dp[N][20];   int main() {     int n, m, k;     cin >> n >> m >> k;     a[0] = 0;     for (int i = 1; i <= m; i++) dp[0][i] = -1e10;     long long ans = 0;     for (int i = 1; i <= n; i++) {       scanf( %lld , &a[i]);       for (int j = 1; j <= m; j++) {         if (j == 1)           dp[i][j] = max(a[i] - k, dp[i - 1][m] + a[i] - k);         else           dp[i][j] = dp[i - 1][j - 1] + a[i];         ans = max(ans, dp[i][j]);       }     }     printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   long long ar[100100];   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> ar[i];     }     sort(ar, ar + n);     long long dif1 = ar[1] - ar[0], dif2 = -1;     int cnt1 = 0, cnt2 = 0;     if (n == 1) {       cout << -1 << endl;       return 0;     } else if (n == 2) {       if (ar[0] == ar[1]) {         cout << 1 << endl << ar[0];         return 0;       } else {         if ((ar[0] + ar[1]) % 2 == 0) {           cout << 3 << endl                << ar[0] - (ar[1] - ar[0]) <<     << (ar[1] + ar[0]) / 2 <<                    << ar[1] + (ar[1] - ar[0]) << endl;           return 0;         } else {           cout << 2 << endl                << ar[0] - (ar[1] - ar[0]) <<     << ar[1] + (ar[1] - ar[0])                << endl;           return 0;         }       }     } else {       set<long long> nd;       set<long long> ans;       for (int i = 1; i < n; i++) {         nd.insert(ar[i] - ar[i - 1]);       }       if (nd.size() > 2) {         cout << 0 << endl;       } else if (nd.size() == 1) {         ans.insert(ar[0] - (ar[1] - ar[0]));         ans.insert(ar[n - 1] + (ar[1] - ar[0]));         cout << ans.size() << endl;         for (auto it : ans) {           cout << it << endl;         }       } else {         int ls1 = -1, ls2 = -1;         dif1 = *nd.begin();         dif2 = *(++nd.begin());         if (dif1 != 2 * dif2 && dif2 != 2 * dif1) {           cout << 0 << endl;           return 0;         }         for (int i = 1; i < n; i++) {           if (ar[i] - ar[i - 1] == dif1) {             cnt1++;             ls1 = i;           } else {             cnt2++;             ls2 = i;           }         }         if (cnt1 == 1 && dif1 % 2 == 0 && dif1 > dif2) {           cout << 1 << endl;           cout << ar[ls1 - 1] + dif2 << endl;           return 0;         }         if (cnt2 == 1 && dif2 % 2 == 0 && dif1 < dif2) {           cout << 1 << endl;           cout << ar[ls2 - 1] + dif1 << endl;           return 0;         }         cout << 0 << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[100009], b[100009], s[100009];   signed main() {     ios_base::sync_with_stdio(false), cin.tie(NULL);     long long i, j, n, m, c;     cin >> n >> m >> c;     for (i = 1; i <= n; i++) cin >> a[i];     for (i = 1; i <= m; i++) cin >> b[i], s[i] = (b[i] + s[i - 1]);     long long x = n - m;     for (i = 1; i <= n; i++) {       long long t = min(i, m);       long long g = min(i, min(n + 1 - i, x + 1));       long long y = s[t] - s[max(t - g, 0ll)];       a[i] = (a[i] + y) % c;     }     for (i = 1; i <= n; i++) cout << a[i] <<    ;   }
#include <bits/stdc++.h>   using namespace std;   long long mas[200004];   long long mas2[200004];   struct ver {     long long abs;     int number;     long long maxabs;     int masnumber;     ver *l;     ver *r;   };   void build(ver *v, int l, int r, long long *m) {     if (l == r) {       v->abs = m[l];       v->number = l;       v->masnumber = l;       v->maxabs = m[l];     } else {       ver *a = new ver;       ver *b = new ver;       v->l = a;       v->r = b;       build(a, l, (l + r) / 2, m);       build(b, (l + r) / 2 + 1, r, m);       if (a->maxabs < b->maxabs) {         v->maxabs = b->maxabs;         v->masnumber = b->masnumber;       } else {         v->maxabs = a->maxabs;         v->masnumber = a->masnumber;       }     }   }   pair<long long, long long> maxver(ver *v, int l, int r, int zl, int zr) {     pair<long long, long long> answer;     answer.first = -100000;     answer.second = 0;     if (l > zr || r < zl)       return answer;     else {       if (zl <= l && r <= zr) {         answer.first = v->maxabs;         answer.second = v->masnumber;         return answer;       } else {         pair<long long, long long> left;         pair<long long, long long> right;         left = maxver(v->l, l, (l + r) / 2, zl, zr);         right = maxver(v->r, (l + r) / 2 + 1, r, zl, zr);         if (left.first < right.first)           return right;         else           return left;       }     }   }   int main() {     ver *root = new ver;     int n, k;     pair<long long, long long> answer;     long long maxans = 0;     answer.first = 0;     answer.second = 0;     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> mas[i];     for (int i = 0; i < 200004; i++) mas2[i] = 0;     for (int i = n - k + 1; i <= n; i++) mas2[n - k + 1] += mas[i];     for (int i = n - k; i >= 1; i--) mas2[i] = mas2[i + 1] + mas[i] - mas[i + k];     build(root, 1, n, mas2);     for (int i = 1; i <= n - 2 * k + 1; i++) {       pair<long long, long long> an = maxver(root, 1, n, i + k, n);       if (an.first + mas2[i] > maxans) {         maxans = an.first + mas2[i];         answer.first = i;         answer.second = an.second;       }     }     cout << answer.first <<     << answer.second;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   double x[100000];   int main() {     ios_base::sync_with_stdio(false);     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; i++) cin >> x[i];     double ans = 0;     for (int i = 1; i <= m; i++) {       int fi, se, we;       cin >> fi >> se >> we;       ans = max(ans, (x[fi] + x[se]) / (double)(we));     }     if (n == 1) {       cout <<  0.0000000000  << endl;       return 0;     }     cout << fixed << setprecision(10) << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     while (n--) {       string s;       cin >> s;       bool b = true, b1 = false, b2 = true, b3 = false;       int sum = 0;       for (int i = 0; i < s.size(); i++) {         sum += (int)s[i] - 48;         int x = (int)s[i] - 48;         if (x % 2 == 0) {           if (x != 0) b3 = true;           if (x == 0 && b2 == false) b3 = true;         }         if (s[i] ==  0  && b2) {           b1 = true;           b2 = false;         }       }       if (sum % 3 == 0 && b1 && b3)         cout <<  red  << endl;       else         cout <<  cyan  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   long long t, d, ct, q, ans, l, m, r, n, mx, mn, i, j, k, x, y, z, s;   char ch;   string ss =   , str =   ;   long long a[200003], b[200003];   long long pref[200003];   int main() {     ios_base::sync_with_stdio(false), cin.tie(nullptr);     s = k = d = l = mx = m = mn = r = q = i = j = z = x = y = ct = ans = 0;     for (i = 0; i < 26; i++) cin >> a[i];     cin >> ss;     n = ss.length();     pref[1] = a[ss[0] -  a ];     for (i = 1; i < n; i++) pref[i + 1] = pref[i] + a[ss[i] -  a ];     vector<long long> v[26];     for (i = 0; i < n; i++) v[ss[i] -  a ].push_back(i + 1);     for (i = 0; i < 26; i++) {       m = v[i].size();       if (m <= 1) continue;       y = 0;       vector<long long> diff;       for (auto h : v[i]) diff.push_back(pref[h - 1]);       map<long long, long long> mp;       for (j = 0; j < diff.size(); j++) {         ans += mp[diff[j] - a[i]];         mp[diff[j]]++;       }     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long maps[105][105];   int main() {     long long a, b, c, d, i, j, k, l, t, n, m;     scanf( %lld , &t);     while (t--) {       scanf( %lld , &a);       for (i = 2; i * i <= a; i++) {         if (a % i) {           b = a % i;           c = a - b;           break;         }       }       printf( %lld %lld n , i, c);     }   }
#include <bits/stdc++.h>   int main() {     int64_t n;     std::cin >> n;     int64_t total(0), smallestOdd(0);     while (n--) {       int64_t x;       std::cin >> x;       total += x;       if ((x % 2) && (smallestOdd == 0 || x < smallestOdd)) {         smallestOdd = x;       }     }     if (total % 2) {       total -= smallestOdd;     }     std::cout << total << std::endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long double pi = acos(-1.0);   long long int inf = 0x3f3f3f3f3f3f3f3f;   const long long int mod = 1e9 + 7;   const long long int mx = 5 * 1000000;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t;     cin >> t;     while (t--) {       long long int n, T, a, b;       cin >> n >> T >> a >> b;       long long int type[n + 1];       for (long long int i = 0; i < n; i++) cin >> type[i];       vector<long long int> easy, hard;       vector<long long int> seq;       seq.push_back(0);       set<long long int> ss;       for (long long int i = 0; i < n; i++) {         long long int in;         cin >> in;         ss.insert(in);         if (type[i] == 0)           easy.push_back(in);         else           hard.push_back(in);       }       for (auto i : ss) seq.push_back(max(i - 1, 0ll));       seq.push_back(T);       sort(easy.begin(), easy.end());       sort(hard.begin(), hard.end());       long long int ans = 0;       for (auto i : seq) {         auto ee = upper_bound(easy.begin(), easy.end(), i);         long long int ee1;         if (ee == easy.end())           ee1 = easy.size();         else           ee1 = ee - easy.begin();         auto hh = upper_bound(hard.begin(), hard.end(), i);         long long int hh1;         if (hh == hard.end())           hh1 = hard.size();         else           hh1 = hh - hard.begin();         long long int mandt = ee1 * a + hh1 * b;         long long int temp = 0;         if (mandt <= i) temp = ee1 + hh1;         long long int rem_time = i - mandt;         long long int rem_easy = easy.size() - ee1;         long long int rem_hard = hard.size() - hh1;         temp += min(rem_easy, rem_time / a);         rem_time -= min(rem_easy, rem_time / a) * a;         temp += min(rem_hard, rem_time / b);         ans = max(ans, temp);       }       cout << ans << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   vector<long long> D0, D1;   vector<vector<pair<long long, long long> > > G;   bool comp(pair<long long, long long> a, pair<long long, long long> b) {     return a.second - a.first > b.second - b.first;   }   void dfs(int x, int k, int p = -1) {     vector<pair<long long, long long> > F;     for (auto it : G[x]) {       int y = it.first;       if (y == p) continue;       dfs(y, k, x);       F.push_back(pair<long long, long long>(D0[y], it.second + D1[y]));     }     sort(F.begin(), F.end(), comp);     for (int i = 0; i < (int)F.size(); ++i) {       if (i < k - 1 and F[i].second > F[i].first)         D1[x] += F[i].second;       else         D1[x] += F[i].first;       if (i < k and F[i].second > F[i].first)         D0[x] += F[i].second;       else         D0[x] += F[i].first;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int q;     cin >> q;     while (q--) {       int n, k;       cin >> n >> k;       G = vector<vector<pair<long long, long long> > >(n);       D0 = D1 = vector<long long>(n, 0);       for (int i = 0; i < n - 1; ++i) {         int x, y, k;         cin >> x >> y >> k;         --x;         --y;         G[x].push_back(pair<long long, long long>(y, k));         G[y].push_back(pair<long long, long long>(x, k));       }       dfs(0, k);       cout << D0[0] <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   struct line {     long long int a, b, c;   };   int main() {     long long int x1, y1, x2, y2;     cin >> x1 >> y1 >> x2 >> y2;     long long int n;     cin >> n;     vector<line> data(n);     for (auto &p : data) {       cin >> p.a >> p.b >> p.c;     }     vector<bool> s1(n), s2(n);     for (int i = 0; i < n; ++i) {       s1[i] = data[i].a * x1 + data[i].b * y1 + data[i].c > 0;       s2[i] = data[i].a * x2 + data[i].b * y2 + data[i].c > 0;     }     int cnt = 0;     for (int i = 0; i < n; ++i) {       cnt += s1[i] ^ s2[i];     }     cout << cnt << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, k;   long long a[100005];   bool moze(long long x) {     long long st[100005];     long long curr = 0;     for (long long i = 0; i < n; i++) {       long long t = x - a[i];       if (t < 0) t = 0;       if (i >= k) curr -= st[i - k];       st[i] = t - curr;       if (st[i] < 0) st[i] = 0;       curr += st[i];     }     long long res = 0;     for (long long i = 0; i < n; i++) res += st[i];     return res <= m;   }   void solve() {     long long l = 1;     long long r = 1000100000;     long long x;     while (l <= r) {       long long m = (l + r) / 2;       if (moze(m)) {         l = m + 1;         x = m;       } else         r = m - 1;     }     printf( %lld , x);   }   int main() {     scanf( %lld%lld%lld , &n, &m, &k);     for (int i = 0; i < n; i++) scanf( %lld , &a[i]);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const long long INF = 1e18;   const int MAXN = 2e5 + 5;   const int MAXM = 9e4 + 500;   const int N = 300 + 10;   const double EPS = 1e-5;   int i, j, d;   int n, k, m, p;   int a[N][N], dp[2][N][N], last[N][N];   vector<pair<int, int> > grid[MAXM];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> m >> p;     for (i = 1; i <= n; i++) {       for (j = 1; j <= m; j++) {         cin >> a[i][j];         grid[a[i][j]].push_back({i, j});       }     }     int ans = 1e9;     for (int now = 1; now <= p; now++) {       for (auto g : grid[now]) {         int first = g.first;         int second = g.second;         int dist = 1e9;         for (i = 1; i <= n; i++) {           if (last[i][second] == now - 1)             dist = min(dist, dp[1][i][second] + abs(first - i));         }         dp[0][first][second] =             (now == 1 ? abs(first - 1) + abs(second - 1) : dist);         if (now == p) ans = min(ans, dist);       }       for (auto g : grid[now]) {         int first = g.first;         int second = g.second;         for (i = 1; i <= m; i++) {           if (last[first][i] < now ||               last[first][i] == now &&                   dp[0][first][second] + abs(second - i) < dp[1][first][i]) {             last[first][i] = now;             dp[1][first][i] = dp[0][first][second] + abs(second - i);           }         }       }     }     cout << ans;     return 0;   }