func_code_string: #include <bits/stdc++.h>   using namespace std;   template <class Q>   Q _min(Q x, Q y) {     return x < y ? x : y;   }   template <class Q>   Q _max(Q x, Q y) {     return x > y ? x : y;   }   set<long long int> S;   long long int a[1005][1005], b[1005][1005];   long long int lights[1005000];   long long int ans = 0, lreg;   long long int tn;   long long int T[4200000], bj[4200000], dep[4200000];   void up(long long int x) {     long long int i;     for (i = x >> 1; i; i >>= 1) T[i] = _min(T[i << 1], T[i << 1 | 1]);   }   void seta(long long int x, long long int b) {     if (b < T[x]) return;     T[x] = bj[x] = b;   }   void down(long long int x) {     long long int i, v;     for (i = dep[x]; i; i--) {       v = x >> i;       if (bj[v]) {         seta(v << 1, bj[v]);         seta(v << 1 | 1, bj[v]);         bj[v] = 0;       }     }   }   long long int f_s(long long int s, long long int t) {     for (; s ^ t ^ 1; s >>= 1, t >>= 1)       if (~s & 1) return s ^ 1;     return 0;   }   long long int f_t(long long int s, long long int t) {     for (; s ^ t ^ 1; s >>= 1, t >>= 1)       if (t & 1) return t ^ 1;     return 1;   }   void change(long long int l, long long int r, long long int L) {     long long int s = l + tn - 1, t = r + tn + 1;     long long int ss = f_s(s, t), tt = f_t(s, t);     down(ss);     down(tt);     for (; s ^ t ^ 1; s >>= 1, t >>= 1) {       if (~s & 1) seta(s ^ 1, L);       if (t & 1) seta(t ^ 1, L);     }     up(ss);     up(tt);   }   long long int Q(long long int l, long long int r) {     long long int s = l + tn - 1, t = r + tn + 1, M = 1000000000;     long long int ss = f_s(s, t), tt = f_t(s, t);     down(ss);     down(tt);     for (; s ^ t ^ 1; s >>= 1, t >>= 1) {       if (~s & 1) M = _min(M, T[s ^ 1]);       if (t & 1) M = _min(M, T[t ^ 1]);     }     return M;   }   long long int bsc(long long int x) {     long long int l = 1, r = lreg, mid;     while (r - l > 1) {       mid = (l + r) >> 1;       if (lights[mid] < x)         l = mid;       else         r = mid;     }     if (lights[r] == x)       return r;     else       return l;   }   int main() {     long long int n, vx, vy, i, j, k, l, M;     cin >> n >> vx >> vy;     if (vx == 0) {       for (i = 1; i <= n; i++) {         M = 0;         for (j = 1; j <= n; j++) {           cin >> a[i][j];           if (a[i][j] > M) M = a[i][j];         }         ans += M;       }       cout << ans;       return 0;     } else if (vy == 0) {       for (i = 1; i <= n; i++)         for (j = 1; j <= n; j++) cin >> a[i][j];       for (j = 1; j <= n; j++) {         M = 0;         for (i = 1; i <= n; i++)           if (a[i][j] > M) M = a[i][j];         ans += M;       }       cout << ans;       return 0;     }     for (i = 0; i <= n; i++)       for (j = 0; j <= n; j++) {         if (vx > 0)           b[i][j] = vx * j - vy * i;         else           b[i][j] = vy * i - vx * j;         if (S.find(b[i][j]) == S.end())           lights[++lreg] = b[i][j], S.insert(b[i][j]);       }     sort(lights + 1, lights + lreg + 1);     for (tn = 1; tn <= lreg + lreg; tn <<= 1)       ;     tn--;     for (i = 1; i <= tn + tn + 1; i++) dep[i] = dep[i >> 1] + 1;     for (i = 0; i <= n; i++)       for (j = 0; j <= n; j++) b[i][j] = bsc(b[i][j]);     for (i = 1; i <= n; i++)       for (j = 1; j <= n; j++) cin >> a[i][j];     if (vx < 0)       i = n;     else       i = 1;     if (vy < 0)       j = n;     else       j = 1;     for (long long int cnt = 1; cnt <= n * n; cnt++) {       long long int M = 1000000001;       long long int L =           _min(_min(b[i - 1][j - 1], b[i - 1][j]), _min(b[i][j - 1], b[i][j]));       long long int R =           _max(_max(b[i - 1][j - 1], b[i - 1][j]), _max(b[i][j - 1], b[i][j]));       M = Q((L << 1), (R << 1));       change((L << 1), (R << 1), a[i][j]);       if (a[i][j] > M) ans += a[i][j] - M;       if (vy < 0)         j--;       else         j++;       if (j < 1 || j > n) {         if (j < 1)           j += n;         else           j -= n;         if (vx < 0)           i--;         else           i++;       }     }     cout << ans;     return 0;   }   