malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int a[5005];   int vals[5005];   int n;   long long d[2][5005];   int main() {     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       vals[i] = a[i];     }     sort(vals, vals + n);     memset(d, 0, sizeof(d));     int cur = 0, nw = 1;     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         d[nw][j] = d[cur][j] + abs(a[i] - vals[j]);         if (j > 0) {           d[nw][j] = min(d[nw][j], d[nw][j - 1]);         }       }       swap(cur, nw);     }     long long res = d[cur][0];     for (int i = 0; i < n; i++) {       res = min(res, d[cur][i]);     }     cout << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> A;   int F(int q, int w, int pr) {     if (w - q <= 2) return w - q;     int s = q;     int g = 0;     int ans = 2;     while (s < w) {       int f = s;       int st = 1;       while (st <= (A[s] - pr)) st <<= 1;       st = pr + (st >> 1);       while (f < w && (A[f] & st) == st) f++;       ans = max(ans, F(s, f, st) + g + ((f == w) ? 0 : 1));       s = f;       g++;     }     return ans;   }   int main() {     int n;     scanf( %d , &n);     A.resize(n);     for (int i = 0; i < n; i++) scanf( %d , &A[i]);     sort(A.begin(), A.end());     reverse(A.begin(), A.end());     printf( %d n , n - F(0, n, 0));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const double pi = acosl(-1);   void solve() {     long long n, s, t;     cin >> n >> s >> t;     if (n == s + t) {       cout << max(s, t) + 1 <<   n ;     } else {       if (2 * n == s + t) {         cout << 1 <<   n ;       } else {         if (s > t) {           swap(s, t);         }         long long same = t - (n - s);         cout << max(n - s, s - same) + 1 <<   n ;       }     }   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long tt = 1;     cin >> tt;     while (tt--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e3 + 10;   const int MOD = 1e9 + 7;   long long bit[maxn], n, k;   long long sum(int i) {     long long s = 0;     while (i > 0) {       s += bit[i];       s %= MOD;       i -= i & -i;     }     return s;   }   void add(int i, long long x) {     while (i <= n) {       bit[i] += x;       bit[i] %= MOD;       i += i & -i;     }   }   long long dp[maxn];   long long a[maxn];   long long quick_mod(long long a, long long b) {     long long ans = 1;     a %= MOD;     while (b) {       if (b & 1) {         ans = ans * a % MOD;         b--;       }       b >>= 1;       a = a * a % MOD;     }     return ans;   }   long long C(long long n, long long m) {     if (m > n) return 0;     long long ans = 1;     for (int i = 1; i <= m; i++) {       long long a = (n + i - m) % MOD;       long long b = i % MOD;       ans = ans * (a * quick_mod(b, MOD - 2) % MOD) % MOD;     }     return ans;   }   long long Lucas(long long n, long long m) {     if (m == 0) return 1;     return C(n % MOD, m % MOD) * Lucas(n / MOD, m / MOD) % MOD;   }   long long combine[maxn];   void init_combine() {     for (int i = 1; i <= n; i++) combine[i] = Lucas(k + i - 1, i);   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     dp[1] = a[1];     init_combine();     for (int i = 2; i <= n; i++) {       dp[i] = a[i];       for (int j = 1; j <= i - 1; j++) {         dp[i] += (combine[j] * a[i - j]) % MOD;         dp[i] %= MOD;       }     }     for (int i = 1; i <= n; i++) cout << dp[i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, x, y;     cin >> n >> x >> y;     vector<char> a(n);     for (long long i = 0; i < n; i++) {       cin >> a[i];     }     long long j = 0, p = 0;     while (j < n) {       while (a[j] == 48 && j < n) {         j++;       }       p++;       while (a[j] == 49 && j < n) {         j++;       }     }     if (a[0] == 49) {       p--;     }     if (p == 0) {       cout <<  0  << endl;       return 0;     }     cout << (p - 1) * min(x, y) + y << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, i, j, k, x, y, z;   int f[100005];   int main() {     scanf( %d%d , &n, &m);     for (; m; --m) {       scanf( %d%d%d , &x, &y, &z);       k = 7;       k -= f[x];       k -= f[y];       k -= f[z];       if (!f[x]) f[x] = k & -k, k -= k & -k;       if (!f[y]) f[y] = k & -k, k -= k & -k;       if (!f[z]) f[z] = k & -k, k -= k & -k;     }     for (i = 1; i <= n; ++i) {       if (f[i] == 1)         printf( 1 );       else if (f[i] == 2)         printf( 2 );       else         printf( 3 );       if (i == n)         printf(  n );       else         printf(   );     }   }
#include <bits/stdc++.h>   using namespace std;   const int m = 50;   struct M {     double v[60][60];     M() { memset(v, 0, sizeof(v)); }     double *operator[](const int &a) { return v[a]; }     M operator*(const M &a) const {       M b;       int i, j, k;       for (i = 0; i <= m; i++)         for (j = 0; j <= m; j++)           for (k = 0; k <= m; k++) b.v[i][j] += v[i][k] * a.v[k][j];       return b;     }   } S, T;   double p, a[60][60], b[60][60], f[60][60];   int n;   inline void pm(int y) {     while (y) {       if (y & 1) S = S * T;       T = T * T, y >>= 1;     }   }   int main() {     int i, j, k, t;     double tmp;     scanf( %d%d , &n, &t), p = t * 1e-9;     a[1][1] = p, a[1][2] = 1 - p;     b[1][2] = 1 - p;     for (i = 2; i <= m; i++) {       a[i][1] = p, a[i][2] = 1 - p, b[i][2] = 1 - p;       for (j = 2; j <= m; j++)         a[i][j] += a[i][j - 1] * a[i - 1][j - 1],             b[i][j] += b[i][j - 1] * a[i - 1][j - 1];     }     for (i = m; i >= 1; i--)       for (j = 1; j <= m; j++)         a[i][j] *= 1 - a[i - 1][j], b[i][j] *= 1 - a[i - 1][j];     f[1][1] = 1, f[1][2] = 2;     for (i = 2; i <= m; i++) {       for (j = 2; j <= m; j++) {         tmp = 0;         for (k = 1; k < j; k++)           f[i][j] += f[i - 1][k] * a[i - 1][k], tmp += a[i - 1][k];         f[i][j] = f[i][j] / tmp + j;       }       tmp = 0;       for (k = 2; k <= m; k++)         f[i][1] += f[i - 1][k] * b[i - 1][k], tmp += b[i - 1][k];       f[i][1] = f[i][1] / tmp + 1;     }     if (n <= m) {       tmp = 0;       for (i = 1; i <= n + 1; i++) tmp += f[n][i] * a[n][i];       printf( %.12lf , tmp);       return 0;     }     S[0][0] = T[0][0] = 1;     for (i = 2; i <= m; i++) {       tmp = 0;       for (j = 1; j < i; j++) T[j][i] += a[m][j], tmp += a[m][j];       for (j = 1; j < i; j++) T[j][i] /= tmp;       T[0][i] = i;     }     tmp = 0;     for (i = 2; i <= m; i++) T[i][1] += b[m][i], tmp += b[m][i];     for (i = 2; i <= m; i++) T[i][1] /= tmp;     T[0][1] = 1;     for (i = 1; i <= m; i++) S[0][i] = f[m][i];     pm(n - m);     tmp = 0;     for (i = 1; i <= m; i++) tmp += S[0][i] * a[m][i];     printf( %.12lf , tmp);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, T;   double p[5555];   int t[5555];   double f[5555][5555];   int main() {     cin >> n >> T;     for (int i = 0; i < n; i++) {       cin >> p[i] >> t[i];       p[i] /= 100;     }     for (int x = 1; x < t[n - 1]; x++)       f[n - 1][x] = f[n - 1][x - 1] + p[n - 1] * pow(1 - p[n - 1], x - 1);     f[n - 1][t[n - 1]] = f[n - 1][t[n - 1] - 1] + pow(1 - p[n - 1], t[n - 1] - 1);     for (int x = t[n - 1] + 1; x <= T; x++) f[n - 1][x] = 1;     for (int i = n - 2; i >= 0; i--) {       double s = 0;       for (int x = 1; x <= T; x++) {         s *= (1 - p[i]);         s += (f[i + 1][x - 1] + 1) * p[i];         if (x >= t[i])           s -= (f[i + 1][x - t[i]] + 1) * p[i] * pow(1 - p[i], t[i] - 1);         if (x >= t[i])           f[i][x] = s + (f[i + 1][x - t[i]] + 1) * pow(1 - p[i], t[i] - 1);         else           f[i][x] = s;       }     }     cout << setprecision(15) << f[0][T] <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   const int mod = 1e9 + 9;   int Case = 1, n, m;   char ss[maxn][maxn];   int dr[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};   int vis[maxn][maxn][5][5];   struct node {     int x, y, d, cnt;     node(int x = 0, int y = 0, int d = 0, int cnt = 0) {       this->x = x;       this->y = y;       this->d = d;       this->cnt = cnt;     }   } s;   void bfs() {     queue<node> A;     for (int i = 0; i < 4; i++) {       int nx = s.x + dr[i][0];       int ny = s.y + dr[i][1];       if (nx >= 0 && nx < n && ny >= 0 && ny < m && ss[nx][ny] !=  * ) {         vis[nx][ny][i][2] = 1;         A.push(node(nx, ny, i, 2));       }     }     while (!A.empty()) {       node temp = A.front();       A.pop();       if (ss[temp.x][temp.y] ==  T ) {         printf( YES n );         return;       }       for (int i = 0; i < 4; i++) {         int nx = temp.x + dr[i][0];         int ny = temp.y + dr[i][1];         if (nx >= 0 && nx < n && ny >= 0 && ny < m && ss[nx][ny] !=  * ) {           if (temp.d != i) {             if (temp.cnt <= 0) continue;             if (!vis[nx][ny][i][temp.cnt - 1]) {               vis[nx][ny][i][temp.cnt - 1] = 1;               A.push(node(nx, ny, i, temp.cnt - 1));             }           } else {             if (!vis[nx][ny][i][temp.cnt]) {               vis[nx][ny][i][temp.cnt] = 1;               A.push(node(nx, ny, i, temp.cnt));             }           }         }       }     }     printf( NO n );   }   void solve() {     scanf( %d%d , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %s , ss[i]);     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         if (ss[i][j] ==  S ) s.x = i, s.y = j;       }     }     bfs();   }   int main() {     ios::sync_with_stdio(false);     while (Case--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int read() {     int x = 0, flag = 1;     char ch = getchar();     while (!isdigit(ch)) {       if (ch ==  - ) flag = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 3) + (x << 1) + (ch ^ 48);       ch = getchar();     }     return x * flag;   }   int n;   int s[109];   int main() {     n = read();     for (int i = (1), i_end_ = (n); i <= i_end_; ++i) {       char c = getchar();       if (c ==  U ) s[i] = 1;       if (c ==  R ) s[i] = 2;     }     int ans = 0, i = 1;     while (i <= n) {       if (s[i] == 1)         if (s[i + 1] == 2)           ++ans, i = i + 2;         else           i++;       else if (s[i] == 2)         if (s[i + 1] == 1)           ++ans, i = i + 2;         else           i++;     }     printf( %d n , n - ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 10;   const int base = 31337;   const int mod = 1e9 + 7;   const int inf = 0x3f3f3f3f;   const int logo = 20;   const int off = 1 << logo;   const int treesiz = off << 1;   int n;   int niz[maxn];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %d , niz + i);       niz[i] *= 2;     }     int sum = 0;     for (int i = 0; i < n; i++) sum += niz[i];     if (sum >= 9 * n)       printf( 0 );     else {       sort(niz, niz + n);       for (int i = 0; i < n; i++) {         sum += 10 - niz[i];         if (sum >= 9 * n) {           printf( %d n , i + 1);           break;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100010;   int n, m;   vector<long long> g[N];   vector<long long> rg[N];   vector<long long> vs;   bool used[N];   int cmp[N], cmpsize[N];   void add_edge(int from, int to) {     g[from].push_back(to);     rg[to].push_back(from);   }   void dfs(int v) {     used[v] = 1;     for (int i = 0; i < (((int)(g[v]).size())); i++) {       if (!used[g[v][i]]) dfs(g[v][i]);     }     vs.push_back(v);   }   void rdfs(int v, int k) {     used[v] = 1;     cmp[v] = k;     for (int i = 0; i < (((int)(rg[v]).size())); i++) {       if (!used[rg[v][i]]) rdfs(rg[v][i], k);     }   }   int scc() {     memset(used, 0, sizeof(used));     vs.clear();     for (int v = 0; v < (n); v++) {       if (!used[v]) dfs(v);     }     memset(used, 0, sizeof(used));     int k = 0;     for (int i = ((int)(vs).size()) - 1; i >= 0; i--) {       if (!used[vs[i]]) rdfs(vs[i], k++);     }     return k;   }   int dfs2(int v) {     used[v] = 1;     int res = cmpsize[cmp[v]] == 1;     for (int i = 0; i < (((int)(g[v]).size())); i++)       if (!used[g[v][i]]) {         res &= dfs2(g[v][i]);       }     for (int i = 0; i < (((int)(rg[v]).size())); i++)       if (!used[rg[v][i]]) {         res &= dfs2(rg[v][i]);       }     return res;   }   int main() {     cin.tie(0);     ios_base::sync_with_stdio(0);     cin >> n >> m;     for (int i = 0; i < (m); i++) {       int a, b;       cin >> a >> b;       add_edge(a - 1, b - 1);     }     scc();     memset(cmpsize, 0, sizeof(cmpsize));     for (int i = 0; i < (n); i++) {       cmpsize[cmp[i]]++;     }     memset(used, 0, sizeof(used));     int ans = n;     for (int i = 0; i < (n); i++)       if (!used[i]) {         ans -= dfs2(i);       }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     string s;     cin >> s;     int n = s.size();     int a = 0, b = 0, c = 0;     for (int i = 0; i < n; ++i) {       a += (s[i] ==  a );       b += (s[i] ==  b );       c += (s[i] ==  c );     }     if (a == 0 || b == 0 || c == 0) {       cout <<  NO n ;       return 0;     }     if (c != a && c != b) {       cout <<  NO n ;       return 0;     }     int lasta = -1, lastb = -1, lastc = -1;     for (int i = 0; i < n; ++i) {       if (s[i] ==  a ) lasta = max(lasta, i);       if (s[i] ==  b ) lastb = max(lastb, i);       if (s[i] ==  c ) lastc = max(lastc, i);     }     if (!(lasta < lastb && lastb < lastc)) {       cout <<  NO n ;       return 0;     }     for (int i = 0; i <= lasta; ++i) {       if (s[i] !=  a ) {         cout <<  NO n ;         return 0;       }     }     for (int i = lasta + 1; i <= lastb; ++i) {       if (s[i] !=  b ) {         cout <<  NO n ;         return 0;       }     }     for (int i = lastb + 1; i <= lastc; ++i) {       if (s[i] !=  c ) {         cout <<  NO n ;         return 0;       }     }     cout <<  YES n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int t, n, lenn;   long long int last(long long int x) {     long long int tr = x;     lenn = 0;     while (x > 0) {       tr = x % 10;       x /= 10;       ++lenn;     }     return tr;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> t;     long long int x, lllll;     while (t--) {       cin >> n;       lllll = last(n);       if (lenn == 1) {         x = 1;       } else if (lenn == 2) {         x = 3;       } else if (lenn == 3) {         x = 6;       } else {         x = 10;       }       cout << x + 10 * (lllll - 1) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using LL = long long;   template <typename T>   using V = vector<T>;   template <typename T, typename S>   using P = pair<T, S>;   template <class T>   using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>;   using LD = long double;   template <typename T, typename S>   void check(T& a, S& b) {     if (a >= b) {       a %= b;     }   }   template <typename T>   T gcd(T u, T v) {     if (u == v) return u;     if (u == 0) return v;     if (v == 0) return u;     if (~u & 1) {       if (v & 1)         return gcd(u >> 1, v);       else         return gcd(u >> 1, v >> 1) << 1;     }     if (~v & 1) return gcd(u, v >> 1);     if (u > v) return gcd((u - v) >> 1, v);     return gcd((v - u) >> 1, u);   }   LL mulmod(LL a, LL b, LL m) {     check(a, m);     check(b, m);     LL q = (LL)(((LD)a * (LD)b) / (LD)m);     LL r = a * b - q * m;     if (r > m) r %= m;     if (r < 0) r += m;     return r;   }   template <typename T, typename S>   T expo(T e, S n) {     T x = 1, p = e;     while (n) {       if (n & 1) x = x * p;       p = p * p;       n >>= 1;     }     return x;   }   template <typename T>   T powerL(T e, T n, T m) {     T x = 1, p = e;     while (n) {       if (n & 1) x = mulmod(x, p, m);       p = mulmod(p, p, m);       n >>= 1;     }     return x;   }   const int N = 200010;   const LL mod = (LL)1e9 + 7;   LL f[N], inv[N];   void pre() {     f[0] = inv[0] = f[1] = inv[1] = 1;     for (LL i = 2; i < N; i++) {       f[i] = mulmod(f[i - 1], i, mod);       inv[i] = powerL(f[i], mod - 2, mod);     }     return;   }   LL ncr(LL n, LL r) {     LL ans = mulmod(f[n], inv[n - r], mod);     return mulmod(ans, inv[r], mod);   }   LL solve(V<LL>& a) {     LL n = (LL)a.size();     LL x = (n >> 1) - 1;     V<LL> co;     for (LL i = 0; i < (LL)(x / 2) + 1; i++) {       LL t = ncr(x, i);       co.push_back(t);       co.push_back(t);     }     if ((LL)co.size() * 2 > n) co.pop_back();     auto temp = co;     reverse(temp.begin(), temp.end());     for (LL& i : temp) co.push_back(i);     if (n % 4 == 0) {       for (LL i = 1; i < n; i += 2) co[i] = -co[i];     }     LL ans = 0;     for (LL i = 0; i < (LL)n; i++) {       LL t = mulmod(a[i], co[i], mod);       ans = (ans + t);       ans %= mod;       ans = (ans + mod) % mod;     }     return ans;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cerr.tie(NULL);     ;     pre();     LL n;     cin >> n;     V<LL> a(n);     for (LL i = 0; i < (LL)n; i++) {       cin >> a[i];     }     if (n == 1) {       cout << a[0] <<   n ;       return 0;     }     if (n & 1) {       V<LL> t;       for (LL i = 0; i < (LL)n - 1; i++) {         t.push_back(a[i] + (2 * (!(i & 1)) - 1) * a[i + 1]);       }       a = t;     }     cout << solve(a) <<   n ;     cerr <<  Time elapsed :  << clock() * 1000.0 / CLOCKS_PER_SEC <<   ms           <<   n ;     return 0;   }
#include <bits/stdc++.h>   int a[100 + 10];   int main() {     int n, m, min_, max_;     scanf( %d %d %d %d , &n, &m, &min_, &max_);     int i, j, t = 0;     for (i = 0; i < m; i++) {       scanf( %d , &a[i]);     }     for (i = 0; i < m; i++) {       for (j = i + 1; j < m; j++) {         if (a[j] < a[i]) {           t = a[i];           a[i] = a[j];           a[j] = t;         }       }     }     if (a[0] < min_ || a[m - 1] > max_)       printf( Incorrect n );     else if (a[0] == min_ && a[m - 1] == max_)       printf( Correct n );     else if (a[0] != min_ && a[m - 1] != max_) {       if (n - m >= 2)         printf( Correct n );       else         printf( Incorrect n );     } else {       if (n - m >= 1)         printf( Correct n );       else         printf( Incorrect n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long spf[101];   long long arr[100001];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long n;     cin >> n;     long long sum = 0;     for (int i = 0; i < n; i++) {       cin >> arr[i];       sum += arr[i];     }     long long sum1 = sum;     sort(arr, arr + n);     for (int i = 0; i < n; i++) {       for (long long j = 1; j <= arr[i]; j++) {         if (arr[i] % j == 0) {           long long sum2 = sum + arr[0] * (j - 1) + arr[i] / j - arr[i];           sum1 = min(sum2, sum1);         }       }     }     cout << sum1;     return 0;   }
/** try <3 **/   #include<bits/stdc++.h>      using namespace std;      #define task  sol    #define lb lower_bound   #define ub upper_bound   #define fi first   #define se second   #define pb push_back   #define mp make_pair   #define zs(v) ((int)(v).size())   #define BIT(x, i) (((x) >> (i)) & 1)   #define CNTBIT __builtin_popcountll   #define ALL(v) (v).begin(),(v).end()   #define endl   n       typedef long double ld;   typedef long long ll;   typedef pair<int, int> pii;      const int dx[4]={-1, 0, 1, 0}, dy[4]={0, 1, 0, -1};   const int dxx[8]={-1, -1, 0, 1, 1, 1, 0, -1}, dyy[8]={0, 1, 1, 1, 0, -1, -1, -1};      const ll mod=998244353 ; /// 998244353   const int base=311;   const int N=2e5+5;      int n,k;   int a[N],pos[N];   ll gt[N];      ll binpow(ll n, ll k)   {       if(k==0) return 1;       ll t=binpow(n,k/2);       t=t*t%mod;       if(k&1) t=t*n%mod;       return t;   }   int C(int k, int n)   {       if(k>n) return 0;       return gt[n]*binpow(gt[k]*gt[n-k]%mod,mod-2)%mod;   }   void add(int &a, int b)   {       a+=b;       if(a>=mod) a-=mod;   }   void gogo()   {       cin>>n>>k;       for(int i=1;i<=n;++i)       {           cin>>a[i];           a[i]++;           pos[a[i]]=i;       }       gt[0]=1;       for(int i=1;i<=N-5;++i) gt[i]=gt[i-1]*i%mod;       int cnt=1;       for(int i=1;i<n;++i)       {   //        cout<<a[i]<<   <<a[i+1]<<   <<pos[a[i]+1]<<   <<pos[a[i+1]+1]<<  n ;           if(pos[a[i]+1]>pos[a[i+1]+1])           {               cnt++;           }       }   //    cout<<cnt<<endl;       int ans=0;       for(int i=cnt;i<=min(n,k);++i)       {           /// choose i-cnt such that it will not be equal           add(ans,(ll)C(i,k)*C(i-cnt,n-cnt)%mod);       }       cout<<ans;   }   int main()   {       ios_base::sync_with_stdio(0); cin.tie(0);       if(fopen(task .inp ,  r ))       {           freopen(task .inp ,  r , stdin);           freopen(task .out ,  w , stdout);       }       gogo();   }
#include <bits/stdc++.h>   using ll = long long;   using namespace std;   const int INF = 1e8;   const int N = 2e5 + 2;   int n, m, k;   vector<int> G[N];   int a[N];   bool sp[N];   int distN[N], dist1[N];   int suf[N];   bool cmp(int& v, int& u) { return dist1[v] < dist1[u]; }   void bfsN() {     for (int i = 0; i < n; i++) distN[i] = INF;     distN[n - 1] = 0;     queue<pair<int, int>> q;     q.push({0, n - 1});     while (!q.empty()) {       int v = q.front().second;       int d = q.front().first;       q.pop();       for (auto u : G[v]) {         if (distN[u] > d + 1) {           distN[u] = d + 1;           q.push({distN[u], u});         }       }     }   }   void bfs1() {     for (int i = 0; i < n; i++) dist1[i] = INF;     dist1[0] = 0;     queue<pair<int, int>> q;     q.push({0, 0});     while (!q.empty()) {       int v = q.front().second;       ;       int d = q.front().first;       q.pop();       for (auto u : G[v]) {         if (dist1[u] > d + 1) {           dist1[u] = d + 1;           q.push({dist1[u], u});         }       }     }   }   int getAns() {     int ans = 0;     for (int i = k - 2; i >= 0; i--) ans = max(ans, dist1[a[i]] + 1 + suf[i + 1]);     return min(ans, distN[0]);   }   signed main() {     cin >> n >> m >> k;     for (int i = 0; i < k; i++) {       cin >> a[i], a[i]--;       sp[a[i]] = true;     }     for (int i = 0; i < m; i++) {       int v, u;       cin >> v >> u, v--, u--;       G[v].push_back(u);       G[u].push_back(v);     }     bfsN();     bfs1();     sort(a, a + k, cmp);     suf[k - 1] = distN[a[k - 1]];     for (int i = k - 2; i >= 0; i--) suf[i] = max(suf[i + 1], distN[a[i]]);     cout << getAns() <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2222;   const int INF = 0x3f3f3f3f;   int n, m, k, s;   int mat[2][N][9], cp[9][9];   void tomin(int &a, int b) {     if (a > b) a = b;   }   void tomax(int &a, int b) {     if (a < b) a = b;   }   void work() {     for (int i = 0; i < m; i++)       for (int j = 0; j < k; j++) {         for (int r = i; r < m; r++)           for (int p = 0; p < k; p++) {             tomax(cp[j][p], abs(r - i) + mat[1][i][j] - mat[0][r][p]);             tomax(cp[j][p], abs(r - i) + mat[1][r][p] - mat[0][i][j]);             tomax(cp[p][j], cp[j][p]);           }       }     int d, ans = 0;     scanf( %d , &d);     d--;     for (int i = 1; i < s; i++) {       int x;       scanf( %d , &x);       x--;       tomax(ans, cp[d][x]);       d = x;     }     printf( %d n , ans);   }   int main() {     scanf( %d%d%d%d , &n, &m, &k, &s);     memset(mat[0], INF, sizeof(mat[0]));     memset(mat[1], -1, sizeof(mat[1]));     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         int d;         scanf( %d , &d);         d--;         tomin(mat[0][j][d], i);         tomax(mat[1][j][d], i);       }     work();     return 0;   }