malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   inline long long in() {     int32_t x;     scanf( %d , &x);     return x;   }   const long long maxn = 1e5 + 10;   const long long mod = 1e9 + 7;   const long long inf = 1e9 + 10;   long long pref[2][maxn], pref2[maxn];   long long maxDp[maxn], minDp[maxn];   int32_t main() {     long long n = in();     string s;     cin >> s;     long long lenT = in();     long long cntA = (lenT + 1) / 2;     long long cntB = lenT / 2;     for (long long i = 0; i < n; i++) {       pref[0][i + 1] = (i ? pref[0][i - 1] : 0) + (s[i] !=  b );       if (i - cntA * 2 >= 0) pref[0][i + 1] -= (s[i - cntA * 2] !=  b );       pref[1][i + 1] = (i ? pref[1][i - 1] : 0) + (s[i] !=  a );       if (i - cntB * 2 >= 0) pref[1][i + 1] -= (s[i - cntB * 2] !=  a );       pref2[i + 1] =           pref2[i] + (s[i] ==  ? ) - (i >= lenT ? (s[i - lenT] ==  ? ) : 0);     }     for (long long i = 0; i < n; i++) {       if (i + 1 < lenT) continue;       maxDp[i + 1] = maxDp[i];       long long value = maxDp[i - lenT + 1];       bool fl = (pref[1][i + 1 * bool(lenT % 2 == 0)] +                  pref[0][i + 1 * bool(lenT % 2)]) == lenT;       value += fl;       maxDp[i + 1] = max(maxDp[i + 1], value);       long long options[2] = {inf, inf};       if (maxDp[i + 1] == maxDp[i]) options[0] = minDp[i];       if (fl) options[1] = minDp[i - lenT + 1] + pref2[i + 1];       minDp[i + 1] = min(options[0], options[1]);     }     cout << minDp[n] <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int stuff(int x) {     int s = 0;     while (x / 10 != 0) {       int r = x % 10;       x /= 10;       s += r;     }     s += x;     return s;   }   int main() {     ios_base::sync_with_stdio(false);     long long int i = 0;     long long int p, f, a, b, c, s, spow = 1;     long long int arr[1000];     cin >> a >> b >> c;     f = a;     for (s = 1; s <= 81; s++) {       f = a;       spow = 1;       while (f--) {         spow *= s;       }       p = (b * spow) + c;       if (stuff(p) == s && p < 1000000000) {         arr[i] = p;         i++;       }     }     sort(arr, arr + i);     cout << i <<   n ;     for (int j = 0; j < i; j++) {       cout << arr[j] <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int t;     cin >> t;     for (int k = 0; k < t; k++) {       int n, total = 0;       cin >> n;       while (n / 10 > 0) {         total = total + (n - (n % 10));         n = ((n - (n % 10)) / 10) + n % 10;       }       cout << total + (n % 10) <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   void max_self(T &a, const T &b) {     if (a < b) a = b;   }   template <class T>   void min_self(T &a, const T &b) {     if (a > b) a = b;   }   const int INF = 1e9 + 7;   const double PI = acos(-1.0);   const double EPS = (1e-9);   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int N, d;     long long cnt = 0;     cin >> N >> d;     vector<int> pos(N);     for (int &ele : pos) cin >> ele;     for (auto itr = pos.begin(); itr != pos.end(); ++itr) {       auto lim = upper_bound((pos).begin(), (pos).end(), *itr + d);       int len = distance(itr, lim) - 1;       cnt += (long long)len * (len - 1) / 2;     }     cout << cnt <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void init() {     ios::sync_with_stdio(0);     cin.tie(0);   }   int main() {     init();     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       long long arr[n][m];       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           cin >> arr[i][j];         }       }       long long ops = 0;       for (long long i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           long long a[4];           a[0] = arr[i][j];           a[1] = arr[n - 1 - i][m - 1 - j];           a[2] = arr[i][m - 1 - j];           a[3] = arr[n - 1 - i][j];           sort(a, a + 4);           long long m = (a[1] + a[2]) / 2;           for (int ii = 0; ii < 4; ii++) {             ops += abs(a[ii] - (m));           }         }       }       ops /= 4;       cout << ops << endl;     }   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   #pragma GCC optimization( unroll-loops )   using namespace std;   const int maxn = 3e5 + 5;   const long long mod = 998244353;   const double eps = 1e-6L;   struct node {     int l, r;     bool operator<(const node& k) const {       if (l == k.l) return r > k.r;       return l < k.l;     }   } a[maxn];   long long fac[maxn], f[maxn], inv[maxn];   void init() {     fac[1] = 1;     f[1] = 1;     inv[1] = 1;     for (long long i = 2; i <= maxn; i++) {       fac[i] = fac[i - 1] * i % mod;       f[i] = (mod - mod / i) * f[mod % i] % mod;       inv[i] = inv[i - 1] * f[i] % mod;     }   }   long long C(long long m, long long n) {     if (m < n) return 0;     if (n == 0 || m == n) return 1;     return fac[m] * inv[n] % mod * inv[m - n] % mod;   }   int b[maxn];   int main() {     int T = 1;     init();     while (T--) {       int n, k, m = 1;       scanf( %d %d , &n, &k);       for (int i = 0; i < n; i++) {         scanf( %d %d , &a[i].l, &a[i].r);         b[i] = a[i].r;       }       sort(a, a + n);       sort(b, b + n);       int left = a[0].l, right = a[0].r;       long long ans = 0;       bool flag = true;       for (int i = n - 1; i >= 0; i--) {         int num = i + 1 - (lower_bound(b, b + n, a[i].l) - b);         ans = (ans + C(num - 1, k - 1)) % mod;       }       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long N;   void solve() {     int i, j, k, l, r, x, y;     string s;     cin >> N;     long long ret = 0;     long long a = 1;     while (N > 1) {       ret += N / 2 * a;       N = (N + 1) >> 1;       a <<= 1;     }     cout << ret << endl;   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 200008;   int n, k, l, r;   char g[2][maxn];   bool f[2][maxn];   struct arr {     int t, s, x;   } h[maxn];   bool Add(int t, int s, int x) {     if (t >= x) return 0;     if (x > n) return 1;     if (f[s][x]) return 0;     if (g[s][x] ==  X ) return 0;     f[s][x] = 1;     ++r;     h[r].t = t;     h[r].s = s;     h[r].x = x;     return 0;   }   bool Work() {     h[1].t = 0;     h[1].s = 0;     h[1].x = 1;     l = 1, r = 1;     f[0][1] = 1;     while (l <= r) {       int t = h[l].t, s = h[l].s, x = h[l].x;       ++l;       if (Add(t + 1, s, x + 1)) return 1;       if (Add(t + 1, s, x - 1)) return 1;       if (Add(t + 1, s ^ 1, x + k)) return 1;     }     return 0;   }   int main() {     scanf( %d%d%*c , &n, &k);     scanf( %s , &g[0][1]);     scanf( %s , &g[1][1]);     if (Work())       printf( YES n );     else       printf( NO n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n, m, a, b;     cin >> n >> m >> a >> b;     int ans = 1000000000;     for (int i = 0; i <= n / m + 1; i++) {       ans = min(ans, i * b + max(n - m * i, 0) * a);     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 5e2 + 5;   vector<pair<int, int> > adj[MAX];   int dis[MAX][MAX], in[MAX][MAX];   int n, m;   void dijkstra(int source) {     memset(dis[source], 0x3F, sizeof dis[source]);     dis[source][source] = 0;     priority_queue<pair<int, int>, vector<pair<int, int> >,                    greater<pair<int, int> > >         pq;     pq.emplace(dis[source][source], source);     while (!pq.empty()) {       pair<int, int> cur = pq.top();       pq.pop();       int w, v;       tie(w, v) = cur;       if (w > dis[source][v]) {         continue;       }       for (auto &each : adj[v]) {         int vv, ww;         tie(vv, ww) = each;         if (dis[source][vv] > w + ww) {           dis[source][vv] = w + ww;           in[source][vv] = 1;           pq.emplace(dis[source][vv], vv);         } else if (dis[source][vv] == w + ww) {           in[source][vv]++;         }       }     }   }   int main() {     scanf( %d %d , &n, &m);     for (int i = int(0); i < int(m); i++) {       int u, v, w;       scanf( %d %d %d , &u, &v, &w);       adj[u].emplace_back(v, w);       adj[v].emplace_back(u, w);     }     for (int i = int(1); i < int(n + 1); i++) {       dijkstra(i);     }     for (int i = int(1); i < int(n + 1); i++) {       for (int j = int(i + 1); j < int(n + 1); j++) {         int total = 0;         for (int k = int(1); k < int(n + 1); k++) {           if (dis[i][k] + dis[k][j] == dis[i][j]) {             total += in[i][k];           }         }         printf( %d  , total);       }     }     puts(  );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using namespace std::chrono;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n;     cin >> n;     if (n % 2 == 0) {       cout <<  NO  << endl;       return 0;     }     cout <<  YES  << endl;     int arr[2 * n + 5];     for (int i = 1, j = 2 * n; i <= n; i++) {       if (i % 2 == 1) {         arr[i] = j;         j--;         arr[i + n] = j;         j--;       } else {         arr[i + n] = j;         j--;         arr[i] = j;         j--;       }     }     for (int i = 1; i <= 2 * n; i++) {       cout << arr[i] <<    ;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class II, class SZ, class OI>   OI cpy_n(II _i, SZ _s, OI _o) {     for (int_fast64_t _j = 1; _j <= _s; _j++) {       *(_o++) = *_i;       if (_j != _s) {         _i++;       }     };     return _o;   }   template <class T>   void slurp(vector<T>& v, int_least32_t n = 1000000000) {     istream_iterator<T> i(cin);     istream_iterator<T> eoi;     ;     if (n == 1000000000) {       copy((i), (eoi), back_inserter((v)));     } else {       v.reserve(n);       cpy_n(i, n, back_inserter((v)));     }   }   template <class T, class U>   void slurp2(vector<T>& va, vector<U>& vb, int_least32_t n = 1000000000) {     istream_iterator<T> a(cin);     istream_iterator<T> eoa;     ;     istream_iterator<U> b(cin);     istream_iterator<U> eob;     ;     if (n != 1000000000) {       va.reserve(n);       vb.reserve(n);     }     auto ao = back_inserter((va));     auto bo = back_inserter((vb));     for (int_fast64_t i = 1; i <= n; i++) {       if (a == eoa) break;       *(ao++) = *a;       *(bo++) = *b;       if (i != n) {         a++;         b++;       }     }   }   chrono::time_point<chrono::system_clock> start_time;   void do_thing(vector<string>& bs, int_fast64_t i, int_fast64_t j) {     for (int_fast64_t int_least32_t = (i); int_least32_t <= (i + 2);          int_least32_t++) {       if (bs[int_least32_t][j + 1] !=  . ) {         return;       }     }     for (int_fast64_t jj = (j); jj <= (j + 2); jj++) {       if (bs[i + 1][jj] !=  . ) {         return;       }     }     for (int_fast64_t int_least32_t = (i); int_least32_t <= (i + 2);          int_least32_t++) {       bs[int_least32_t][j + 1] =  # ;     }     for (int_fast64_t jj = (j); jj <= (j + 2); jj++) {       bs[i + 1][jj] =  # ;     }   }   void do_it() {     int_fast64_t n;     cin >> n;     vector<string> bs;     slurp(bs, n);     for (int_fast64_t i = (0); i <= (n - 3); i++) {       for (int_fast64_t j = (0); j <= (n - 3); j++) {         do_thing(bs, i, j);       }     }     for (int_fast64_t i = (0); i <= (n - 1); i++) {       for (int_fast64_t j = (0); j <= (n - 1); j++) {         if (bs[i][j] !=  # ) {           cout <<  NO  << endl;           return;         }       }     }     cout <<  YES  << endl;   }   void start() { start_time = chrono::system_clock::now(); }   void end() {     chrono::time_point<chrono::system_clock> end_time;     end_time = chrono::system_clock::now();     chrono::duration<double> diff = end_time - start_time;     cerr << fixed << setprecision(4) << diff.count() <<  s  << endl;   }   void do_mult() {     int_least32_t times;     cin >> times;     for (int_fast64_t i = 1; i <= times; i++) {       do_it();     }   }   int main() {     ios::sync_with_stdio(false);     start();     do_it();     end();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   const int MOD = 1e9 + 7;   const int dx[4] = {0, 0, -1, 1};   const int dy[4] = {-1, 1, 0, 0};   const int N = 1e5 + 1;   int n;   long long a[N], s[N], p[N], k;   int ans = INF;   bool Check(int val) {     long long ans = k + 1;     for (int l = 1, r = 1; l <= n; l++) {       while (r <= n && a[r] - a[l] <= val) r++;       r--;       long long x = (a[l] * l - p[l]) + (s[r + 1] - (a[l] + val) * (n - r));       ans = min(ans, x);     }     for (int r = n, l = n; r > 0; r--) {       while (l > 0 && a[r] - a[l] <= val) l--;       l++;       long long x =           (s[r] - a[r] * (n - r + 1)) + ((a[r] - val) * (l - 1) - p[l - 1]);       ans = min(ans, x);     }     return (ans <= k);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> n >> k;     for (int i = 1; i <= n; i++) cin >> a[i];     sort(a + 1, a + 1 + n);     for (int i = 1; i <= n; i++) p[i] = ((i == 1) ? a[i] : p[i - 1] + a[i]);     for (int i = n; i > 0; i--) s[i] = ((i == n) ? a[i] : s[i + 1] + a[i]);     int l = 0;     int r = INF;     while (l <= r) {       int m = (l + r) / 2;       if (Check(m)) {         ans = m;         r = m - 1;       } else         l = m + 1;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[100000];   int b[100000];   int c[100000];   struct P {     int a, b;   } p[100000];   int cmp(P x, P y) { return x.a < y.a; }   int main() {     int n, x;     while (cin >> n) {       for (int i = 1; i <= n; i++) {         cin >> x;         a[x] = i;       }       for (int i = 1; i <= n; i++) {         cin >> x;         b[x] = i;         p[i - 1].a = i;         p[i - 1].b = x;       }       for (int i = 1; i <= n; i++) {         int key = a[i];         int l = 0, r = n, mid;         while (l < r) {           mid = (l + r) / 2;           if (p[mid].a == key) {             if (i != n)               cout << p[mid].b <<    ;             else               cout << p[mid].b << endl;             break;           } else if (p[mid].a > key)             r = mid;           else             l = mid + 1;         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 5;   int cnt[N], steps[N], vis[N];   void solve() {     int n, res = INT_MAX, x, y;     cin >> n;     for (int i = 1; i < n + 1; i++) {       cin >> x;       queue<pair<int, int>> q;       q.push({x, 0});       while (!q.empty()) {         x = q.front().first;         y = q.front().second;         q.pop();         if (x > N) continue;         if (vis[x] == i) continue;         vis[x] = i;         steps[x] += y;         cnt[x]++;         q.push({2 * x, y + 1});         q.push({x / 2, y + 1});       }     }     for (int i = 0; i < N; i++) {       if (cnt[i] == n) {         if (res > steps[i]) res = steps[i];       }     }     cout << res;   }   int main() {     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cerr.tie(0);     ;     int t = 1;     while (t--) solve();   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int n, k, a[N], ans = 0;   multiset<int> ms;   int main(int argc, char const *argv[]) {     cin >> n >> k;     for (int i = 1; i <= n; ++i) {       cin >> a[i];       if (!a[i] && i != k) a[i] = n + 1;       if (i == k && a[i]) a[i] = 0, ans = 1;       ms.insert(a[i]);     }     int mex = 0;     while (!ms.empty()) {       if (ms.find(mex) == ms.end()) {         int k = *ms.rbegin();         ms.erase(ms.find(k));         ans++;       } else {         ms.erase(mex);       }       mex++;     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 3e5 + 5;   int n, q;   struct Edge {     int to, nxt;   } e[MAX_N];   int head[MAX_N], tote;   void add_edge(int u, int v) {     e[++tote].to = v, e[tote].nxt = head[u];     head[u] = tote;   }   int siz[MAX_N], fa[MAX_N], ans[MAX_N];   void dfs(int u, int pre) {     fa[u] = pre;     siz[u] = 1;     int son = -1;     for (int i = head[u]; i; i = e[i].nxt) {       int to = e[i].to;       dfs(to, u);       siz[u] += siz[to];       if (son == -1 || siz[to] > siz[son]) son = to;     }     if (son == -1)       ans[u] = u;     else {       son = ans[son];       while ((2 * (siz[u] - siz[son])) > siz[u]) son = fa[son];       ans[u] = son;     }   }   void init() { memset(head, 0, sizeof(head)), tote = 0; };   int main() {     init();     scanf( %d , &n), scanf( %d , &q);     for (register int i = 2; i <= n; i++) {       int x;       scanf( %d , &x);       add_edge(x, i);     }     dfs(1, 1);     while (q--) {       int x;       scanf( %d , &x);       printf( %d n , ans[x]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int inf = 0x3f3f3f3f;   const long long inff = 0x3f3f3f3f3f3f3f3f;   const int mod = 998244353;   const double eps = 1e-9;   const int maxV = 3e5 + 5;   int n, V, mV, a[maxV], tot;   long long dp[5][maxV];   vector<long long> w[5];   void solve(int l, int r, int L, int R, int id) {     if (l > r) return;     int pos = L, mid = l + r >> 1;     for (int i = max(mid - (int)w[id].size(), L); i <= min(mid, R); i++) {       if (dp[id][a[mid]] <           dp[id - 1][a[i]] + (i == mid ? 0 : w[id][mid - i - 1])) {         dp[id][a[mid]] = dp[id - 1][a[i]] + (i == mid ? 0 : w[id][mid - i - 1]);         pos = i;       }     }     solve(l, mid - 1, L, pos, id);     solve(mid + 1, r, pos, R, id);   }   namespace fastIO {   static char buf[100000], *h = buf, *d = buf;   template <typename T>   inline void read(T& x) {     int f = 1;     x = 0;     register char c(         h == d && (d = (h = buf) + fread(buf, 1, 100000, stdin), h == d) ? EOF                                                                          : *h++);     while (c >  9  || c <  0 ) {       if (c ==  - ) f = -1;       c = h == d && (d = (h = buf) + fread(buf, 1, 100000, stdin), h == d) ? EOF                                                                            : *h++;     }     while (c <=  9  && c >=  0 )       x = (x << 1) + (x << 3) + (c ^ 48),       c = h == d && (d = (h = buf) + fread(buf, 1, 100000, stdin), h == d) ? EOF                                                                            : *h++;     x *= f;   }   template <typename T>   void output(T x) {     if (x < 0) {       putchar( - );       x = ~(x - 1);     }     static int s[20], top = 0;     while (x) {       s[++top] = x % 10;       x /= 10;     }     if (!top) s[++top] = 0;     while (top) putchar(s[top--] +  0 );   }   }  // namespace fastIO   using fastIO::output;   using fastIO::read;   int main() {     read(n), read(V);     for (int i = (1); i <= (int)(n); ++i) {       int c, w0;       read(c), read(w0);       w[c].push_back(w0);       mV = max(mV, c);     }     for (int i = (1); i <= (int)(mV); ++i) {       sort(w[i].begin(), w[i].end(), greater<int>());       for (int j = 1; j < w[i].size(); j++) {         w[i][j] += w[i][j - 1];       }     }     for (int i = (1); i <= (int)(mV); ++i) {       for (int j = (0); j <= (int)(i - 1); ++j) {         tot = 0;         for (int k = j; k <= V; k += i) {           a[++tot] = k;         }         solve(1, tot, 1, tot, i);       }     }     int flag = 0;     printf( %lld n , dp[mV][V]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long max_n = 500 + 5;   long long n;   long long a[max_n], b[max_n];   bool are_equal(long long arr1[], long long arr2[], long long sz) {     for (int i = 0; i < sz; ++i) {       if (arr1[i] != arr2[i]) return false;     }     return true;   }   bool same_elements(long long arr1[], long long arr2[], long long sz) {     long long tarr1[sz], tarr2[sz];     for (int i = 0; i < sz; ++i) {       tarr1[i] = arr1[i];       tarr2[i] = arr2[i];     }     sort(tarr1, tarr1 + sz);     sort(tarr2, tarr2 + sz);     return are_equal(tarr1, tarr2, sz);   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long t;     cin >> t;     while (t--) {       cin >> n;       for (int z = 0; z < n; ++z) cin >> a[z];       ;       for (int z = 0; z < n; ++z) cin >> b[z];       ;       if (!same_elements(a, b, n) or (n & 1 and a[n / 2] != b[n / 2])) {         cout <<  No n ;         continue;       }       vector<pair<long long, long long> > p;       for (int i = 0; i < (n / 2); ++i) p.push_back({a[i], a[n - 1 - i]});       bool flag = true;       for (int i = 0; i < (n / 2) and flag; ++i) {         long long x;         bool got = false;         pair<long long, long long> temp = {b[i], b[n - 1 - i]};         for (x = 0; x < p.size() and !got; ++x) {           if (p[x] == temp) {             p.erase(p.begin() + x);             got = true;           }         }         temp = {b[n - 1 - i], b[i]};         for (x = 0; x < p.size() and !got; ++x) {           if (p[x] == temp) {             p.erase(p.begin() + x);             got = true;           }         }         flag = got;       }       if (flag)         cout <<  Yes n ;       else         cout <<  No n ;     }     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:1024000000,1024000000 )   using namespace std;   const double pi = acos(-1);   const double e = exp(1);   const int INF = 0x3f3f3f3f;   const int MOD = 1e9 + 7;   template <typename T>   inline void read(T &x) {     T ans = 0;     char last =    , ch = getchar();     while (ch <  0  || ch >  9 ) last = ch, ch = getchar();     while (ch >=  0  && ch <=  9 ) ans = ans * 10 + ch -  0 , ch = getchar();     if (last ==  - ) ans = -ans;     x = ans;   }   const int maxn = 1000 + 10;   char str[maxn];   int vis[maxn];   int main() {     while (~scanf( %s , str)) {       memset(vis, 0, sizeof(vis));       int flag = 1;       for (int i = 0; str[i]; i++) {         for (int j = 0; j < str[i] -  a ; j++) {           if (!vis[j]) {             flag = 0;             break;           }         }         if (!flag) break;         vis[str[i] -  a ] = 1;       }       puts(flag ?  YES  :  NO );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[105][105], v[105][105], dp[105][105];   long long slove(int x, int y, int n, int m) {     long long t = a[x][y] - (x + y - 2);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         v[i][j] = a[i][j] - (t + i + j - 2);       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         dp[i][j] = -1;         if (v[i][j] < 0) continue;         if (i == 1 && j == 1)           dp[i][j] = v[i][j];         else if (i == 1) {           long long t = dp[i][j - 1];           if (t != -1) dp[i][j] = t + v[i][j];         } else if (j == 1) {           long long t = dp[i - 1][j];           if (t != -1) dp[i][j] = t + v[i][j];         } else {           long long t1 = dp[i - 1][j], t2 = dp[i][j - 1];           if (t1 != -1) dp[i][j] = t1 + v[i][j];           if (t2 != -1) {             if (dp[i][j] == -1)               dp[i][j] = t2 + v[i][j];             else               dp[i][j] = min(dp[i][j], t2 + v[i][j]);           }         }       }     }     if (dp[n][m] != -1)       return dp[n][m];     else       return 1e18;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n, m;       cin >> n >> m;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           cin >> a[i][j];         }       }       long long ans = 1e18;       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= m; j++) {           ans = min(ans, slove(i, j, n, m));         }       }       cout << ans <<   n ;     }     return 0;   }