malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, t;   char G[500005];   int solve(int k) {     int last = n, i;     int hs, ss;     hs = ss = 0;     for (i = 1; i <= n; i++) {       if (G[i] ==  H ) hs++;       if (G[i] ==  S ) ss++;       if (G[i] ==  H ) last = i;       if (hs - ss >= k && G[i] !=  . ) last = i;     }     if (hs - ss > k) {       return 0;     }     if (t < last) return 0;     int remT = t - 1;     int pos, val = k, twice, st;     for (pos = 1; pos <= last; pos++) {       if (remT < 0) return 0;       if (G[pos] ==  H ) {         if (val > 0)           val--;         else {           twice = (last - pos) << 1;           if (twice <= remT) {             return 1;           } else {             st = pos;             ss = hs = 0;             for (; pos <= last; pos++) {               if (G[pos] ==  H )                 hs++;               else if (G[pos] ==  S )                 ss++;               if (hs <= ss) break;             }             if (pos > last) {               return 0;             }             twice = (pos - st) * 3;             remT -= twice;             if (remT < 0) return 0;           }         }       } else if (G[pos] ==  S )         val++;       remT--;     }     return 1;   }   int main() {     scanf( %d%d , &n, &t);     scanf( %s , G + 1);     int high = n + 1;     int low = 0;     int mid;     while (low < high) {       mid = (low + high) / 2;       if (solve(mid)) {         high = mid;       } else {         low = mid + 1;       }     }     if (high <= n)       printf( %d , high);     else       printf( -1 );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 0x3f3f3f3f;   const double eps = 1e-6;   const long long mod = 998244353;   inline string getstr(string &s, long long l, long long r) {     string ret =   ;     for (long long i = l; i <= r; i++) ret.push_back(s[i]);     return ret;   }   long long modpow(long long x, long long y, long long md = mod) {     if (y == 0) return 1;     long long ret = modpow(x, y >> 1, md);     ret = (long long)ret * ret % md;     if (y & 1) ret = (long long)ret * x % md;     return ret;   }   string s;   vector<pair<long long, long long> > g[2005];   long long value[2005];   long long LEN = 0;   bool error = 0;   void add_edge(long long x, long long y, long long cost) {     g[x].push_back(make_pair(y, cost));     g[y].push_back(make_pair(x, cost));   }   void dfs(long long x) {     if (error) return;     for (auto p : g[x]) {       if (~value[p.first] && value[p.first] != value[x] ^ p.second) error = 1;       if (error) return;       if (~value[p.first]) continue;       value[p.first] = value[x] ^ p.second;       dfs(p.first);     }   }   long long calc(long long len) {     LEN = len;     memset(value, -1, sizeof value);     long long Maxa = (len) / 2;     long long Maxb = ((long long)s.size() - 1) / 2;     swap(Maxa, Maxb);     for (long long i = 0; i <= 2002; i++) g[i].clear();     value[0] = 1;     value[1001] = 1;     for (long long i = 0; i < s.size(); i++) {       if (s[i] ==  ? ) continue;       long long posa = (i <= Maxa ? i : ((long long)s.size() - i - 1));       long long posb = (i <= Maxb ? i : (len - i));       if (i > len) {         if (~value[posa] && value[posa] != s[i] -  0 ) return 0;         value[posa] = s[i] -  0 ;         continue;       }       add_edge(posa, posb + 1001, s[i] -  0 );     }     long long ret = 1;     error = 0;     for (long long i = 1; i <= 2002; i++) {       if (~value[i]) dfs(i);     }     for (long long i = 1; i <= 2002; i++) {       if (((0 <= i && i <= Maxa) || (1001 <= i && i <= 1001 + Maxb)) &&           value[i] == -1) {         value[i] = 0;         (ret *= 2) %= mod;         dfs(i);       }       if (error) return 0;     }     return ret;   }   signed main() {     cin >> s;     reverse(s.begin(), s.end());     long long ans = 0;     for (long long len = 0; len < s.size(); len++) {       (ans += calc(len)) %= mod;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   int main() {     double n, m;     scanf( %lf%lf , &n, &m);     int a[1000], b[1000];     for (int i = 0; i < n; i++) scanf( %d , a + i);     for (int i = 0; i < n; i++) scanf( %d , b + i);     double x;     if (b[0] != 1 && a[0] != 1) {       x = m + m / (b[0] - 1);       for (int i = n - 1; i >= 1; i--) {         if (a[i] == 1 || b[i] == 1) {           printf( -1 );           return 0;         }         x += x / (a[i] - 1);         x += x / (b[i] - 1);       }       x += x / (a[0] - 1);       printf( %.10f , x - m);     } else       printf( -1 );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int x, y;     cin >> x >> y;     int l = 0, r = 0, u = 0, d = 0;     int tx = 0, ty = 0, dir = 0, px = 0, py = 0, k = 0;     while (true) {       px = tx;       py = ty;       if (dir == 0) {         r = tx = r + 1;         dir = 1;       } else if (dir == 1) {         u = ty = u + 1;         dir = 2;       } else if (dir == 2) {         l = tx = l - 1;         dir = 3;       } else if (dir == 3) {         d = ty = d - 1;         dir = 0;       }       int rx1 = min(tx, px), ry1 = min(ty, py);       int rx2 = max(tx, px), ry2 = max(ty, py);       if ((x >= rx1 && x <= rx2) && (y >= ry1 && y <= ry2)) {         cout << k;         return 0;       }       k++;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int a[n];     int mini1 = 0;     for (int i = 0; i < n; i++) {       cin >> a[i];       mini1 = max(mini1, a[i]);     }     int mini2 = 0;     cin >> n;     int b[n];     for (int i = 0; i < n; i++) {       cin >> b[i];       mini2 = max(mini2, b[i]);     }     cout << mini1 <<     << mini2 << endl;   }
#include <bits/stdc++.h>   using namespace std;   void read(int &a) {     a = 0;     char c = getchar();     while (c <  0  || c >  9 ) {       c = getchar();     }     while (c >=  0  && c <=  9 ) {       a = (a << 1) + (a << 3) + (c ^ 48);       c = getchar();     }   }   const int Maxn = 200000;   struct Segment {     int l, r, t;     friend bool operator<(Segment p, Segment q) {       if (p.r == q.r) {         return p.l < q.l;       }       return p.r < q.r;     }   } a[Maxn + 5];   int f[Maxn + 5];   int n;   int d[Maxn << 1 | 5], d_len;   struct Segment_Node {     int lson, rson;     int val, lazy;   } seg[Maxn << 4 | 5];   int Root[2];   int id_tot;   void insert(int l, int r, int x, int &root, int left = 1, int right = d_len) {     if (l > right || r < left) {       return;     }     if (root == 0) {       root = ++id_tot;     }     if (l <= left && r >= right) {       seg[root].val += x;       seg[root].lazy += x;       return;     }     int mid = (left + right) >> 1;     insert(l, r, x, seg[root].lson, left, mid);     insert(l, r, x, seg[root].rson, mid + 1, right);     seg[root].val =         max(seg[seg[root].lson].val, seg[seg[root].rson].val) + seg[root].lazy;   }   int query(int l, int r, int root, int left = 1, int right = d_len) {     if (l > right || r < left) {       return 0;     }     if (root == 0) {       return 0;     }     if (l <= left && r >= right) {       return seg[root].val;     }     int mid = (left + right) >> 1;     return max(query(l, r, seg[root].lson, left, mid),                query(l, r, seg[root].rson, mid + 1, right)) +            seg[root].lazy;   }   void update(int x, int a, int &root, int left = 1, int right = d_len) {     if (root == 0) {       root = ++id_tot;     }     a -= seg[root].lazy;     if (left == right) {       seg[root].val = max(seg[root].val, a);       return;     }     int mid = (left + right) >> 1;     if (x <= mid) {       update(x, a, seg[root].lson, left, mid);     } else {       update(x, a, seg[root].rson, mid + 1, right);     }     seg[root].val =         max(seg[seg[root].lson].val, seg[seg[root].rson].val) + seg[root].lazy;   }   int main() {     read(n);     for (int i = 1; i <= n; i++) {       read(a[i].l), read(a[i].r), read(a[i].t);       a[i].t--;       d[++d_len] = a[i].l;       d[++d_len] = a[i].r;     }     sort(d + 1, d + 1 + d_len);     d_len = unique(d + 1, d + 1 + d_len) - d - 1;     for (int i = 1; i <= n; i++) {       a[i].l = lower_bound(d + 1, d + 1 + d_len, a[i].l) - d + 1;       a[i].r = lower_bound(d + 1, d + 1 + d_len, a[i].r) - d + 1;     }     d_len++;     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n; i++) {       f[i] = query(1, a[i].l - 1, Root[a[i].t]) + 1;       insert(1, a[i].l - 1, 1, Root[a[i].t]);       update(a[i].r, f[i], Root[a[i].t ^ 1]);     }     int ans = 0;     for (int i = 1; i <= n; i++) {       ans = max(ans, f[i]);     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int problem, time;     cin >> problem >> time;     int sum = 240;     int j = 0;     for (int i = 1; i <= problem; ++i) {       sum -= i * 5;       if (sum < time) {         break;       }       j++;     }     cout << j;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100 + 20;   const int LG = 15;   int a[N], cnt[N];   map<vector<int>, int> mp;   int main() {     ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> a[i];       cnt[i] = __builtin_popcount(a[i]);     }     for (int mask = 0; mask < (1 << LG); mask++) {       vector<int> vec(n + 20, 0);       for (int i = 0; i < n; i++) {         for (int j = 0; j < LG; j++) {           if (!((mask >> j) & 1)) continue;           vec[i] += (((a[i] >> j) & 1) ? -1 : 1);         }         vec[i] += cnt[i];       }       mp[vec] = mask;     }     for (int mask = 0; mask < (1 << 30); mask += (1 << LG)) {       vector<int> vec(n + 20, 0);       for (int i = 0; i < n; i++) {         for (int j = LG; j < 30; j++) {           if (!((mask >> j) & 1)) continue;           vec[i] += (((a[i] >> j) & 1) ? -1 : 1);         }         vec[i] *= -1;       }       for (int i = 0; i <= 30; i++) {         auto it = mp.find(vec);         if (it != mp.end()) return cout << (mask | it->second) << endl, 0;         for (int j = 0; j < n; j++) vec[j]++;       }     }     cout << -1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 7, maxn = 3e5 + 500;   vector<pair<int, int> > v[maxn];   bool visited[maxn], deg[maxn];   int d[maxn];   set<int> s;   void dfs(int u, int par) {     visited[u] = 1;     int idx;     for (int i = 0; i < v[u].size(); i++) {       int w = v[u][i].first;       if (!visited[w]) dfs(w, u);       if (w == par) idx = v[u][i].second;     }     if (deg[u] != d[u]) {       deg[par] ^= 1;       s.insert(idx);     }   }   int main() {     ios_base::sync_with_stdio(false);     int n, m, cntt = 0, cnt = 0;     cin >> n >> m;     for (int i = 1; i <= n; i++) {       cin >> d[i];       if (d[i] == 1) cnt++;       if (d[i] == -1) cntt++;     }     for (int i = 1; i <= m; i++) {       int u, w;       cin >> u >> w;       v[u].push_back(make_pair(w, i));       v[w].push_back(make_pair(u, i));     }     if (cnt % 2 == 1 and cntt == 0) return cout << -1 << endl, 0;     if (cnt % 2 == 1) {       for (int i = 1; i <= n; i++) {         if (d[i] == -1) {           d[i] = 1;           break;         }       }     }     for (int i = 1; i <= n; i++)       if (d[i] == -1) d[i] = 0;     dfs(1, -1);     cout << s.size() << endl;     for (auto it : s) cout << it <<    ;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> print;   const int Maxn = int(1e2) + 10;   int primes[20] = {2,  3,  5,  7,  11, 13, 17, 19, 23,                     29, 31, 37, 41, 43, 47, 53, 59};   int n, a[Maxn], xrn[60 + 10], dp[Maxn][(1 << 18) + 1000],       adad[Maxn][(1 << 18) + 1000];   void xr() {     for (int(i) = (1); (i) < (60); ++(i)) {       for (int(j) = (0); (j) < (17); ++(j))         if (i % primes[j] == 0) xrn[i] |= (1 << j);     }   }   int main() {     xr();     cin >> n;     for (int(i) = (1); (i) < (n + 1); ++(i)) cin >> a[i];     for (int(i) = (0); (i) < (n + 1); ++(i))       for (int(j) = (0); (j) < ((1 << 17)); ++(j)) dp[i][j] = int(1e9) + 10;     dp[0][0] = 0;     for (int(i) = (1); (i) < (n + 1); ++(i))       for (int(j) = (0); (j) < ((1 << 17)); ++(j))         for (int(m) = (1); (m) < (60); ++(m))           if ((j & xrn[m]) == xrn[m]) {             if (dp[i - 1][j - xrn[m]] + abs(m - a[i]) < dp[i][j])               dp[i][j] = dp[i - 1][j - xrn[m]] + abs(m - a[i]), adad[i][j] = m;           }     int ans = int(1e9) + 1000, ansj = 0;     for (int(j) = (0); (j) < ((1 << 17)); ++(j))       if (dp[n][j] < ans) ans = dp[n][j], ansj = j;     int r = n;     while (r) {       print.push_back(adad[r][ansj]);       ansj -= xrn[adad[r][ansj]];       r--;     }     for (int i = print.size() - 1; i >= 0; --i) cout << print[i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int x[100010];   int main() {     int n, a;     cin >> n >> a;     if (n == 1) {       cout << 0 << endl;       return 0;     }     for (int i = 0; i < n; ++i) {       cin >> x[i];     }     sort(x, x + n);     int l = 1, r = n - 1;     int ans = 0;     ans = min(abs(a - x[r]) + x[r] - x[l], abs(a - x[l]) + x[r] - x[l]);     l = 0, r = n - 2;     ans = min(ans, min(abs(a - x[r]) + x[r] - x[l], abs(a - x[l]) + x[r] - x[l]));     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k, op, i, index = 0;     cin >> n >> k;     char ss[n - 1];     op = k;     i = 97;     while (op--) {       ss[index] = char(i);       i++;       index++;     }     if (n - k != 0) {       for (int d = 0; d < n - k; d++) {         ss[index] = ss[d];         index++;       }     }     for (int c = 0; c < n; c++) cout << ss[c];   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, m, n, rstart, rend, cstart, cend;     scanf( %d %d %d %d , &n, &m, &a, &b);     if ((m >= n) || (m == 1))       printf( 1 n );     else {       rstart = (a / m) + 1;       if ((a % m) == 0) --rstart;       cstart = a - ((rstart - 1) * m);       rend = (b / m) + 1;       if ((b % m) == 0) --rend;       cend = b - ((rend - 1) * m);       if (((cstart == 1) && ((cend == m) || (b == n))) || (rstart == rend) ||           ((a == 1) && (b == n)))         printf( 1 n );       else {         if (((cstart != 1) && ((cend == m) || (b == n))) ||             ((cstart == 1) && (cend != m)))           printf( 2 n );         else if ((rend - rstart) == 1)           printf( 2 n );         else if ((cstart - cend) == 1)           printf( 2 n );         else           printf( 3 n );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, ans, fa[100001];   bool mark[100001];   int find(int x) {     if (x == fa[x]) return x;     return fa[x] = find(fa[x]);   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) fa[i] = i;     for (int i = 1; i <= m; i++) {       int x, y;       cin >> x >> y;       int fx = find(x), fy = find(y);       if (fx != fy) {         fa[fx] = fy;         if (mark[fx] || mark[x] || mark[fy] || mark[y])           mark[fx] = mark[x] = mark[fy] = mark[y] = 1;       } else         mark[fx] = mark[x] = mark[fy] = mark[y] = 1;     }     for (int i = 1; i <= n; i++)       if (find(i) == i && !mark[i]) ans++;     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5;   const int INF = 1e9;   long long MOD = 998244353;   long long sum(long long n) {     long long sm = 0;     while (n) {       sm += n % 10;       n /= 10;     }     return sm;   }   int main() {     list<int> a;     list<int> b;     long long n;     cin >> n;     int ans = 0;     vector<long long> res;     for (long long i = max(n - 100, 0LL); i <= n; i++) {       if (sum(i) + i == n) {         ans++;         res.push_back(i);       }     }     cout << ans <<   n ;     for (int i = 0; i < res.size(); i++) {       cout << res[i] <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[514];   long long int dp[514][514];   long long int mod = 1000000007ll;   long long int mdp(int s, int e) {     if (s == e) return 1;     if (dp[s][e] != -1) return dp[s][e];     long long int& ans = dp[s][e];     if (s + 1 == e) return ans = 1;     ans = mdp(s + 1, e);     for (int i = s + 1; i < e; i++) {       if (a[s] < a[i]) {         ans += mdp(s + 1, i) * mdp(i, e) % mod;       }     }     return ans = ans % mod;   }   int sol() {     memset(dp, -1, sizeof(dp));     int n;     if (scanf( %d , &n) == EOF) return 0;     for (int i = 0; i < n; i++) {       scanf( %d , a + i);     }     printf( %I64d n , n == 1 ? 1 : mdp(1, n));     return 1;   }   int main() {     while (sol())       ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, a, b, Q;   long long W[(500000)], S[(500000)], O[(500000)];   map<pair<int, int>, vector<pair<int, int> > > M;   void pro(int t, int b, long long* g, int& l) {     *g = 0, l = 1;     while (b < N) {       long long S = W[b] + g[l - 1];       g[l++] = S, b += t;     }   }   int main() {     scanf( %d , &N);     for (int i(0); i < N; i++) scanf( %lld , W + i);     scanf( %d , &Q);     for (int i(0); i < Q; i++)       scanf( %d%d , &a, &b), --a, M[{b, a % b}].push_back({a, i});     for (auto& h : M) {       pro(h.first.first, h.first.second, S, a);       for (auto& w : h.second)         O[w.second] = S[a - 1] - S[w.first / h.first.first];     }     for (int i(0); i < Q; i++) printf( %lld n , O[i]);   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   int chkmax(T& a, T b) {     if (b > a) {       a = b;       return 1;     }     return 0;   }   template <class T>   int chkmin(T& a, T b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   template <class iterator>   void output(iterator begin, iterator end, ostream& out = cerr) {     while (begin != end) {       out << (*begin) <<    ;       begin++;     }     out <<   n ;   }   template <class T>   void output(T x, ostream& out = cerr) {     output(x.begin(), x.end(), out);   }   const int MOD = 1e9 + 7;   const int INF = 1e9;   int n, flag, ans = 1;   set<int> buy, sell, middle;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int max_buy = -INF, min_sell = INF;     cin >> n;     for (int i = 0; i < n; ++i) {       int x;       string s;       cin >> s >> x;       if (flag) continue;       if (s ==  ADD ) {         if (x <= max_buy) buy.insert(x);         if (x >= min_sell) sell.insert(x);         if (x > max_buy && x < min_sell) middle.insert(x);       } else {         if (x < max_buy || x > min_sell) {           cout << 0 <<   n ;           flag = 1;           continue;         }         if (x > max_buy && x < min_sell) ans = (ans + ans) % MOD;         for (auto val : middle) {           if (val < x)             buy.insert(val);           else             sell.insert(val);         }         middle.clear();         if (buy.find(x) != buy.end())           buy.erase(x);         else           sell.erase(x);         max_buy = -INF;         min_sell = INF;         if (!buy.empty()) max_buy = (*prev(buy.end()));         if (!sell.empty()) min_sell = (*sell.begin());       }     }     if (flag) return 0;     ans = ((long long)ans * (long long)(middle.size() + 1)) % (long long)MOD;     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, d;     cin >> d >> n;     long long int ilosc = 0;     for (int i = 0; i < n - 1; i++) {       int p;       cin >> p;       ilosc += (d - p);     }     cin >> d;     cout << ilosc <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long fastpow(long long base, long long power, long long M) {     if (power == 1) return base;     long long val = fastpow(base, power / 2, M);     return (power % 2 == 0) ? (val * val) % M : (((val * val) % M) * base) % M;   }   inline void debugMode() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);   }   const int N = 1e5 + 1;   const int MOD = 1e9 + 7;   int main() {     debugMode();     int s, b;     cin >> s >> b;     int arr[s];     for (int i = 0; i < s; i++) cin >> arr[i];     vector<pair<int, int>> base(b);     for (int i = 0; i < b; i++) cin >> base[i].first >> base[i].second;     sort((base).begin(), (base).end());     for (int i = 1; i < b; i++) base[i].second += base[i - 1].second;     for (int i = 0; i < s; i++) {       int left = -1, right = b;       while (left + 1 < right) {         int mid = (left + right) / 2;         if (base[mid].first <= arr[i])           left = mid;         else           right = mid;       }       if (left == -1)         cout << 0 <<    ;       else         cout << base[left].second <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   void cmax(T& a, T b) {     a = max(a, b);   }   template <typename T>   void cmin(T& a, T b) {     a = min(a, b);   }   void _BG(const char* s) {}   template <typename T, typename... TT>   void _BG(const char* s, T a, TT... b) {     for (int c = 0; *s && (c || *s !=  , ); ++s) {       cerr << *s;       switch (*s) {         case  ( :         case  [ :         case  { :           ++c;           break;         case  ) :         case  ] :         case  } :           --c;           break;       }     }     cerr <<   =   << a;     if (*s) {       cerr <<  ,  ;       _BG(++s, b...);     } else       cerr << endl;   }   bool RD(void) { return true; }   bool RD(char& a) { return scanf(  %c , &a) == 1; }   bool RD(char* a) { return scanf( %s , a) == 1; }   bool RD(double& a) { return scanf( %lf , &a) == 1; }   bool RD(int& a) { return scanf( %d , &a) == 1; }   bool RD(long long& a) { return scanf( %lld , &a) == 1; }   template <typename T, typename... TT>   bool RD(T& a, TT&... b) {     return RD(a) && RD(b...);   }   void PT(const char a) { putchar(a); }   void PT(const char* a) { fputs(a, stdout); }   void PT(char* a) { fputs(a, stdout); }   void PT(const double a) { printf( %.16f , a); }   void PT(const int a) { printf( %d , a); }   void PT(const long long a) { printf( %lld , a); }   template <char sep =    , char end =   n >   void PL(void) {     if (end) PT(end);   }   template <char sep =    , char end =   n , typename T, typename... TT>   void PL(const T a, const TT... b) {     PT(a);     if (sizeof...(b) && sep) PT(sep);     PL<sep, end>(b...);   }   const long long inf = 4e10;   const int N = 106;   int n, r, a[N];   pair<int, int> p[N];   long long d[N];   bool used[N];   long long dis(int i, int j) {     return r * (abs(p[i].first - p[j].first) + abs(p[i].second - p[j].second)) -            a[i];   }   int main() {     RD(n, r);     for (int i(2), _B((n - 1) + 1); i < _B; ++i) RD(a[i]);     for (int i(1), _B((n) + 1); i < _B; ++i) RD(p[i].first, p[i].second);     fill_n(d, n + 1, inf);     d[1] = 0;     for (int u = 1, v; u != n; u = v) {       used[u] = 1;       v = 0;       for (int i(1), _B((n) + 1); i < _B; ++i)         if (!used[i]) {           cmin(d[i], d[u] + dis(u, i));           if (d[i] < d[v]) v = i;         }     }     PL(d[n]);   }
#include <bits/stdc++.h>   using namespace std;   const int a[] = {4,   22,  27,  58,  85,  94,   121,  166, 202, 265, 274,                    319, 346, 355, 378, 382, 391,  438,  454, 483, 517, 526,                    535, 562, 576, 588, 627, 634,  636,  645, 648, 654, 663,                    666, 690, 706, 728, 729, 762,  778,  825, 852, 861, 895,                    913, 915, 922, 958, 985, 1086, 1111, 1165};   int main() {     int n;     cin >> n;     cout << n[a - 1];     return 0;   }