malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long long C = 1000000007;   long long pow1(long long x, long long y, long long z) {     if (y == 0) {       return 1;     }     if (y % 2 == 0) {       return pow1(x * x % z, y / 2, z);     }     return pow1(x, y - 1, z) * x % z;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     long long n, k, t = 0, r = 0;     cin >> n >> k;     for (long long q = 0; q < n; q++) {       long long w;       cin >> w;       if (w == 50) {         t++;       } else {         r++;       }     }     long long c[51][51];     for (long long q = 0; q <= 50; q++) {       c[0][q] = 1;       for (long long q1 = 1; q1 <= q; q1++) {         c[q1][q] = c[q1 - 1][q] * (q - q1 + 1) % C * pow1(q1, C - 2, C) % C;       }     }     long long d[5 * n + 2][t + 1][r + 1];     for (long long q = 0; q < 5 * n + 2; q++) {       for (long long q1 = 0; q1 <= t; q1++) {         fill(d[q][q1], d[q][q1] + r + 1, 0);       }     }     d[0][0][0] = 1;     bool flag = true;     for (long long q = 0; q <= 5 * n; q++) {       if (q % 2 == 1) {         for (long long q1 = 0; q1 <= t; q1++) {           for (long long q2 = 0; q2 <= r; q2++) {             d[q][q1][q2] %= C;             for (long long q3 = 0; q3 <= q1; q3++) {               for (long long q4 = 0; q4 <= q2; q4++) {                 if ((q1 - q3) * 50 + (q2 - q4) * 100 <= k && q1 - q3 + q2 - q4) {                   d[q + 1][q3][q4] +=                       d[q][q1][q2] * c[q1 - q3][q1] % C * c[q2 - q4][q2] % C;                 }               }             }           }         }       } else {         for (long long q1 = 0; q1 <= t; q1++) {           for (long long q2 = 0; q2 <= r; q2++) {             d[q][q1][q2] %= C;             for (long long q3 = q1; q3 <= t; q3++) {               for (long long q4 = q2; q4 <= r; q4++) {                 if ((q3 - q1) * 50 + (q4 - q2) * 100 <= k && q3 - q1 + q4 - q2) {                   d[q + 1][q3][q4] += d[q][q1][q2] * c[q3 - q1][t - q1] % C *                                       c[q4 - q2][r - q2] % C;                 }               }             }           }         }       }       if (d[q][t][r] > 0) {         cout << q <<   n  << d[q][t][r] <<   n ;         flag = false;         break;       }     }     if (flag) {       cout << -1 <<   n  << 0 <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void init_code() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   int main() {     init_code();     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       vector<int> arr(n + 1, 0);       map<int, int> mp;       for (int i = 1; i <= n; ++i) {         cin >> arr[i];         mp[arr[i]] += 1;       }       int res = 0, shifts = 0;       for (int i = 1; i <= n; ++i) {         if (arr[i] > i + shifts) {           int diff = arr[i] - i - shifts;           res += diff;           shifts += diff;         }       }       cout << res <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int mod = 1000000007;   void __print(long long int x) { cerr << x; }   void __print(float x) { cerr << x; }   void __print(double x) { cerr << x; }   void __print(long double x) { cerr << x; }   void __print(char x) { cerr <<      << x <<     ; }   void __print(const char *x) { cerr <<     << x <<    ; }   void __print(const string &x) { cerr <<     << x <<    ; }   void __print(bool x) { cerr << (x ?  true  :  false ); }   template <typename T, typename V>   void __print(const pair<T, V> &x) {     cerr <<  { ;     __print(x.first);     cerr <<  , ;     __print(x.second);     cerr <<  } ;   }   template <typename T>   void __print(const T &x) {     long long int f = 0;     cerr <<  { ;     for (auto &i : x) cerr << (f++ ?  ,  :   ), __print(i);     cerr <<  } ;   }   void _print() { cerr <<  ] n ; }   template <typename T, typename... V>   void _print(T t, V... v) {     __print(t);     if (sizeof...(v)) cerr <<  ,  ;     _print(v...);   }   void solve() {     long long int n;     cin >> n;     vector<long long int> arr(n);     for (long long int i = 0; i < n; i++) cin >> arr[i];     long long int l = n, r = -1;     for (long long int i = 0; i < n; i++) {       if (arr[i] == 1) {         l = i;         break;       }     }     for (long long int i = n - 1; n - 1 < -1 ? i < -1 : i > -1;          n - 1 < -1 ? i += 1 : i -= 1) {       if (arr[i] == 1) {         r = i;         break;       }     }     long long int ans = 0;     for (long long int i = l; l < r + 1 ? i < r + 1 : i > r + 1;          l < r + 1 ? i += 1 : i -= 1) {       if (arr[i] == 0) {         ans++;       }     }     cout << ans << endl;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int t = 1;     cin >> t;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   T sqr(T a) {     return (a) * (a);   }   template <class T>   T abs(T a) {     return (a < 0) ? -(a) : (a);   }   const double Pi = acos(-1.0);   const double eps = 1e-10;   const int INF = 1000 * 1000 * 1000;   const double phi = 0.5 + sqrt(1.25);   int main() {     string Less =  z , More =    , tmp =   ;     int n;     scanf( %d n , &n);     for (int i = 0, _n = (n)-1; i <= _n; ++i) {       getline(cin, tmp);       Less = min(Less, tmp);       More = max(More, tmp);     }     int i = 0, m = ((int)((Less).size()));     bool coincide = 1;     while (i < m && coincide)       if (Less.substr(0, i + 1) == More.substr(0, i + 1))         i++;       else         coincide = 0;     printf( %d n , i);     return 0;   }
#include <bits/stdc++.h>   const long long mod = 1e9 + 7;   const long long LL_MAX = 9223372036854775807;   using namespace std;   int a[100005];   int main() {     int n;     scanf( %d , &n);     for (int i = 0; i < 2 * n; i++) scanf( %d , &a[i]);     int ans = 0;     for (int i = 0; i < 2 * n; i++) {       if (a[i] == 0) {         continue;       }       if (a[i] != a[i + 1]) {         int t = 0, j;         for (j = i + 1; j < 2 * n && a[j] != a[i]; j++) {           if (a[j] != 0) t++;         }         a[j] = 0;         ans += t;       } else         i++;     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int record[300000];   int main() {     map<pair<int, int>, int> m;     memset(record, 0, sizeof(record));     int n, p;     scanf( %d%d , &n, &p);     map<pair<int, int>, int>::iterator iter;     for (int i = 0; i < n; i++) {       int a, b;       scanf( %d%d , &a, &b);       record[a]++;       record[b]++;       if (a > b)         m[make_pair(b, a)]++;       else         m[make_pair(a, b)]++;     }     long long ans = 0;     for (iter = m.begin(); iter != m.end(); iter++) {       int x = (iter->first).first;       int y = (iter->first).second;       if (record[x] + record[y] - iter->second < p && record[x] + record[y] >= p)         ans--;     }     sort(record + 1, record + n + 1);     int ed = n;     for (int st = 1; st <= n; st++) {       while (record[st] + record[ed] >= p && ed > st) ed--;       ans += min(n - ed, n - st);     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long fastpow(int a, int b, int MOD) {     long long x = 1, y = a;     while (b > 0) {       if (b % 2 == 1) {         x = (x * y) % MOD;       }       y = (y * y) % MOD;       b /= 2;     }     return x;   }   long long InverseEuler(int n, int MOD) { return fastpow(n, MOD - 2, MOD); }   long long f[3];   bool init;   long long C(int n, int r, int MOD) {     if (!init) {       init = 1;       f[0] = 1;       for (int i = 1; i < 300000; i++) f[i] = (f[i - 1] * i) % MOD;     }     return (f[n] *             ((InverseEuler(f[r], MOD) * InverseEuler(f[n - r], MOD)) % MOD)) %            MOD;   }   int N;   string s[77000];   unordered_map<int, int> ct[9];   unordered_map<int, int> loc[9][77000];   int main() {     std::ios::sync_with_stdio(false);     cin >> N;     for (int i = 0; i < N; i++) {       cin >> s[i];       int M = s[i].length();       for (int j = 0; j < M; j++) {         int tmp = 0;         for (int k = j; k < M; k++) {           tmp = tmp * 10 + (s[i][k] -  0 );           ct[k - j][tmp]++;           loc[k - j][i][tmp]++;         }       }     }     for (int i = 0; i < N; i++) {       int M = s[i].length();       int ln = 10;       int res;       for (int j = 0; j < M; j++) {         int tmp = 0;         for (int k = j; k < M; k++) {           if (k - j >= ln) break;           tmp = tmp * 10 + (s[i][k] -  0 );           if (ct[k - j][tmp] == loc[k - j][i][tmp]) {             res = tmp;             ln = k - j;           }         }       }       string res2 =   ;       while (res) {         res2 +=  0  + (res % 10);         res /= 10;       }       reverse(res2.begin(), res2.end());       while (res2.size() != ln + 1) res2 =  0  + res2;       cout << res2 << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   using namespace chrono;   const int maxn = 1e6;   void solve() {     int n, a[21][21], val;     vector<int> data;     map<int, int> m;     cin >> n;     for (int i = 0; i < n * n; i++) {       cin >> val;       data.push_back(val);       m[val]++;     }     if (n == 1) {       cout <<  YES             <<   n ;       cout << data[0] <<   n ;       return;     }     for (int ii = 0; ii < n; ii++) {       for (int jj = 0; jj < n; jj++) {         a[ii][jj] = 0;       }     }     for (int ii = 0; ii < n / 2; ii++) {       for (int jj = 0; jj < n / 2; jj++) {         int v_f = 0, v_s = 0;         for (auto it = m.begin(); it != m.end(); it++) {           if (it->second >= 4) {             v_f = it->first;             break;           }         }         if (v_f == 0) {           cout <<  NO                 <<   n ;           return;         }         a[ii][jj] = v_f;         a[n - ii - 1][jj] = v_f;         a[ii][n - jj - 1] = v_f;         a[n - ii - 1][n - jj - 1] = v_f;         m[v_f] -= 4;       }     }     if ((n & 1) == 0) {       cout <<  YES             <<   n ;       for (int ii = 0; ii < n; ii++) {         for (int jj = 0; jj < n; jj++) {           cout << a[ii][jj] <<    ;         }         cout <<   n ;       }       cout <<   n ;       return;     }     for (int ii = 0; ii < n / 2; ii++) {       int f = 0;       for (auto it = m.begin(); it != m.end(); it++) {         if (it->second >= 2) {           f = it->first;         }       }       if (f == 0) {         cout <<  NO               <<   n ;         return;       }       a[ii][n / 2] = f;       a[n - ii - 1][n / 2] = f;       m[f] -= 2;     }     for (int ii = 0; ii < n / 2; ii++) {       int f = 0;       for (auto it = m.begin(); it != m.end(); it++) {         if (it->second >= 2) f = it->first;       }       if (f == 0) {         cout <<  NO               <<   n ;         return;       }       a[n / 2][ii] = f;       a[n / 2][n - ii - 1] = f;       m[f] -= 2;     }     int f = 0;     for (auto it = m.begin(); it != m.end(); it++) {       if (it->second == 1) {         f = it->first;       }     }     if ((n & 1) == 1) {       a[n / 2][n / 2] = f;     }     cout <<  YES           <<   n ;     for (int ii = 0; ii < n; ii++) {       for (int jj = 0; jj < n; jj++) {         cout << a[ii][jj] <<    ;       }       cout <<   n ;     }     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = (int)(2e6 + 123);   const int R = (int)(2e6);   const long long inf = (long long)(1e12 + 7);   long long pn, p[maxn], lp[maxn], b[maxn];   bool u[maxn];   void resheto(long long n) {     for (long long i = 3; i * i <= n; i += 2)       if (!u[i])         for (long long j = i * i; j <= n; j += 2 * i) u[j] = 1;     p[pn++] = 2;     for (long long i = 3; i <= n; i += 2)       if (!u[i]) p[pn++] = i;     for (long long i = 0; i < pn; ++i) {       for (long long j = p[i]; j <= n; j += p[i]) lp[j] = p[i];     }   }   long long n, val, x, k, t, ans1;   long long res(long long x) {     long long ans = x;     for (long long mask = 1; mask < (1 << n); ++mask) {       long long cnt = 0, a = 1;       for (long long i = 0; i < n; ++i) {         if ((mask >> i) & 1) {           cnt++;           a *= b[i];         }       }       if (cnt & 1)         ans -= x / a;       else         ans += x / a;     }     return ans - ans1;   }   long long bin(long long l, long long r, long long k) {     while (r - l > 1) {       long long m = (l + r) / 2;       if (res(m) >= k)         r = m;       else         l = m;     }     if (res(l) == k) return l;     return r;   }   int main() {     ios_base ::sync_with_stdio(NULL);     resheto(R);     cin >> t;     for (long long cs = 1; cs <= t; ++cs) {       cin >> x >> val >> k;       n = ans1 = 0;       while (val != 1) {         b[n++] = lp[val];         long long y = lp[val];         while (val % y == 0) val /= y;       }       ans1 = res(x);       cout << bin(x + 1, inf, k) << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e6 + 3;   const int INFI = 1e9 * 2;   const long long LINFI = 1e17;   const double pi = acos(-1.0);   const int N = 111;   const int M = 11;   const int move[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1};   double mabs(double x) { return x > 0 ? x : -x; }   int main() {     int n;     while (scanf( %d , &n) != EOF) {       if (n == 1)         printf( a na nb nb n );       else {         if (n & 1) {           n--;           for (int i = 0; i < n; i++) putchar((i & 2) ?  a  :  b );           puts( c );           for (int i = 0; i < n; i++) putchar((i & 2) ?  d  :  e );           puts( c );           putchar( f );           for (int i = 0; i < n; i++) putchar((i & 2) ?  g  :  h );           printf(  n );           putchar( f );           for (int i = 0; i < n; i++) putchar((i & 2) ?  i  :  j );           printf(  n );         } else {           for (int i = 0; i < n; i++) putchar((i & 2) ?  a  :  b );           printf(  n );           for (int i = 0; i < n; i++) putchar((i & 2) ?  c  :  d );           printf(  n );           n -= 2;           putchar( e );           for (int i = 0; i < n; i++) putchar((i & 2) ?  f  :  g );           puts( h );           putchar( e );           for (int i = 0; i < n; i++) putchar((i & 2) ?  i  :  j );           puts( h );         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, h;     cin >> n >> h;     vector<int> a(n, 0);     copy(istream_iterator<int>(cin), istream_iterator<int>(), a.begin());     vector<int> a_orig = a;     sort(a.begin(), a.end());     int max1 = a[n - 1] + a[n - 2];     int min1 = a[0] + a[1];     int best_sofar = max1 - min1;     vector<int> res(n, 2);     if (n >= 3) {       min1 = min(min1 + h, a[1] + a[2]);       max1 = max(max1, a[0] + a[n - 1] + h);       if (abs(max1 - min1) < best_sofar) {         best_sofar = abs(max1 - min1);         res[distance(a_orig.begin(), min_element(a_orig.begin(), a_orig.end()))] =             1;       }     }     cout << best_sofar << endl;     copy(res.begin(), res.end(), ostream_iterator<int>(cout,    ));     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = (long long)1e18;   const long long mod = (long long)1e9 + 7;   const double eps = (double)1e-9;   const double pi = acos(-1.0);   const int dx[] = {0, 0, 1, 0, -1};   const int dy[] = {0, 1, 0, -1, 0};   const int N = 100500;   void solve() {     int a, b, cnt = 0;     string second;     cin >> a >> b >> second;     vector<int> v;     cnt = 0;     for (int i = 0; i < (int)second.size(); ++i) {       if (second[i] ==  X ) continue;       int j = i;       while (j + 1 < (int)second.size() && second[j + 1] == second[j]) j++;       int len = j - i + 1;       if (len < b) continue;       if (b <= len && len < a) {         cout <<  NO  << endl;         return;       }       if (a <= len && len < 2 * b)         cnt++;       else         v.push_back(len);       i = j;     }     if (v.size() == 0) {       if (cnt % 2 == 0)         cout <<  NO  << endl;       else         cout <<  YES  << endl;       return;     }     if (v.size() > 1) {       cout <<  NO  << endl;       return;     }     int x = v[0];     for (int i = 0; i + a <= x; ++i) {       if ((b <= i && i < a) || (b <= x - a - i && x - a - i < a)) continue;       if (2 * b <= max(i, x - a - i)) continue;       int ncnt = cnt;       if (a <= i && i < 2 * b) ncnt++;       if (a <= x - a - i && x - a - i < 2 * b) ncnt++;       if (ncnt % 2 == 0) {         cout <<  YES  << endl;         return;       }     }     cout <<  NO  << endl;   }   int main() {     cin.tie(NULL);     cout.tie(NULL);     ios_base::sync_with_stdio(false);     int T;     cin >> T;     while (T--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void bringToTen(int &k, int &rating, vector<int> &bucket) {     for (int i = 9; i > 0; i--) {       int x = 0;       while (x < 100) {         int h = min(k / (10 - i), bucket[x + i]);         k -= h * (10 - i);         bucket[x + 10] += h;         rating += h;         bucket[x + i] -= h;         x += 10;       }     }   }   void addToTen(int &k, int &rating, vector<int> &bucket) {     int x = 0;     while (x < 100) {       int h = min(k / 10, bucket[x]);       rating += h;       bucket[x + 10] += h;       bucket[x] -= h;       k -= 10 * h;       x += 10;     }   }   int main(void) {     int n, k, rating = 0, h;     scanf(  %d %d , &n, &k);     vector<int> a(n);     for (int(i) = (0); i < (n); i++) scanf(  %d , &a[i]);     vector<int> num(101, 0);     for (int(i) = (0); i < (n); i++) {       num[a[i]]++;       rating += a[i] / 10;     }     bringToTen(k, rating, num);     addToTen(k, rating, num);     printf( %d n , rating);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int arr[200000], arr2[200000];   int main() {     int n, i;     long long sum = 0;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d , &arr[i]);       arr2[i] = arr[i] + arr2[i - 1];     }     for (i = 0; i < n; i++) {       if (arr[i] == 0) {         sum += arr2[i];       }     }     cout << sum;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 6, M = 3, nBits = 2e8 + 5, MM = (1 << 16), MAX = 1111,             OO = 0x3f3f3f3f, MOD = 1000000007, inf = 1 << 30;   const double PI = acos(-1);   const long long INF = (long long)1e18, MOOD = 1000000007;   long long GCD(long long a, long long b) { return !b ? a : GCD(b, a % b); }   long long LCM(long long x, long long y) { return (x * y / GCD(x, y)); }   long long fact(long long z) { return (z <= 1) ? 1 : z * fact(z - 1); }   void fast() {     std::ios_base::sync_with_stdio(0);     cin.tie(NULL);     cout.tie(NULL);   }   int t, a, b, color[N], n, m, k, x, y, r[5001], c[5001];   int main() {     fast();     cin >> n >> m >> k;     for (int i = 1; i <= k; ++i) {       cin >> a >> b >> color[i];       if (a == 1)         r[b] = i;       else         c[b] = i;     }     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) cout << color[max(r[i], c[j])] <<    ;       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<pair<long long int, long long int> > P;   int n;   bool compare_y(const pair<long long int, long long int>& p1,                  const pair<long long int, long long int>& p2) {     if (p1.second != p2.second) return p1.second < p2.second;     return p1.first < p2.first;   }   long long int d(pair<long long int, long long int> p1,                   pair<long long int, long long int> p2) {     return pow(p1.first - p2.first, 2) + pow(p1.second - p2.second, 2);   }   long long int func(int l, int h,                      vector<pair<long long int, long long int> > Y) {     if (h - l == 1) return d(P[l], P[h]);     if (h - l == 2)       return min(min(d(P[l], P[l + 1]), d(P[l + 1], P[h])), d(P[l], P[h]));     int mid = (l + h) / 2, sz = Y.size();     vector<pair<long long int, long long int> > left, right;     for (int i = 0; i < sz; ++i) {       if (Y[i].first <= P[mid].first)         left.push_back(Y[i]);       else         right.push_back(Y[i]);     }     long long int m = min(func(l, mid, left), func(mid + 1, h, right));     for (int i = 0; i < sz; ++i)       for (int j = i + 1; j < i + 10 && j < sz; ++j)         if (d(Y[i], Y[j]) < m) m = d(Y[i], Y[j]);     return m;   }   int main() {     cin >> n;     vector<pair<long long int, long long int> > Q;     for (int i = 0; i < n; ++i) {       long long int u;       cin >> u;       P.push_back(make_pair(i + 1, u));       Q.push_back(make_pair(i + 1, u));       if (i) P[i].second += P[i - 1].second;       if (i) Q[i].second += Q[i - 1].second;     }     sort(Q.begin(), Q.end(), compare_y);     cout << func(0, n - 1, Q) << endl;   }
#include <bits/stdc++.h>   using namespace std;   signed main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1;     cin >> t;     while (t--) {       long long n, x;       cin >> n >> x;       long long deg[1005] = {};       for (long long i = 1; i < n; i++) {         long long u, v;         cin >> u >> v;         deg[u]++, deg[v]++;       }       if (n == 1 || deg[x] == 1)         cout <<  Ayush n ;       else {         if (n % 2)           cout <<  Ashish n ;         else           cout <<  Ayush n ;       }     }     cerr <<  Time elapsed:   << 1.0 * clock() / CLOCKS_PER_SEC <<   sec  n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1005;   int n, g[21][21], a, t[maxn];   bool ans = true;   int find(int times) {     int i = 1;     bool finded = false;     for (; i <= 1000; ++i)       if (t[i] >= times) {         finded = true;         break;       }     if (!finded) ans = false;     t[i] -= times;     return i;   }   int main() {     cin >> n;     for (int i = 0; i < n * n; i++) {       cin >> a;       t[a]++;     }     for (int i = 1; i <= n / 2; i++)       for (int j = 1; j <= n / 2; j++)         g[i][j] = g[n - i + 1][j] = g[i][n - j + 1] = g[n - i + 1][n - j + 1] =             find(4);     if (ans) {       if (n & 1) {         for (int i = 1; i <= n / 2; i++)           if (i != (n + 1) / 2) {             g[i][(n + 1) / 2] = g[n - i + 1][(n + 1) / 2] = find(2);             g[(n + 1) / 2][i] = g[(n + 1) / 2][n - i + 1] = find(2);           }         g[(n + 1) / 2][(n + 1) / 2] = find(1);       }       if (ans) {         cout <<  YES  << endl;         for (int i = 1; i <= n; i++)           for (int j = 1; j <= n; j++) cout << g[i][j] <<    n [j == n];       }     }     if (!ans) cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, d;   int a[200];   int x[200], y[200];   int dis[200];   vector<pair<int, int> > edge[200];   int bellman_ford() {     for (int i = 0; i < n; ++i) dis[i] = 1000000000;     dis[0] = 0;     for (int i = 1; i < n; ++i) {       for (int j = 0; j < n; ++j) {         for (int k = 0, len = edge[j].size(); k < len; ++k)           dis[edge[j][k].first] =               min(dis[edge[j][k].first], dis[j] + edge[j][k].second);       }     }     return max(0, dis[n - 1]);   }   int D(int i, int j) { return abs(x[i] - x[j]) + abs(y[i] - y[j]); }   int main() {     while (cin >> n >> d) {       a[0] = 0;       a[n - 1] = 0;       for (int i = 1; i < n - 1; ++i) cin >> a[i];       for (int i = 0; i < n; ++i) {         cin >> x[i] >> y[i];       }       for (int i = 0; i < n; ++i)         for (int j = 0; j < n; ++j)           if (i != j) {             edge[i].push_back(make_pair(j, D(i, j) * d - a[i]));           }       cout << bellman_ford() << endl;     }     return 0;   }