#include <bits/stdc++.h>
 using namespace std;
 const int mod = int(1e6 + 3);
 vector<vector<int> > v;
 char second[600000];
 int n, m;
 int main() {
   scanf('%d%d
', &n, &m);
   v.resize(n);
   for (int i = 0; i < n; ++i) {
     gets(second);
     v[i].resize(m);
     for (int j = 0; j < m; ++j) {
       v[i][j] = (second[j] == '.') ? -1 : second[j] - '0';
     }
   }
   int res = 1;
   for (int i = 0; i < n; ++i) {
     int dir = -1;
     for (int j = 0; j < m; ++j)
       if (v[i][j] != -1) {
         dir = (v[i][j] > 2) ? 1 : 0;
         if (j & 1) dir = 1 - dir;
         break;
       }
     if (dir != -1) {
       for (int j = 0; j < m; ++j)
         if (v[i][j] != -1) {
           int d2 = (v[i][j] > 2) ? 1 : 0;
           if (j & 1) d2 = 1 - d2;
           if (dir != d2) res = 0;
         }
     } else if (dir == -1)
       res = (res * 2) % mod;
   }
   for (int j = 0; j < m; ++j) {
     int dir = -1;
     for (int i = 0; i < n; ++i)
       if (v[i][j] != -1) {
         dir = (v[i][j] > 1 && v[i][j] < 4) ? 1 : 0;
         if (i & 1) dir = 1 - dir;
         break;
       }
     if (dir != -1) {
       for (int i = 0; i < n; ++i)
         if (v[i][j] != -1) {
           int d2 = (v[i][j] > 1 && v[i][j] < 4) ? 1 : 0;
           if (i & 1) d2 = 1 - d2;
           if (dir != d2) res = 0;
         }
     } else if (dir == -1)
       res = (res * 2) % mod;
   }
   printf('%d
', res);
   return 0;
 }


#include <bits/stdc++.h>
 const int maxn = 1e5 + 5;
 using namespace std;
 int a[maxn];
 int vis[maxn];
 int step[maxn * 2];
 int main() {
   int n;
   scanf('%d', &n);
   for (int i = 0; i < n; i++) {
     scanf('%d', &a[i]);
     int temp = a[i];
     int step1 = 0, step2 = 0, step3 = 0;
     while (temp < maxn) {
       step1++;
       vis[temp]++;
       temp = temp << 1;
       step[temp] += step1;
     }
     while (a[i] >= 1) {
       if (a[i] & 1 && a[i] != 1) {
         int temp1 = a[i] = a[i] >> 1;
         step2++;
         step3 = step2;
         vis[temp1]++;
         step[temp1] += step3;
         while (temp1 < maxn) {
           step3++;
           temp1 = temp1 << 1;
           vis[temp1]++;
           step[temp1] += step3;
         }
       } else {
         step2++;
         a[i] = a[i] >> 1;
         vis[a[i]]++;
         step[a[i]] += step2;
       }
     }
   }
   int ans = 1e9;
   for (int i = 1; i < maxn; i++) {
     if (vis[i] == n) {
       ans = min(step[i], ans);
     }
   }
   printf('%d
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename Arg1>
 void __f(const char* name, Arg1&& arg1) {
   cerr << name << ' : ' << arg1 << std::endl;
 }
 template <typename Arg1, typename... Args>
 void __f(const char* names, Arg1&& arg1, Args&&... args) {
   const char* comma = strchr(names + 1, ',');
   cerr.write(names, comma - names) << ' : ' << arg1 << ' | ';
   __f(comma + 1, args...);
 }
 const int mod = int(1e9) + 7;
 const int N = int(1e6) + 7;
 long long int two[N];
 void pre() {
   two[0] = 1;
   for (int i = (int)1; i < (int)N; i++) {
     two[i] = (two[i - 1] * 2) % mod;
   }
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   pre();
   string s;
   cin >> s;
   int cnt = 0;
   long long int ans = 0;
   for (int i = (int)0; i < (int)(int)(s.size()); i++) {
     if (s[i] == 'b') {
       ans = (ans + (two[cnt] - 1)) % mod;
       continue;
     }
     cnt++;
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const double PI =
     3.14159265358979323846264338327950288419716939937510582097494459230;
 void swaps(char *x, char *y) {
   char temp;
   temp = *x;
   *x = *y;
   *y = temp;
 }
 void swapi(int *a, int *b) {
   int temp;
   temp = *a;
   *a = *b;
   *b = temp;
 }
 unsigned long long gcd(unsigned long long a, unsigned long long b) {
   if (a == 0) return b;
   if (b == 0) return a;
   if (a == 1 || b == 1) return 1;
   if (a == b) return a;
   if (a > b)
     return gcd(b, a % b);
   else
     return gcd(a, b % a);
 }
 long long Tree[4000010];
 int V[4000010];
 int N;
 int Time;
 int Outtime[4000010];
 int Intime[4000010];
 int Lvl[4000010];
 int maxval;
 bool vis[4000010];
 std::vector<std::vector<int> > Edge(2000010);
 void Add(int idx, int val) {
   for (int i = idx; i <= maxval; i += (i & -i)) Tree[i] += val;
 }
 long long Query(int idx) {
   long long S = 0;
   for (int i = idx; i > 0; i -= (i & -i)) S += Tree[i];
   return S;
 }
 void dfs(int idx) {
   vis[idx] = true;
   Intime[idx] = ++Time;
   vis[idx] = true;
   for (int i = 0; i < Edge[idx].size(); i++) {
     if (vis[Edge[idx][i]]) continue;
     Lvl[Edge[idx][i]] = Lvl[idx] + 1;
     dfs(Edge[idx][i]);
   }
   Outtime[idx] = ++Time;
 }
 int main() {
   int Cases = 1;
   for (int Case = 0; Case < Cases; Case++) {
     int u, v, opt, node, Q;
     cin >> N >> Q;
     for (int i = 1; i <= N; i++) cin >> V[i];
     for (int i = 0; i < N - 1; i++) {
       cin >> u >> v;
       Edge[u].push_back(v);
       Edge[v].push_back(u);
     }
     dfs(1);
     maxval = Outtime[1];
     for (int i = 0; i < Q; i++) {
       cin >> opt;
       opt -= 1;
       if (opt == 0) {
         cin >> node >> v;
         if (Lvl[node] % 2) {
           Add(Intime[node], -v);
           Add(Outtime[node], v);
         } else {
           Add(Intime[node], v);
           Add(Outtime[node], -v);
         }
       } else {
         cin >> node;
         if (Lvl[node] % 2)
           cout << V[node] - Query(Intime[node]) << endl;
         else
           cout << V[node] + Query(Intime[node]) << endl;
       }
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 long long int binpow(long long int a, long long int b) {
   if (b == 0) return 1;
   long long int res = binpow(a, b / 2);
   if (b % 2)
     return res * res * a;
   else
     return res * res;
 }
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int t;
   cin >> t;
   while (t--) {
     int n;
     cin >> n;
     vector<int> v(n, 0);
     for (int i = 0; i < v.size(); i++) cin >> v[i];
     sort(v.begin(), v.end(), greater<int>());
     for (int i = 0; i < n; i++) cout << v[i] << ' ';
     cout << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const long long LLINF = 1e18;
 const int INF = 1e9;
 const int MOD = 20101009;
 const int HMOD = 999959;
 const int VMOD = 5000000;
 const int MAXN = 1e5 + 10;
 const int MAXM = 5e5 + 10;
 const int INV = 10050505;
 const long long LLINV = 1e18;
 const double eps = 1e-3;
 int dirx[5] = {0, -1, 1, 0, 0}, diry[5] = {0, 0, 0, -1, 1};
 int n, d, m;
 int cnt1, cnt2;
 long long a[MAXN], b[MAXN];
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   ;
   scanf('%d%d%d', &n, &d, &m);
   for (int i = 1, x; i <= n; ++i) {
     scanf('%d', &x);
     if (x <= m)
       a[++cnt1] = x;
     else
       b[++cnt2] = x;
   }
   sort(a + 1, a + 1 + cnt1, greater<int>());
   sort(b + 1, b + 1 + cnt2, greater<int>());
   if (cnt2 == 0) {
     long long sum = 0;
     for (int i = 1; i <= n; ++i) sum += a[i];
     printf('%lld
', sum);
     return 0;
   }
   for (int i = 1; i <= cnt1; ++i) a[i] = a[i - 1] + a[i];
   for (int i = 1; i <= cnt2; ++i) b[i] = b[i - 1] + b[i];
   long long ans = 0;
   for (int i = 1; i <= cnt2; ++i) {
     long long tmp = (long long)(i - 1) * d;
     long long oth = n - i;
     long long ned = cnt1 - (max(0LL, tmp - (cnt2 - i)));
     if (oth >= tmp) ans = max(ans, b[i] + a[ned]);
   }
   printf('%lld
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, m, q;
 int tt;
 int ss = n + m;
 int fa[400005];
 bool vis[500005];
 int ans;
 int find(int x) {
   if (fa[x] == x) return x;
   return fa[x] = find(fa[x]);
 }
 int main() {
   scanf('%d%d%d', &n, &m, &q);
   for (int i = 1; i <= n + m; i++) {
     fa[i] = i;
   }
   for (int i = 1; i <= q; i++) {
     int x, y;
     scanf('%d%d', &x, &y);
     fa[find(x)] = find(n + y);
   }
   for (int i = 1; i <= n + m; i++) {
     if (!vis[find(i)]) {
       vis[fa[i]] = 1;
       ans++;
     }
   }
   cout << ans - 1;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, m;
   cin >> n >> m;
   long long ans = 0;
   for (int i = (0); i < (n); ++i) {
     long long t, T, x, cost;
     cin >> t >> T >> x >> cost;
     if (t >= T) {
       ans += cost + m * x;
       continue;
     }
     long long aux1 = cost;
     if (m > (T - t)) aux1 += m * x;
     long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1;
     aux2 *= cost;
     ans += min(aux1, aux2);
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long modulo(long long base, long long exponent, long long modulus);
 long long choose(long long n, long long k);
 long long inverse(long long a, long long m);
 void build();
 void fileio();
 long long ncr(long long n, long long r);
 const int nax = 1e6 + 10;
 const int LG = log2(nax) + 1;
 vector<long long> fact(nax);
 int32_t main() {
   ios_base::sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
   long long t;
   cin >> t;
   while (t--) {
     long long n;
     cin >> n;
     string s;
     cin >> s;
     long long ans = n;
     for (long long i = 0; i < n; i++) {
       if (s[i] == '1') {
         ans = max(ans, 2 * (i + 1));
         ans = max(ans, 2 * (n - i));
       }
     }
     cout << ans << '
';
   }
   return 0;
 }
 long long ncr(long long n, long long r) {
   if (r > n || r < 0 || n < 0) return 0;
   long long ans = fact[n];
   long long temp = (fact[n - r] * fact[r]) % 1000000007;
   ans = (ans * inverse(temp, 1000000007)) % 1000000007;
   return ans;
 }
 void fileio() {
   freopen('input.txt', 'r', stdin);
   freopen('output.txt', 'w', stdout);
 }
 void build() {
   fact[0] = 1;
   for (long long i = 1; i < nax; i++) fact[i] = (fact[i - 1] * i) % 1000000007;
 }
 long long modulo(long long base, long long exponent, long long modulus) {
   if (modulus == 1) return 0;
   long long result = 1;
   base = base % modulus;
   while (exponent > 0) {
     if (exponent % 2 == 1) {
       result = (result * base) % modulus;
     }
     exponent = exponent >> 1;
     base = (base * base) % modulus;
   }
   return result;
 }
 long long choose(long long n, long long k) {
   if (k == 0) return 1;
   return (n * choose(n - 1, k - 1)) / k;
 }
 void EE(long long a, long long b, long long &co1, long long &co2) {
   if (a % b == 0) {
     co1 = 0;
     co2 = 1;
     return;
   }
   EE(b, a % b, co1, co2);
   long long temp = co1;
   co1 = co2;
   co2 = temp - co2 * (a / b);
 }
 long long inverse(long long a, long long m) {
   long long x, y;
   EE(a, m, x, y);
   if (x < 0) x += m;
   return x;
 }


#include <bits/stdc++.h>
 using namespace std;
 void fastio() {
   cin.tie(nullptr);
   cin.sync_with_stdio(false);
 }
 using LL = long long;
 using LD = long double;
 const LL MOD = 1e9 + 7;
 const LL INF = LLONG_MAX;
 const LL N = 2e5 + 1;
 int main() {
   fastio();
   LL n;
   cin >> n;
   vector<LL> a(n);
   for (auto &e : a) {
     cin >> e;
   }
   vector<LL> ans(n, 0);
   sort(a.begin(), a.end(), greater<LL>());
   LL p = 0;
   for (LL i = 0; i < n; i += 2) {
     ans[i] = a[p++];
   }
   for (LL i = 0; i < n; ++i) {
     if (ans[i] == 0) {
       ans[i] = a[p++];
     }
   }
   cout << (n - 1) / 2 << '
';
   for (auto &e : ans) {
     cout << e << ' ';
   }
   cout << '
';
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int q, n, sum, a;
   cin >> q;
   while (q--) {
     sum = 0;
     cin >> n;
     for (int i = 0; i < n; i++) {
       cin >> a;
       sum += a;
     }
     cout << (sum + n - 1) / n << endl;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 5000 + 20;
 const int maxn = 1e5 + 20;
 const int mod = 998244353;
 int cnt, head[maxn], pos[maxn];
 long long ksm(long long a, long long b) {
   long long res = 1;
   while (b) {
     if (b & 1) res = (res * a) % mod;
     b >>= 1, a = a * a % mod;
   }
   return res % mod;
 }
 void read(int &v) {
   int k = 1;
   v = 0;
   int c = getchar();
   while (c < '0' || c > '9') {
     if (c == '-') k = 0;
     c = getchar();
   }
   while (c >= '0' && c <= '9')
     v = (v << 3) + (v << 1) + (c - 48), c = getchar();
   if (k == 0) v = -v;
 }
 int d[maxn], dp[maxn], a[maxn], same[maxn];
 int main() {
   int n, q;
   read(n), read(q);
   for (int i = 1; i <= n; i++) {
     read(a[i]);
     if (a[i] == a[i - 1]) same[i] = same[i - 1] + 1;
     if (a[i] >= a[i - 1])
       d[i] = 0;
     else
       d[i] = 1;
   }
   dp[0] = 0;
   for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] + d[i];
   while (q--) {
     int l, r;
     read(l), read(r);
     if (dp[r] - dp[l] == 0)
       puts('Yes');
     else if (dp[r] - dp[l] == r - l)
       puts('Yes');
     else {
       int x = dp[r] - dp[l];
       r = r - same[r];
       if (dp[r] - dp[r - x] == x)
         puts('Yes');
       else
         puts('No');
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int query(int x, int y) {
   if (x == -1) return 0;
   cout << 1 << ' ' << x << ' ' << y << endl;
   string ret;
   cin >> ret;
   return ('TAK' == ret);
 }
 int get(int l, int r) {
   if (l > r) return -1;
   while (l < r) {
     int m = (l + r) >> 1;
     if (query(m, m + 1)) {
       r = m;
     } else {
       l = m + 1;
     }
   }
   return l;
 }
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   int n, k;
   cin >> n >> k;
   int x = get(1, n);
   int y = get(1, x - 1);
   if (!query(y, x)) y = get(x + 1, n);
   cout << 2 << ' ' << x << ' ' << y << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, k, d, done[300005] = {0};
 vector<int> graph[300005];
 map<pair<int, int>, int> edge;
 set<int> p;
 void go() {
   queue<pair<int, int> > q;
   for (auto it : p) q.push({it, 0});
   while (!q.empty()) {
     int x = q.front().first, P = q.front().second;
     q.pop();
     if (done[x]) continue;
     done[x] = 1;
     for (auto it : graph[x]) {
       if (done[it] && it != P)
         printf('%d ', edge[{min(x, it), max(x, it)}]);
       else if (it != P)
         q.push({it, x});
     }
   }
 }
 int main() {
   scanf('%d', &n);
   scanf('%d', &k);
   scanf('%d', &d);
   for (__typeof(k) i = 0 - (0 > k); i != k - (0 > k); i += 1 - 2 * (0 > k)) {
     int x;
     scanf('%d', &x);
     p.insert(x);
   }
   for (__typeof(n - 1) i = 0 - (0 > n - 1); i != n - 1 - (0 > n - 1);
        i += 1 - 2 * (0 > n - 1)) {
     int u, v;
     scanf('%d', &u);
     scanf('%d', &v);
     graph[u].push_back(v);
     graph[v].push_back(u);
     edge[{min(u, v), max(u, v)}] = i + 1;
   }
   printf('%d
', (int)p.size() - 1);
   go();
   printf('
');
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 const int N = 88;
 const int M = 100100;
 int dp[N][M][2];
 pair<int, int> a[N];
 int n, m;
 int main() {
   scanf('%d%d', &n, &m);
   for (int i = 0; i < n; i++) scanf('%d%d', &a[i].first, &a[i].second);
   sort(a, a + n);
   for (int i = 0; i <= n; i++)
     for (int j = 0; j <= m; j++)
       for (int f = 0; f < 2; f++) dp[i][j][f] = M;
   dp[0][0][1] = 0;
   for (int i = 0; i < n; i++) {
     for (int x = 0; x <= m; x++)
       dp[i + 1][x][0] = min(dp[i + 1][x][0], min(dp[i][x][0], dp[i][x][1]));
     for (int x = 0; x <= m; x++) {
       int r = a[i].first - x - 1;
       r = max(r, x - a[i].first);
       r = max(r, a[i].second);
       int y = min(m, a[i].first + r);
       dp[i + 1][y][1] =
           min(dp[i + 1][y][1], min(dp[i][x][0], dp[i][x][1]) + r - a[i].second);
     }
     for (int x = 1; x <= m; x++)
       dp[i + 1][x][1] = min(dp[i + 1][x][1], dp[i + 1][x - 1][1] + 1);
   }
   printf('%d
', min(dp[n][m][0], dp[n][m][1]));
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, a[200010];
 int main() {
   scanf('%d', &n);
   for (int i = 1; i <= n; i++) scanf('%d', &a[i]);
   sort(a + 1, a + n + 1);
   int p = 2e9 + 7, ans = 0;
   for (int i = 1; i < n; i++) p = min(p, a[i + 1] - a[i]);
   for (int i = 1; i < n; i++) ans += (a[i + 1] - a[i] == p);
   return printf('%d %d', p, ans), 0;
 }


#include <bits/stdc++.h>
 const long long mod = 1000000007;
 const long long Inf = 1e9;
 using namespace std;
 bool valid(string s) {
   for (int i = 0; i < s.length(); ++i) {
     if (s[i] == ' ' || s[i] == '#') {
       if (s[i] == '#') return true;
     } else
       return false;
   }
   return false;
 }
 string wtSpace(string s) {
   string res = '';
   for (int i = 0; i < s.length(); ++i) {
     if (s[i] != ' ') res += s[i];
   }
   return res;
 }
 int main() {
   int n, a, d;
   scanf('%d %d %d', &n, &a, &d);
   vector<double> ans(n);
   for (int i = 0; i < n; i++) {
     int t, v;
     scanf('%d %d', &t, &v);
     double time = pow(2 * d / (double)a, 0.5);
     double time2 = v / (double)a;
     if (time <= time2)
       ans[i] = t + time;
     else {
       double x = a * pow(time2, 2) / 2.0;
       ans[i] = t + time2 + (d - x) / (double)v;
     }
   }
   for (int i = 0; i < n - 1; i++) {
     if (ans[i + 1] < ans[i]) ans[i + 1] = ans[i];
     printf('%.6f
', ans[i]);
   }
   printf('%.6f
', ans[n - 1]);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T, typename U>
 inline void smin(T &a, U b) {
   if (a > b) a = b;
 }
 template <typename T, typename U>
 inline void smax(T &a, U b) {
   if (a < b) a = b;
 }
 template <class T>
 inline void gn(T &first) {
   char c, sg = 0;
   while (c = getchar(), (c > '9' || c < '0') && c != '-')
     ;
   for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9';
        c = getchar())
     first = (first << 1) + (first << 3) + c - '0';
   if (sg) first = -first;
 }
 template <class T, class T1>
 inline void gn(T &first, T1 &second) {
   gn(first);
   gn(second);
 }
 template <class T, class T1, class T2>
 inline void gn(T &first, T1 &second, T2 &z) {
   gn(first);
   gn(second);
   gn(z);
 }
 template <class T>
 inline void print(T first) {
   if (first < 0) {
     putchar('-');
     return print(-first);
   }
   if (first < 10) {
     putchar('0' + first);
     return;
   }
   print(first / 10);
   putchar(first % 10 + '0');
 }
 template <class T>
 inline void printsp(T first) {
   print(first);
   putchar(' ');
 }
 template <class T>
 inline void println(T first) {
   print(first);
   putchar('
');
 }
 template <class T, class U>
 inline void print(T first, U second) {
   printsp(first);
   println(second);
 }
 template <class T, class U, class V>
 inline void print(T first, U second, V z) {
   printsp(first);
   printsp(second);
   println(z);
 }
 int power(int a, int b, int m, int ans = 1) {
   for (; b; b >>= 1, a = 1LL * a * a % m)
     if (b & 1) ans = 1LL * ans * a % m;
   return ans;
 }
 const int B = 2222222;
 int n, u, cnt[B << 1], flag[B];
 long long s[B << 1], first, second;
 long long calc(int p) {
   long long ans = 0;
   int r = max(1, p - (int)(first / second));
   for (int t = 0; t + p < B; t += p) {
     ans += second * ((long long)(cnt[t + p - 1] - cnt[t + r - 1]) * (t + p) -
                      s[t + p - 1] + s[t + r - 1]);
     ans += first * (cnt[t + r - 1] - cnt[t]);
   }
   return ans;
 }
 void solve_B() {
   gn(n, first, second);
   for (int i = 0; i < n; i++) {
     gn(u);
     s[u] += u;
     cnt[u]++;
   }
   for (int i = 1; i < B; i++) s[i] += s[i - 1], cnt[i] += cnt[i - 1];
   long long ans = n * first;
   for (int i = 2; i < B; i++)
     if (!flag[i]) {
       smin(ans, calc(i));
       if ((long long)i * i >= B) continue;
       for (int j = i * i; j < B; j += i) flag[j] = 1;
     }
   println(ans);
   exit(0);
 }
 int main() {
   solve_B();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   cin >> t;
   for (int ii = 0; ii < t; ii++) {
     int n;
     cin >> n;
     bool A[52];
     char Aa[52][52];
     for (int i = 0; i < n; i++) {
       char t;
       cin >> t;
       if (t == '1')
         A[i] = 0;
       else
         A[i] = 1;
     }
     int k = 0;
     int t = -1;
     for (int i = n - 1; i >= 0; i--) {
       if (A[i] == 0) {
         k++;
       } else {
         t = i;
       }
     }
     if (n - k > 2 || n - k == 0)
       cout << 'YES' << endl;
     else {
       cout << 'NO' << endl;
       continue;
     }
     for (int i = 0; i < n; i++) {
       for (int j = 0; j < n; j++) {
         Aa[i][j] = '=';
       }
     }
     for (int i = 0; i < n; i++) Aa[i][i] = 'X';
     int c1 = t;
     if (n - k > 2) {
       for (int i = t + 1; i < n; i++) {
         if (A[i] == 1) {
           Aa[c1][i] = '+';
           Aa[i][c1] = '-';
           c1 = i;
         }
       }
       Aa[c1][t] = '+';
       Aa[t][c1] = '-';
     }
     for (int i = 0; i < n; i++) {
       for (int j = 0; j < n; j++) {
         cout << Aa[i][j];
       }
       cout << endl;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long T;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   cin >> T;
   int a, b, c, d;
   while (T--) {
     cin >> a >> b >> c >> d;
     bool ok = 0;
     if (a == c) {
       if (d + b == a) ok = 1;
     }
     if (a == d) {
       if (c + b == a) ok = 1;
     }
     if (b == c) {
       if (a + d == b) ok = 1;
     }
     if (b == d) {
       if (a + c == b) ok = 1;
     }
     if (ok)
       cout << 'Yes
';
     else
       cout << 'No
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<int> adjc[100005];
 vector<int> cadj[100005];
 struct edge {
   int u, v, c;
 };
 class dsu {
  public:
   dsu(vector<int> vert) : vert(vert) {
     n = vert.size();
     par.resize(n);
     iota(par.begin(), par.end(), 0);
   }
   int root(int u) {
     if (par[u] == u) {
       return u;
     }
     return (par[u] = root(par[u]));
   }
   void merge(int u, int v) {
     int ui = lower_bound(vert.begin(), vert.end(), u) - vert.begin();
     int vi = lower_bound(vert.begin(), vert.end(), v) - vert.begin();
     int ru = root(ui), rv = root(vi);
     if (ru != rv) {
       if (rand() % 2) {
         swap(ru, rv);
       }
       par[ru] = rv;
     }
   }
   bool ced(int u, int v) {
     int ui = lower_bound(vert.begin(), vert.end(), u) - vert.begin();
     int vi = lower_bound(vert.begin(), vert.end(), v) - vert.begin();
     if ((ui == n || (vert[ui] != u)) || (vi == n || (vert[vi] != v))) return 0;
     return (root(ui) == root(vi));
   }
   int n;
   vector<int> vert, par;
 };
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int n, m;
   cin >> n >> m;
   vector<edge> e;
   for (int i = 0; i < m; i++) {
     int u, v, c;
     cin >> u >> v >> c;
     e.push_back({u, v, c});
     cadj[c].push_back(u);
     cadj[c].push_back(v);
     adjc[u].push_back(c);
     adjc[v].push_back(c);
   }
   for (int i = 1; i <= n; i++) {
     sort(adjc[i].begin(), adjc[i].end());
     adjc[i].resize(unique(adjc[i].begin(), adjc[i].end()) - adjc[i].begin());
   }
   vector<dsu> cdsu;
   for (int i = 1; i <= m; i++) {
     sort(cadj[i].begin(), cadj[i].end());
     cadj[i].resize(unique(cadj[i].begin(), cadj[i].end()) - cadj[i].begin());
     cdsu.emplace_back(cadj[i]);
   }
   for (edge ed : e) {
     cdsu[ed.c - 1].merge(ed.u, ed.v);
   }
   map<pair<int, int>, int> dp;
   int q;
   cin >> q;
   while (q--) {
     int u, v;
     cin >> u >> v;
     if (adjc[u].size() > adjc[v].size()) {
       swap(u, v);
     }
     if (dp.find({u, v}) != dp.end()) {
       cout << dp[{u, v}] << endl;
       continue;
     }
     int ans = 0;
     for (auto c : adjc[u]) {
       ans += cdsu[c - 1].ced(u, v);
     }
     cout << ans << endl;
     dp[{u, v}] = ans;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 20 + 5;
 const long long INF = 3e9;
 const char *cc = '0123456789abcdef';
 int c[maxn][maxn], used[maxn];
 long long dp[maxn][maxn];
 char s[maxn];
 long long solve(int t, int m, bool flag) {
   memset(dp[0], 0, sizeof(dp[0]));
   if (flag) {
     for (int i = 0; i < m; ++i) {
       for (int j = 0; j + used[0] <= t && j + i + 1 <= m; ++j) {
         dp[0][i + j] = min(INF, dp[0][i + j] + c[m - i - 1][j]);
       }
     }
   } else
     for (int i = 0; i + used[0] <= t && i <= m; ++i) dp[0][i] = c[m][i];
   for (int i = 1; i < 16; ++i) {
     memset(dp[i], 0, sizeof(dp[i]));
     for (int j = 0; j <= m; ++j) {
       if (!dp[i - 1][j]) continue;
       for (int k = 0; k + used[i] <= t && k + j <= m; ++k) {
         dp[i][j + k] = min(INF, dp[i][j + k] + dp[i - 1][j] * c[m - j][k]);
       }
     }
   }
   return dp[15][m];
 }
 int main() {
   for (int i = 0; i < maxn; ++i) {
     c[i][0] = c[i][i] = 1;
     for (int j = 1; j < i; ++j) c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
   }
   int k, t;
   cin >> k >> t;
   bool flag = true;
   for (int i = 0; i < 20; ++i) {
     long long sum = 0;
     for (int j = 0; j < 16; ++j) {
       if (used[j] >= t) continue;
       if (!flag || j != 0) ++used[j];
       long long tmp = solve(t, 20 - i - 1, flag && j == 0);
       sum += tmp;
       if (sum >= k) {
         s[i] = cc[j];
         k -= sum - tmp;
         if (j > 0) flag = false;
         break;
       }
       if (!flag || j != 0) --used[j];
     }
   }
   s[20] = 0;
   int p = 0;
   while (s[p] == '0') ++p;
   cout << s + p << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 vector<pair<int, int> > inc[100010];
 priority_queue<pair<double, int> > Q;
 long long A[100010];
 int N, M, K;
 vector<pair<int, int> > mul;
 int sel[100010];
 int typ[100010];
 void compute() {
   int k;
   while (M-- && !(Q.empty() && mul.empty())) {
     k = Q.empty() ? -1 : Q.top().second;
     if (mul.empty() || (k >= 0 && Q.top().first >= mul.back().first)) {
       A[k] += inc[k].back().first;
       sel[inc[k].back().second] = 1;
       inc[k].pop_back();
       Q.pop();
       if (!inc[k].empty())
         Q.push(make_pair((A[k] + inc[k].back().first) / (1.0 * A[k]), k));
       continue;
     }
     sel[mul.back().second] = 1;
     mul.pop_back();
   }
 }
 pair<int, int> typ1[100010];
 int main() {
   int i, b, t;
   cin >> K >> N >> M;
   for (i = 0; i < K; ++i) cin >> A[i];
   for (int idx = 0; idx < N; ++idx) {
     cin >> t >> i >> b;
     typ[idx] = t;
     if (t == 3) {
       mul.push_back(make_pair(b, idx));
       continue;
     }
     --i;
     if (t == 1) {
       typ1[i] = max(typ1[i], make_pair(b - (int)A[i], idx));
       continue;
     }
     inc[i].push_back(make_pair(b, idx));
   }
   for (i = 0; i < K; ++i) {
     if (!typ1[i].first) continue;
     inc[i].push_back(typ1[i]);
   }
   sort(mul.begin(), mul.end());
   for (i = 0; i < K; ++i) {
     if (inc[i].empty()) continue;
     sort(inc[i].begin(), inc[i].end());
     Q.push(make_pair((A[i] + inc[i].back().first) / (1.0 * A[i]), i));
   }
   compute();
   cout << accumulate(sel, sel + N, 0) << '
';
   for (i = 0; i < N; ++i) {
     if (!sel[i] || typ[i] > 1) continue;
     cout << i + 1 << ' ';
   }
   for (i = 0; i < N; ++i) {
     if (!sel[i] || typ[i] != 2) continue;
     cout << i + 1 << ' ';
   }
   for (i = 0; i < N; ++i) {
     if (!sel[i] || typ[i] < 3) continue;
     cout << i + 1 << ' ';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 char a[102];
 char b[102];
 char c[102];
 int main() {
   gets(a);
   gets(b);
   int l = strlen(a);
   int i;
   for (i = l - 1; i >= 0; i--) {
     if (a[i] == 'z') {
       a[i] = 'a';
     } else {
       a[i] = a[i] + 1;
       break;
     }
   }
   if (!strcmp(a, b))
     cout << 'No such string';
   else
     cout << a;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 500005;
 int a[N];
 int main() {
   int n;
   while (scanf('%d', &n) != EOF) {
     memset(a, 0, sizeof(a));
     for (int i = 0; i < n; i++) {
       scanf('%d', &a[i]);
     }
     sort(a, a + n);
     int mid = (n - 1) / 2;
     int big = n - 1;
     int small = mid;
     int ok = 0;
     while (big > mid && small >= 0) {
       if (a[small] * 2 <= a[big]) {
         ok++;
         small--;
         big--;
       } else {
         small--;
       }
     }
     printf('%d
', n - ok);
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, sol;
 multiset<int> S;
 multiset<int>::iterator p;
 int main() {
   scanf('%d', &n);
   for (int i = 1; i <= n; i++) {
     int val;
     scanf('%d', &val);
     S.insert(val);
   }
   long long s = 0;
   for (int i = 1; i <= n; i++) {
     p = S.lower_bound(s);
     if (p == S.end()) break;
     if (*p < s) p++;
     sol++;
     s += *p;
     S.erase(p);
   }
   printf('%d
', sol);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long bz = -10000000000;
 long long t[50001], ll[50001], rr[50001], dd[50001];
 long long tu[50001];
 int main() {
   long long n, m;
   cin >> n >> m;
   for (long long a = 1; a <= n; a++) tu[a] = bz;
   for (long long a = 1; a <= m; a++) cin >> t[a] >> ll[a] >> rr[a] >> dd[a];
   for (long long a = 1; a <= m; a++) {
     if (t[a] == 1) {
       for (long long b = ll[a]; b <= rr[a]; b++) {
         if (tu[b] == bz)
           continue;
         else
           tu[b] += dd[a];
       }
     } else {
       long long jc = 1;
       for (long long b = ll[a]; b <= rr[a]; b++) {
         if (tu[b] == bz) {
           tu[b] = dd[a];
           jc = 0;
         } else {
           if (tu[b] >= dd[a]) {
             tu[b] = dd[a];
             jc = 0;
           }
         }
       }
       if (jc) {
         cout << 'NO';
         return 0;
       }
     }
   }
   for (long long a = 1; a <= n; a++)
     if (tu[a] == bz) tu[a] = 0;
   for (long long a = m; a >= 1; a--) {
     if (t[a] == 2) {
       long long jc = 1;
       for (long long b = ll[a]; b <= rr[a]; b++) {
         if (tu[b] == dd[a]) jc = 0;
       }
       if (jc) {
         cout << 'NO';
         return 0;
       }
       continue;
     }
     for (long long b = ll[a]; b <= rr[a]; b++)
       if (tu[a] != bz) tu[b] -= dd[a];
   }
   cout << 'YES' << endl;
   long long jc = 0;
   for (long long a = 1; a <= n; a++) {
     if (tu[a] == bz) tu[a] = 0;
     if (!jc) {
       cout << tu[a];
       jc = 1;
     } else {
       cout << ' ' << tu[a];
     }
   }
 }


#include <bits/stdc++.h>
 #pragma GCC optimize('Ofast')
 const int N = 4040;
 using namespace std;
 int n, m;
 int l[N], s[N];
 int cost[N];
 int inf = -1e9;
 int d[N][N];
 int f[N][N];
 vector<int> v[N];
 vector<int> pos[N];
 vector<int> fut[N][14];
 int main() {
   ios_base::sync_with_stdio(0);
   cin >> n >> m;
   for (int i = 1; i <= n; i++) cin >> l[i];
   for (int i = 1; i <= n; i++) cin >> s[i];
   reverse(l + 1, l + n + 1);
   reverse(s + 1, s + n + 1);
   for (int i = 1; i <= n + m; i++) cin >> cost[i];
   for (int i = 1; i <= n; i++) v[l[i]].push_back(i);
   for (int i = 0; i < N; i++)
     for (int j = 0; j < N; j++) d[i][j] = inf;
   for (int i = 1; i <= n + m; i++) {
     for (int j = 0; j < 13; j++) {
       fut[i][j].resize((1 << 13 - j));
       while (fut[i][j].size() >= N - 4) fut[i][j].pop_back();
       for (auto &x : fut[i][j]) x = inf;
     }
   }
   for (int i = 0; i < N; i++) {
     for (int j = 1; j <= n + m; j++) {
       f[i][j] = cost[j];
       for (int h = 0; h < 13; h++) {
         if (i & (1 << h))
           f[i][j] += cost[j + h + 1];
         else
           break;
       }
     }
   }
   for (int i = 1; i <= n; i++) {
     for (int j = 1; j <= l[i]; j++) {
       int dif = min(12, l[i] - j);
       for (int p = 0; p < fut[j][dif].size(); p++) {
         d[p + 1][i] = max(d[p + 1][i], fut[j][dif][p] + f[p][l[i]] - s[i]);
       }
     }
     d[1][i] = max(d[1][i], cost[l[i]] - s[i]);
     for (int j = 0; j < N; j++) {
       int nj = j, x = l[i];
       for (int h = 0; h < 13; h++) {
         fut[x][h][nj] = max(fut[x][h][nj], d[j][i]);
         nj >>= 1;
       }
     }
   }
   int res = 0;
   for (int i = 0; i < N; i++) {
     for (int j = 1; j <= n; j++) {
       res = max(res, d[i][j]);
     }
   }
   cout << res << '
';
 }


#include <bits/stdc++.h>
 using namespace std;
 long long int XpowerY(long long int x, long long int y, long long int m) {
   long long int ans = 1;
   x = x % m;
   while (y > 0) {
     if (y % 2 == 1) ans = (ans * x) % m;
     x = ((x % m) * (x % m)) % m;
     y = y >> 1;
   }
   return ans % m;
 }
 vector<long long int> gr[100023];
 long long int s[100023], val[100023];
 long long int fl, minP;
 long long int dfs(long long int node, long long int prev, long long int lev) {
   long long int mx = 1e12;
   if (lev % 2 == 0) {
     for (int i = 0; i < gr[node].size(); i++) {
       if (s[gr[node][i]] < prev) fl = 1;
       mx = min(mx, s[gr[node][i]] - prev);
     };
     val[node] = mx;
     if (gr[node].size() == 0) val[node] = 0;
   }
   for (int i = 0; i < gr[node].size(); i++) {
     val[gr[node][i]] = s[gr[node][i]] - mx - prev;
     long long int temp;
     if (lev % 2 == 0)
       temp = prev + val[node];
     else
       temp = s[node];
     dfs(gr[node][i], temp, lev + 1);
   }
   return 0;
 }
 int main() {
   long long int n, x;
   cin >> n;
   for (long long int i = 0; i < n - 1; i++) {
     cin >> x;
     gr[x].push_back(i + 2);
   }
   for (long long int i = 1; i <= n; i++) cin >> s[i];
   dfs(1, -1, 1);
   val[1] = s[1];
   if (fl) return cout << -1 << endl, 0;
   for (long long int i = 0; i < n; i++) {
     ;
     minP += val[i + 1];
   }
   cout << minP << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int ALPHA = 26 + 1;
 struct Trie {
   Trie() { nodes.resize(2); }
   struct Node {
     Node() { memset(next, 0, sizeof(next)); }
     int next[ALPHA];
     int finish = -1;
   };
   int alloc() {
     nodes.push_back(Node());
     return nodes.size() - 1;
   }
   void addWord(const string& s, int id) {
     int v = root;
     for (char c : s) {
       c = tolower(c) - 'a';
       if (!nodes[v].next[c]) {
         int x = alloc();
         nodes[v].next[c] = x;
       }
       v = nodes[v].next[c];
     }
     nodes[v].finish = id;
   }
   vector<Node> nodes;
   const int root = 1;
 };
 int n;
 string ciph;
 int m;
 Trie trie;
 vector<int> solved;
 vector<int> toTake;
 bool solve(int pos) {
   if (pos == n) {
     return true;
   }
   if (solved[pos] == -1) {
     solved[pos] = false;
     int v = trie.root;
     int i = pos;
     while (i < ciph.length() && trie.nodes[v].next[ciph[i] - 'a']) {
       v = trie.nodes[v].next[ciph[i] - 'a'];
       ++i;
       if (trie.nodes[v].finish != -1) {
         if (solve(i)) {
           solved[pos] = true;
           toTake[pos] = trie.nodes[v].finish;
         }
       }
     }
   }
   return solved[pos];
 }
 int main() {
   ios::sync_with_stdio(false);
   cin >> n;
   solved.resize(n, -1);
   toTake.resize(n, -1);
   cin >> ciph;
   cin >> m;
   vector<string> words(m);
   for (int i = 0; i < m; ++i) {
     cin >> words[i];
     trie.addWord(words[i], i);
   }
   reverse(ciph.begin(), ciph.end());
   solve(0);
   vector<int> ans;
   if (solved[0] == 1) {
     int pos = 0;
     while (pos != n) {
       int id = toTake[pos];
       ans.push_back(id);
       pos += words[id].length();
     }
   } else {
     assert(false);
   }
   reverse(ans.begin(), ans.end());
   for (int i = 0; i < ans.size(); ++i) {
     if (i != 0) {
       cout << ' ';
     }
     cout << words[ans[i]];
   }
   cout << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long x, y, m, cnt = 0;
 int main() {
   scanf('%lld%lld%lld', &x, &y, &m);
   if (x >= m || y >= m) {
     printf('0');
     return 0;
   }
   if (x <= 0 && y <= 0) {
     printf('-1');
     return 0;
   }
   while (x < m && y < m) {
     if (x > y) swap(x, y);
     if (x + y <= 0) {
       cnt += abs(x) / y;
       x = x + (abs(x) - abs(x) % y);
     } else {
       x += y;
       cnt++;
       ;
     }
   }
   printf('%lld', cnt);
   return 0;
 }


#include <bits/stdc++.h>
 #pragma comment(linker, '/STACK:256000000')
 using namespace std;
 const int maxN = 2000;
 const int S_CNT = 9;
 const int M_CNT = 25;
 const int DIST = 60 * 60;
 int n, m;
 int a[maxN][maxN];
 int t[maxN][maxN];
 int used[maxN][maxN];
 int rc[maxN][maxN];
 int is_in(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
 int get(int x, int y) {
   int cnt = 0;
   for (int i = x - 2; i <= x + 2; ++i) {
     for (int j = y - 2; j <= y + 2; ++j) {
       if (is_in(i, j)) {
         cnt += a[i][j];
       }
     }
   }
   return cnt;
 }
 pair<int, int> choose_max(int x, int y) {
   pair<int, int> res = make_pair(-1, -1);
   int rs = -1;
   for (int i = x - 2; i <= x + 2; ++i) {
     for (int j = y - 2; j <= y + 2; ++j) {
       if (is_in(i, j)) {
         if (t[i][j] > rs) {
           rs = t[i][j];
           res = make_pair(i, j);
         }
       }
     }
   }
   return res;
 }
 void change_used(int x, int y, int num) {
   for (int i = x - 2; i <= x + 2; ++i) {
     for (int j = y - 2; j <= y + 2; ++j) {
       if (is_in(i, j)) {
         used[i][j] = num;
       }
     }
   }
 }
 int dist(pair<int, int> a, pair<int, int> b) {
   return (a.first - b.first) * (a.first - b.first) +
          (a.second - b.second) * (a.second - b.second);
 }
 pair<int, int> choose_long(int cx, int cy, int x, int y, int step) {
   pair<int, int> res = make_pair(cx, cy);
   int rs = -1;
   for (int i = cx - 2; i <= cx + 2; ++i) {
     for (int j = cy - 2; j <= cy + 2; ++j) {
       if (is_in(i, j) && a[i][j] && used[i][j] != step) {
         if (dist(make_pair(i, j), make_pair(x, y)) > rs) {
           rs = dist(make_pair(i, j), make_pair(x, y));
           res = make_pair(i, j);
         }
       }
     }
   }
   return res;
 }
 void dfs(int x, int y, int color) {
   if (rc[x][y] != 0) return;
   rc[x][y] = color;
   if (is_in(x + 1, y) && a[x + 1][y]) dfs(x + 1, y, color);
   if (is_in(x - 1, y) && a[x - 1][y]) dfs(x - 1, y, color);
   if (is_in(x, y + 1) && a[x][y + 1]) dfs(x, y + 1, color);
   if (is_in(x, y - 1) && a[x][y - 1]) dfs(x, y - 1, color);
 }
 int main() {
   scanf('%d%d', &n, &m);
   for (int i = 0; i < n; ++i) {
     for (int j = 0; j < m; ++j) {
       scanf('%d', &a[i][j]);
     }
   }
   int c_col = 0;
   for (int i = 0; i < n; ++i) {
     for (int j = 0; j < m; ++j) {
       if (a[i][j] && !rc[i][j]) {
         ++c_col;
         dfs(i, j, c_col);
       }
     }
   }
   for (int i = 0; i < n; ++i) {
     for (int j = 0; j < m; ++j) {
       if (a[i][j]) {
         t[i][j] = get(i, j);
       }
     }
   }
   vector<pair<int, int> > cur;
   int step = 1;
   for (int i = 0; i < n; ++i) {
     for (int j = 0; j < m; ++j) {
       if (a[i][j] && !used[i][j] && t[i][j] == S_CNT) {
         ++step;
         int cx = i, cy = j;
         change_used(cx, cy, step);
         cur.push_back(make_pair(cx, cy));
       }
     }
   }
   vector<int> col(cur.size(), 0);
   for (int i = 0; i < col.size(); ++i) {
     col[i] = rc[cur[i].first][cur[i].second];
   }
   map<int, int> res;
   for (int i = 0; i < col.size(); ++i) {
     ++res[col[i]];
   }
   cout << res.size() << endl;
   vector<int> ans;
   for (map<int, int>::iterator it = res.begin(); it != res.end(); ++it) {
     ans.push_back(it->second);
   }
   sort(ans.begin(), ans.end());
   for (int i = 0; i < ans.size(); ++i) {
     cout << ans[i] << ' ';
   }
   cout << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 template <class T, class L>
 bool smax(T &x, L y) {
   return x < y ? (x = y, 1) : 0;
 }
 template <class T, class L>
 bool smin(T &x, L y) {
   return y < x ? (x = y, 1) : 0;
 }
 const int maxn = 5e5 + 17;
 list<int> a;
 list<int>::iterator per[maxn];
 int n;
 int main() {
   ios::sync_with_stdio(0), cin.tie(0);
   cin >> n;
   for (int i = 0, x; i < n; i++) cin >> x, a.push_back(x), per[i] = --a.end();
   sort(per, per + n,
        [](list<int>::iterator &i, list<int>::iterator &j) { return *i < *j; });
   long long ans = 0, dam = 0;
   for_each(per, per + max(0, n - 2), [&](list<int>::iterator &i) {
     if (i == a.begin() || i == --a.end()) {
       ans += (a.size() - 2) * (*i - dam);
       dam = *i;
     } else {
       auto prv = i, nxt = i;
       prv--, nxt++;
       ans += min(*prv, *nxt) - dam;
     }
     a.erase(i);
   });
   cout << ans << '
';
   return 0;
 }


#include <bits/stdc++.h>
 int main() {
   int t;
   scanf('%d', &t);
   while (t--) {
     int n;
     scanf('%d', &n);
     std::vector<int> a(n);
     for (int i = 0; i < n; i++) {
       scanf('%d', &a[i]);
     }
     int minadd = 0;
     int cur = a[0];
     for (int i = 1; i < n; i++) {
       if (cur > a[i]) {
         minadd = std::max(minadd, cur - a[i]);
       }
       cur = std::max(cur, a[i]);
     }
     int nBits = 0;
     while (minadd) {
       nBits++;
       minadd >>= 1;
     }
     printf('%d
', nBits);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct Ans {
   long long maxm, maxp, lmr, rml;
   Ans() { maxm = maxp = lmr = rml = -(1ll << 60); }
 } node[100010 << 2];
 void Union(Ans &o, Ans a, Ans b) {
   o.maxm = max(a.maxm, b.maxm);
   o.maxp = max(a.maxp, b.maxp);
   o.lmr = max(max(a.lmr, b.lmr), a.maxp + b.maxm);
   o.rml = max(max(a.rml, b.rml), a.maxm + b.maxp);
 }
 long long h[100010], d[100010], n;
 void build(int o, int l, int r) {
   if (l == r) {
     node[o].maxm = 2 * h[l] - (long long)d[l];
     node[o].maxp = 2 * h[l] + (long long)d[l];
     node[o].rml = -(1ll << 60);
     node[o].lmr = -(1ll << 60);
     return;
   }
   build((o << 1), l, ((l + r) >> 1));
   build((o << 1 | 1), ((l + r) >> 1) + 1, r);
   Union(node[o], node[(o << 1)], node[(o << 1 | 1)]);
 }
 Ans query(int L, int R, int o = 1, int l = 1, int r = n) {
   Ans ret;
   if (L > R) return ret;
   if (L <= l && r <= R) return node[o];
   if (R <= ((l + r) >> 1)) return query(L, R, (o << 1), l, ((l + r) >> 1));
   if (L > ((l + r) >> 1))
     return query(L, R, (o << 1 | 1), ((l + r) >> 1) + 1, r);
   Union(ret, query(L, R, (o << 1), l, ((l + r) >> 1)),
         query(L, R, (o << 1 | 1), ((l + r) >> 1) + 1, r));
   return ret;
 }
 int Q;
 Ans ansl, ansr;
 int main() {
   scanf('%d%d', &n, &Q);
   for (int i = 1; i <= n; i++) {
     cin >> d[i + 1];
     d[i + 1] += d[i];
   }
   for (int i = 1; i <= n; i++) cin >> h[i];
   build(1, 1, n);
   while (Q--) {
     int l, r;
     scanf('%d %d', &l, &r);
     if (l <= r) {
       ansl = query(r + 1, n);
       ansr = query(1, l - 1);
       cout << max(max(ansr.rml, ansl.rml), ansr.maxp + d[n + 1] + ansl.maxm)
            << endl;
     } else {
       cout << query(r + 1, l - 1).rml << endl;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, m, tot;
 long long ans;
 int main(int argc, char **argv) {
   scanf('%d%d', &n, &m);
   ++n;
   for (int i = m + 1; (i << 1) <= n && i < n - m; ++i)
     for (int j = m + 1; j < n - m; ++j) {
       tot = (-i * i * 2 + i * j + i * n * 2 - j * n - 1) / (i + j);
       ans += max(min(tot, n - m - 1) - m, 0) * ((i << 1) == n ? 1 : 2);
     }
   printf('%I64d
', ans * 3);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int A, B, C, N;
 int main() {
   scanf('%d%d%d%d', &A, &B, &C, &N);
   if (A < C || B < C || A + B >= N + C)
     puts('-1');
   else
     printf('%d
', N - (A + B - C));
 }


#include <bits/stdc++.h>
 using namespace std;
 template <int mod>
 struct modulo {
   int w;
   operator int() const { return w; }
   modulo() {}
   modulo(int w) {
     this->w = w;
     fix();
   }
   template <class type>
   modulo(type w) {
     this->w = w % mod;
     fix();
   }
   void inline fix() {
     if (w >= mod || w < -mod) w %= mod;
     if (w < 0) w += mod;
   }
   inline modulo operator-() const { return mod - w; }
   inline modulo &operator+=(const modulo &b) {
     if ((w += b.w) >= mod) w -= mod;
     return *this;
   }
   inline modulo &operator-=(const modulo &b) {
     if ((w -= b.w) < 0) w += mod;
     return *this;
   }
   inline modulo &operator*=(const modulo &b) {
     w = (unsigned long long)w * b.w % mod;
     return *this;
   }
   inline modulo &operator/=(const modulo &b) { return *this *= inv(b); }
   friend modulo operator+(const modulo &a, const modulo &b) {
     return modulo(a) += b;
   }
   template <class type>
   friend modulo operator+(const modulo &a, const type &b) {
     return modulo(a) += b;
   }
   template <class type>
   friend modulo operator+(const type &a, const modulo &b) {
     return modulo(a) += b;
   }
   friend modulo operator-(const modulo &a, const modulo &b) {
     return modulo(a) -= b;
   }
   template <class type>
   friend modulo operator-(const modulo &a, const type &b) {
     return modulo(a) -= b;
   }
   template <class type>
   friend modulo operator-(const type &a, const modulo &b) {
     return modulo(a) -= b;
   }
   friend modulo operator*(const modulo &a, const modulo &b) {
     return modulo(a) *= b;
   }
   template <class type>
   friend modulo operator*(const modulo &a, const type &b) {
     return modulo(a) *= b;
   }
   template <class type>
   friend modulo operator*(const type &a, const modulo &b) {
     return modulo(a) *= b;
   }
   friend modulo operator/(const modulo &a, const modulo &b) {
     return modulo(a) /= b;
   }
   template <class type>
   friend modulo operator/(const modulo &a, const type &b) {
     return modulo(a) /= b;
   }
   template <class type>
   friend modulo operator/(const type &a, const modulo &b) {
     return modulo(a) /= b;
   }
   friend inline modulo inv(const modulo &w) {
     int a = w, m = mod, u = 0, v = 1, t;
     for (; a != 0; t = m / a, m -= t * a, swap(a, m), u -= t * v, swap(u, v))
       ;
     return u;
   }
   friend inline modulo pow(const modulo &w, int p) {
     if (!w) return 0;
     modulo a = w, r = 1;
     for (; p > 0; a *= a, p >>= 1)
       if (p & 1) r *= a;
     return r;
   }
 };
 const int mod = 998244353;
 const int nsz = 5e3;
 int n;
 modulo<mod> f[2][nsz + 5], g[nsz + 5], S[nsz + 5][nsz + 5], ans[nsz + 5];
 modulo<mod> inline C(int n, int m) { return f[0][n] * f[1][m] * f[1][n - m]; }
 void inline init() {
   f[0][0] = 1;
   for (int i = 1; i <= n; ++i) f[0][i] = f[0][i - 1] * i;
   f[1][n] = 1 / f[0][n];
   for (int i = n; i >= 1; --i) f[1][i - 1] = f[1][i] * i;
   S[0][0] = 1;
   for (int i = 1; i <= n; ++i) {
     for (int j = 1; j <= i; ++j) S[i][j] = S[i - 1][j] * j + S[i - 1][j - 1];
   }
   for (int i = 0; i <= n; ++i) {
     for (int len = i + 1; len <= n; ++len)
       g[i] += f[0][len - i] * f[1][len] * S[len][len - i];
   }
 }
 void inline solve() {
   for (int k = 0; k < n; ++k) {
     for (int i = k; i <= n; ++i) {
       modulo<mod> res = g[i] * C(i, k) * f[0][n];
       ans[k + 1] += (i - k) & 1 ? -res : res;
     }
   }
 }
 int main() {
   scanf('%d', &n);
   init();
   solve();
   for (int k = 1; k <= n; ++k) printf('%d%c', ans[k].w, '
 '[k < n]);
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, m;
 vector<int> prices[1010];
 int allp[1010];
 int all;
 int margin_price;
 int above_cnt[1010], margin_cnt[1010], cnt[1010];
 double f[1010][1010], g[1010][1010];
 double comb[1010][1010];
 int main() {
   comb[1][0] = comb[1][1] = 1;
   for (int i = 2; i <= 1000; i++) {
     comb[i][0] = comb[i][i] = 1;
     for (int j = 1; j < i; j++)
       comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
   }
   scanf('%d%d', &n, &m);
   for (int i = 1; i <= m; i++) {
     int k;
     scanf('%d', &k);
     while (k--) {
       int v;
       scanf('%d', &v);
       prices[i].push_back(v);
       allp[++all] = v;
     }
   }
   sort(allp + 1, allp + all + 1);
   reverse(allp + 1, allp + all + 1);
   margin_price = allp[n];
   int need_margin = 0;
   for (int i = 1; i <= n; i++)
     if (allp[i] == margin_price) need_margin++;
   for (int i = 1; i <= m; i++) {
     for (int p : prices[i]) {
       if (p > margin_price)
         above_cnt[i]++;
       else if (p == margin_price)
         margin_cnt[i]++;
       cnt[i]++;
     }
   }
   f[0][0] = 1;
   g[0][0] = 1;
   for (int i = 1; i <= m; i++) {
     for (int j = 0; j <= need_margin; j++) {
       if (margin_cnt[i] == 0) {
         f[i][j] += f[i - 1][j] / comb[cnt[i]][above_cnt[i]];
         g[i][j] += g[i - 1][j];
       } else if (margin_cnt[i] == 1) {
         f[i][j] += f[i - 1][j] / comb[cnt[i]][above_cnt[i]];
         g[i][j] += g[i - 1][j];
         f[i][j + 1] += f[i - 1][j] / comb[cnt[i]][above_cnt[i] + 1];
         g[i][j + 1] += g[i - 1][j];
       } else {
         assert(false);
       }
     }
   }
   printf('%.15f
', f[m][need_margin] / g[m][need_margin]);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int INF = 1000000005;
 long long INFF = 1000000000000000005LL;
 int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
 int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
 double pi = 3.1415926536;
 int MOD = 1e9 + 7;
 long long power(long long x, long long n) {
   long long res = 1;
   while (n > 0) {
     if (n & 1) res = res * x;
     x = (x * x);
     n /= 2;
   }
   return res;
 }
 bool ok(int n, long long d) {
   long long sum = (n * (n + 1) / 2);
   if (sum < d) return false;
   return (d % 2 == sum % 2);
 }
 int main() {
   ios::sync_with_stdio(false);
   cin.tie();
   cout.tie();
   int t;
   cin >> t;
   while (t--) {
     int a, b;
     cin >> a >> b;
     int i = 0;
     while (!ok(i, abs(a - b))) i++;
     cout << i << '
';
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 void input() {
   int n;
   cin >> n;
   vector<int> v;
   int i = 1;
   int k = n - 1;
   while (k--) {
     cout << '? ' << i << ' ' << i + 1 << '
';
     fflush(stdout);
     int x;
     cin >> x;
     v.push_back(x);
     i++;
   }
   cout << '? ' << 1 << ' ' << 3 << '
';
   fflush(stdout);
   int x;
   cin >> x;
   int a[n + 1];
   a[1] = x - v[1];
   for (int i = 2; i <= n; i++) {
     a[i] = v[i - 2] - a[i - 1];
   }
   cout << '! ';
   for (int i = 1; i <= n; i++) {
     cout << a[i] << ' ';
   }
   cout << '
';
 }
 int main() {
   input();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long dp[100010];
 int a[310], in[310];
 long long tmp[310];
 struct edge {
   int to;
   int next;
 } e[310];
 int box[100010], cnt;
 long long T;
 void init() {
   memset(box, -1, sizeof(box));
   memset(in, 0, sizeof(in));
   memset(dp, 0, sizeof(dp));
   cnt = 0;
 }
 void add(int from, int to) {
   e[cnt].to = to;
   e[cnt].next = box[from];
   box[from] = cnt++;
 }
 long long dfs(int now) {
   tmp[now] = a[now];
   int t, v;
   for (t = box[now]; t + 1; t = e[t].next) {
     v = e[t].to;
     long long tt = dfs(v);
     tmp[now] += tt;
   }
   T -= tmp[now];
   return tmp[now];
 }
 bool iscircle(int n) {
   int qu[310], tmp[310];
   int i, sum = 0, top = 0;
   for (i = 1; i <= n; i++) {
     tmp[i] = in[i];
     if (tmp[i] == 0) qu[top++] = i;
   }
   for (i = 0; i < top; i++) {
     int now = qu[i], t, v;
     for (t = box[now]; t + 1; t = e[t].next) {
       v = e[t].to;
       tmp[v]--;
       if (!tmp[v]) qu[top++] = v;
     }
   }
   if (top < n) return true;
   return false;
 }
 int main() {
   int n, q, i, aa, b, j;
   scanf('%d%d%I64d', &n, &q, &T);
   for (i = 1; i <= n; i++) scanf('%d', &a[i]);
   init();
   for (i = 1; i <= q; i++) {
     scanf('%d%d', &aa, &b);
     in[aa]++;
     add(b, aa);
   }
   bool tru = iscircle(n);
   for (i = 1; i <= n; i++) {
     if (!in[i]) {
       dfs(i);
       T += tmp[i];
     }
   }
   if (tru || T < 0)
     printf('0
');
   else {
     dp[0] = 1;
     for (i = 1; i <= n; i++) {
       for (j = tmp[i]; j <= T; j++)
         dp[j] = (dp[j] + dp[j - tmp[i]]) % 1000000007;
     }
     printf('%I64d
', dp[T]);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int m[26];
 int main() {
   float a, c, d, b;
   cin >> a >> b >> c >> d;
   float sa = max((3 * a) / 10.0, a - (a / 250.0) * c);
   float sb = max((3 * b) / 10.0, b - (b / 250.0) * d);
   if (sa > sb)
     cout << 'Misha';
   else if (sb > sa)
     cout << 'Vasya';
   else
     cout << 'Tie';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 3e5 + 5;
 int n, m;
 int first[maxn], second[maxn];
 void solve() {
   cin >> n >> m;
   for (int i = 1; i <= m; ++i) cin >> first[i] >> second[i];
   int a, b;
   a = first[1];
   b = -1;
   for (int i = 1; i <= m; ++i) {
     if (first[i] != a && second[i] != a) {
       if (first[i] != b && second[i] != b) {
         if (b == -1)
           b = first[i];
         else
           break;
       }
     }
     if (i == m) {
       puts('YES');
       return;
     }
   }
   a = -1;
   b = second[1];
   for (int i = 1; i <= m; ++i) {
     if (first[i] != b && second[i] != b) {
       if (first[i] != a && second[i] != a) {
         if (a == -1)
           a = first[i];
         else
           break;
       }
     }
     if (i == m) {
       puts('YES');
       return;
     }
   }
   a = -1;
   b = second[1];
   for (int i = 1; i <= m; ++i) {
     if (first[i] != b && second[i] != b) {
       if (first[i] != a && second[i] != a) {
         if (a == -1)
           a = second[i];
         else
           break;
       }
     }
     if (i == m) {
       puts('YES');
       return;
     }
   }
   a = first[1];
   b = -1;
   for (int i = 1; i <= m; ++i) {
     if (first[i] != a && second[i] != a) {
       if (first[i] != b && second[i] != b) {
         if (b == -1)
           b = second[i];
         else
           break;
       }
     }
     if (i == m) {
       puts('YES');
       return;
     }
   }
   puts('NO');
 }
 int main() { solve(); }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int x, y;
   cin >> x >> y;
   if (((x < y) ? x : y) % 2)
     cout << 'Akshat';
   else
     cout << 'Malvika';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int P = 10, N = 1e5 + 10;
 int inverse(int a, int m) {
   int m0 = m, t, q;
   int x0 = 0, x1 = 1;
   if (m == 1) return 0;
   while (a > 1) {
     q = a / m;
     t = m;
     m = a % m, a = t;
     t = x0;
     x0 = x1 - q * x0;
     x1 = t;
   }
   if (x1 < 0) x1 += m0;
   return x1;
 }
 int fact[N], ifact[N], pot[N][P];
 vector<int> primes(int n) {
   vector<int> ans;
   for (int d = 2; d * d <= n; d++)
     if (n % d == 0) {
       ans.push_back(d);
       while (n % d == 0) {
         n /= d;
       }
     }
   if (n > 1) ans.push_back(n);
   return ans;
 }
 int mod;
 vector<int> p;
 int expomod(long long int a, long long int e) {
   int ans = 1;
   while (e) {
     if (e & 1) ans = ans * a % mod;
     e /= 2;
     a = a * a % mod;
   }
   return ans;
 }
 int comb(int m, int n) {
   int ans = (long long int)fact[m] * ifact[n] % mod * ifact[m - n] % mod;
   for (int i = 0; i < (int)(((int)(p).size())); i++) {
     int pp = pot[m][i] - pot[n][i] - pot[m - n][i];
     if (pp) ans = (long long int)ans * expomod(p[i], pp) % mod;
   }
   return ans;
 }
 int go(int n, int bal) {
   long long int ans = 0;
   for (int x = 0, y = bal; x + y <= n; x++, y++) {
     ans += (long long int)comb(n, x) * comb(n - x, y) % mod;
     if (ans >= mod) ans -= mod;
   }
   return ans;
 }
 int main() {
   int n, l, r;
   cin >> n >> mod >> l >> r;
   p = primes(mod);
   fact[0] = ifact[0] = 1;
   for (int i = 1; i <= n; i++) {
     int d = -1;
     for (int j = 0; j < (int)(((int)(p).size())); j++)
       if (i % p[j] == 0) {
         d = j;
         break;
       }
     if (d == -1)
       fact[i] = i;
     else {
       fact[i] = fact[i / p[d]];
       for (int j = 0; j < (int)(((int)(p).size())); j++)
         pot[i][j] = pot[i / p[d]][j] + (j == d);
     }
     ifact[i] = inverse(fact[i], mod);
   }
   for (int i = 0; i < (int)(n); i++) {
     fact[i + 1] = (long long int)fact[i] * fact[i + 1] % mod;
     ifact[i + 1] = (long long int)ifact[i] * ifact[i + 1] % mod;
     for (int j = 0; j < (int)(((int)(p).size())); j++)
       pot[i + 1][j] += pot[i][j];
   }
   long long int ans = 0;
   for (int i = 0; i < (int)(2); i++) {
     ans += go(n, l + i);
     ans -= go(n, r + 1 + i);
   }
   ans %= mod;
   if (ans < 0) ans += mod;
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long a[101][101];
 int main() {
   long long n, m;
   cin >> n >> m;
   long long mn = min(m, n);
   cout << mn + 1 << endl;
   for (long long i = 0; i <= mn; i++)
     cout << i << ' ' << (mn - i) % (mn + 1) << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 char _;
 long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
 long long fpow(long long b, long long exp, long long mod) {
   if (exp == 0) return 1;
   long long t = fpow(b, exp / 2, mod);
   if (exp & 1) return t * t % mod * b % mod;
   return t * t % mod;
 }
 int num_boxes, req, start, vals[55], dp[55][2005];
 char type[55];
 int solve(int box, int sum) {
   if (sum >= req) return 0;
   if (dp[box][sum] != -1) return dp[box][sum];
   int res = INT_MAX;
   for (int i = 1; i <= num_boxes; i++)
     if (vals[i] > vals[box] && type[i] != type[box])
       if (solve(i, sum + vals[i]) != INT_MAX)
         res = min(res, abs(i - box) + solve(i, sum + vals[i]));
   return dp[box][sum] = res;
 }
 int main() {
   scanf('%d %d %d', &num_boxes, &start, &req);
   for (int i = 1; i <= num_boxes; i++) scanf(' %d', &vals[i]);
   for (int i = 1; i <= num_boxes; i++) scanf(' %c', &type[i]);
   memset(dp, -1, sizeof dp);
   int res = solve(start, vals[start]);
   for (int i = 1; i <= num_boxes; i++)
     if (solve(i, vals[i]) != INT_MAX)
       res = min(res, abs(i - start) + solve(i, vals[i]));
   printf('%d
', (res == INT_MAX ? -1 : res));
 }


#include <bits/stdc++.h>
 using namespace std;
 template <typename T>
 void maxE(T& a, const T& b) {
   a = max(a, b);
 }
 template <typename T>
 void minE(T& a, const T& b) {
   a = min(a, b);
 }
 template <typename T>
 ostream& operator<<(ostream& out, const vector<T>& t_) {
   out << '[';
   for (auto i : t_) out << i << ', ';
   out << ']';
   return out;
 }
 template <typename S, typename T>
 ostream& operator<<(ostream& out, const pair<S, T>& rhs) {
   out << '(' << rhs.first << ',' << rhs.second << ')';
   return out;
 }
 int gcd(int a, int b) {
   while (a && b) a > b ? a %= b : b %= a;
   return a + b;
 }
 map<int, int> t;
 void proc(int l, int c) {
   if (0)
     cout << 'proc '
          << 'l'
          << ' ' << l << ' '
          << 'c'
          << ' ' << c << ' ' << endl;
   if (t.find(l) == t.end()) {
     t[l] = c;
     if (0) cout << 'init0' << endl;
   } else if (t[l] > c) {
     if (0) cout << 'relax1' << endl;
     t[l] = c;
   }
   vector<pair<int, int> > tmp;
   for (auto i : t) tmp.push_back(i);
   for (auto i : tmp) {
     int g = gcd(i.first, l);
     if (t.find(g) == t.end() || t[g] > i.second + c) {
       if (0)
         cout << 'relax2 '
              << 'i'
              << ' ' << i << ' '
              << 'g'
              << ' ' << g << ' '
              << 'l'
              << ' ' << l << ' '
              << 'c'
              << ' ' << c << ' '
              << 'i.nd + c'
              << ' ' << i.second + c << ' ' << endl;
       t[g] = i.second + c;
     }
   }
 }
 int main() {
   int n;
   assert(scanf('%d', &n) == 1);
   int l[n];
   for (int i = 0; i < int(n); ++i) assert(scanf('%d', l + i) == 1);
   int c[n];
   for (int i = 0; i < int(n); ++i) assert(scanf('%d', c + i) == 1);
   for (int i = 0; i < int(n); ++i) proc(l[i], c[i]);
   if (t.find(1) == t.end())
     printf('-1
');
   else
     printf('%d
', t[1]);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long read() {
   char x = getchar();
   long long ans = 0;
   while (!isdigit(x)) x = getchar();
   while (isdigit(x)) ans = ans * 10 + x - '0', x = getchar();
   return ans;
 }
 long long in[250005], now, n, vis[250005], dp[250005][2];
 vector<pair<long long, long long> > E[250005];
 void add_edge(long long u, long long v, long long w) {
   E[u].push_back(make_pair(v, w));
 }
 bool cmp(pair<long long, long long> x, pair<long long, long long> y) {
   return in[x.first] > in[y.first];
 }
 struct node {
   long long siz, sum;
   priority_queue<long long> q1, q2;
   void pop(long long x) {
     q2.push(x);
     siz--;
     sum -= x;
   }
   void push(long long x) {
     q1.push(x);
     siz++;
     sum += x;
   }
   void init() {
     while (!q1.empty() && !q2.empty() && q1.top() == q2.top())
       q1.pop(), q2.pop();
   }
   void pop() {
     init();
     siz--;
     sum -= q1.top();
     q1.pop();
   }
   long long top() {
     init();
     return q1.top();
   }
 } P[250005];
 void erase(long long u) {
   for (long long i = 0; i < E[u].size(); i++) {
     long long v = E[u][i].first, w = E[u][i].second;
     if (in[v] <= now) break;
     P[v].push(w);
   }
 }
 void dfs(long long u, long long pre) {
   vis[u] = now;
   long long tot = 0;
   long long num = in[u] - now;
   while (P[u].siz > num) P[u].pop();
   for (long long i = 0; i < E[u].size(); i++) {
     long long v = E[u][i].first, w = E[u][i].second;
     if (in[v] <= now) break;
     if (v == pre) continue;
     dfs(v, u);
   }
   vector<long long> tmp1, tmp2;
   for (long long i = 0; i < E[u].size(); i++) {
     long long v = E[u][i].first, w = E[u][i].second;
     if (v == pre) continue;
     if (in[v] <= now) break;
     long long tmp = dp[v][1] + w - dp[v][0];
     if (tmp <= 0) {
       num--, tot += dp[v][1] + w;
       continue;
     }
     tot += dp[v][0];
     P[u].push(tmp), tmp2.push_back(tmp);
   }
   while (P[u].siz && P[u].siz > num) {
     tmp1.push_back(P[u].top());
     P[u].pop();
   }
   dp[u][0] = tot + P[u].sum;
   while (P[u].siz && P[u].siz > num - 1) {
     tmp1.push_back(P[u].top());
     P[u].pop();
   }
   dp[u][1] = tot + P[u].sum;
   for (long long i = 0; i < tmp1.size(); i++) P[u].push(tmp1[i]);
   for (long long i = 0; i < tmp2.size(); i++) P[u].pop(tmp2[i]);
 }
 signed main() {
   cin >> n;
   long long tot = 0;
   for (long long i = 1; i < n; i++) {
     long long u = read(), v = read(), w = read();
     add_edge(u, v, w);
     add_edge(v, u, w);
     tot += w;
   }
   cout << tot << ' ';
   for (long long i = 1; i <= n; i++) in[i] = E[i].size();
   for (long long i = 1; i <= n; i++) sort(E[i].begin(), E[i].end(), cmp);
   vector<pair<long long, long long> > V;
   for (long long i = 1; i <= n; i++) V.push_back(make_pair(in[i], i));
   sort(V.begin(), V.end());
   long long pos = 0;
   for (now = 1; now < n; now++) {
     while (pos < V.size() && V[pos].first == now) erase(V[pos].second), pos++;
     tot = 0;
     for (long long j = pos; j < V.size(); j++) {
       if (vis[V[j].second] == now) continue;
       dfs(V[j].second, 0);
       tot += dp[V[j].second][0];
     }
     cout << tot << ' ';
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 100005;
 int n, s;
 long long a[maxn], b[maxn], c[maxn];
 vector<int> p[2];
 bool cmp1(int i, int j) { return b[i] - c[i] > b[j] - c[j]; }
 bool cmp2(int i, int j) { return c[i] - b[i] > c[j] - b[j]; }
 int main() {
   scanf('%d%d', &n, &s);
   long long tot = 0, A = 0, B = 0, v1 = 0, v2 = 0;
   p[0].clear();
   p[1].clear();
   for (int i = 0; i < n; i++) {
     scanf('%I64d%I64d%I64d', a + i, b + i, c + i);
     tot += a[i];
     if (b[i] > c[i]) {
       p[0].push_back(i);
       A += a[i] * b[i];
       v1 += a[i];
     } else {
       p[1].push_back(i);
       B += a[i] * c[i];
       v2 += a[i];
     }
   }
   sort(p[0].begin(), p[0].end(), cmp1);
   sort(p[1].begin(), p[1].end(), cmp2);
   long long ans = 0;
   long long c1 = v1 / s + (v1 % s ? 1 : 0);
   long long c2 = v2 / s + (v2 % s ? 1 : 0);
   tot = tot / s + (tot % s ? 1 : 0);
   if (c1 + c2 > tot) {
     long long tmp = A + B;
     long long now = v1 % s;
     for (int i = p[0].size() - 1; i >= 0; i--) {
       now -= a[p[0][i]];
       long long cnt = a[p[0][i]];
       if (now < 0) cnt = a[p[0][i]] + now;
       tmp += (c[p[0][i]] - b[p[0][i]]) * cnt;
       if (now <= 0) break;
     }
     ans = max(ans, tmp);
     tmp = A + B;
     now = v2 % s;
     for (int i = p[1].size() - 1; i >= 0; i--) {
       now -= a[p[1][i]];
       long long cnt = a[p[1][i]];
       if (now < 0) cnt = a[p[1][i]] + now;
       tmp += (b[p[1][i]] - c[p[1][i]]) * cnt;
       if (now <= 0) break;
     }
     ans = max(ans, tmp);
   } else
     ans = A + B;
   printf('%I64d
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const double PI = 2.0 * acos(0.0);
 const int INF = 1000 * 1000 * 1000;
 const int maxn = 1100;
 int W, H, N, M, cnt, tot;
 char g[maxn][maxn];
 int upp[maxn][maxn], lf[maxn][maxn];
 bool can;
 void play(int x, int y) {
   if (x == N || y == M) return;
   bool r, d;
   if (y + 1 < M && upp[x][y + 1] >= H)
     r = true;
   else
     r = false;
   if (x + 1 < N && lf[x + 1][y] >= W)
     d = true;
   else
     d = false;
   if (r && d)
     can = false;
   else if (r) {
     cnt += H;
     play(x, y + 1);
   } else if (d) {
     cnt += W;
     play(x + 1, y);
   }
 }
 int solve(int maxVal, int coefh, int coefw, int sx, int sy) {
   for (int len = 1; len <= maxVal; len++) {
     W += (coefw);
     H += (coefh);
     bool all = true;
     for (int k = 0; k < H && all; k++)
       for (int t = 0; t < W && all; t++) {
         if (g[k + sx][t + sy] != 'X') all = false;
       }
     if (!all) continue;
     can = true;
     cnt = H * W;
     play(sx + H - 1, sy + W - 1);
     if (can && cnt == tot) return H * W;
   }
   return -1;
 }
 int main() {
   ios_base::sync_with_stdio(false);
   int i, j, k;
   cin >> N >> M;
   int sx = -1, sy;
   tot = 0;
   W = H = 0;
   for (int i = 0; i < N; i++)
     for (int j = 0; j < M; j++) {
       cin >> g[i][j];
       if (g[i][j] == 'X') tot++;
       if (g[i][j] == 'X' && sx == -1) {
         sx = i, sy = j;
       }
     }
   memset(upp, 0, sizeof upp);
   memset(lf, 0, sizeof lf);
   for (int i = 0; i < M; i++) upp[0][i] = (g[0][i] == 'X' ? 1 : 0);
   for (int i = 0; i < N; i++) lf[i][0] = (g[i][0] == 'X' ? 1 : 0);
   for (int i = 0; i < N; i++)
     for (int j = 0; j < M; j++) {
       if (i != 0 && g[i][j] == 'X') upp[i][j] = upp[i - 1][j] + 1;
       if (j != 0 && g[i][j] == 'X') lf[i][j] = lf[i][j - 1] + 1;
     }
   int w = 0, h = 0;
   for (int i = sy; i < M && g[sx][i] == 'X'; i++) w++;
   for (int i = sx; i < N && g[i][sy] == 'X'; i++) h++;
   int coefh = 0, coefw = 0;
   int ans;
   W = w, H = 0;
   ans = solve(h, 1, 0, sx, sy);
   W = 0, H = h;
   int ans2 = solve(w, 0, 1, sx, sy);
   if (ans == -1 || (ans2 != -1 && ans2 < ans)) ans = ans2;
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int k;
   int l;
   cin >> k >> l;
   int u[k];
   int y[l];
   for (int i = 0; i < k; i++) {
     cin >> u[i];
   }
   for (int i = 0; i < l; i++) {
     cin >> y[i];
   }
   long long int h = 0;
   long long int v = 0;
   int f = 0;
   int g = 0;
   int p = 0;
   while (p != k || g != l) {
     if (v == h && v == 0) {
       v += y[g];
       h += u[p];
       g++;
       p++;
     } else if (v > h) {
       h += u[p];
       p++;
     } else if (v < h) {
       v += y[g];
       g++;
     } else {
       f++;
       h = 0;
       v = 0;
     }
   }
   cout << f + 1;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long n, m, k, x = 1;
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   cin >> n >> k;
   m = k * (5 + (n - 1) * 6);
   cout << m << endl;
   while (n--) {
     cout << x * k << ' ' << (x + 1) * k << ' ' << (x + 2) * k << ' '
          << (x + 4) * k << endl;
     x += 6;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 3e5 + 5;
 const long double PI = 3.1415926535;
 const long double eps = 1e-07;
 const long long INF = 1e18;
 const int inf = 1e9;
 const long long M = 305;
 const string IO[2]{'NO
', 'YES
'};
 const string io[2]{'No
', 'Yes
'};
 struct STree {
   int start;
   vector<int> t;
   STree(int n) {
     start = 1;
     while (start < n) start *= 2;
     t = vector<int>(2 * start, inf);
   }
   int min(int v, int l, int r, int ql, int qr) {
     if (l > qr || r < ql) return inf;
     if (l >= ql && r <= qr) return t[v];
     int c = (l + r) / 2;
     return std::min(min(2 * v, l, c, ql, qr), min(2 * v + 1, c + 1, r, ql, qr));
   }
   void upd(int v, int l, int r, int p, int x) {
     if (l > p || r < p) return;
     if (l == r) {
       t[v] = x;
       return;
     }
     int c = (l + r) / 2;
     upd(2 * v, l, c, p, x);
     upd(2 * v + 1, c + 1, r, p, x);
     t[v] = std::min(t[2 * v], t[2 * v + 1]);
   }
 };
 void solve() {
   int m;
   cin >> m;
   vector<int> x(m);
   for (int i = 0; i < m; ++i) cin >> x[i];
   sort(x.begin(), x.end());
   auto last = unique(x.begin(), x.end());
   x.erase(last, x.end());
   m = x.size();
   int a, b;
   cin >> b >> a;
   int n = b - a + 1;
   STree st = STree(n);
   int start = st.start;
   vector<int> maxx(n, 1);
   for (auto& nx : x) {
     int fa = ((a - 1) / nx) * nx + nx;
     for (int i = fa; i <= b; i += nx) maxx[i - a] = max(maxx[i - a], nx - 1);
   }
   st.upd(1, 0, start - 1, n - 1, 0);
   for (int i = b - 1; i >= a; --i) {
     int x = i - a;
     int cw = maxx[x];
     int ans = 1 + st.min(1, 0, start - 1, x + 1, x + cw);
     st.upd(1, 0, start - 1, x, ans);
   }
   cout << st.min(1, 0, start - 1, 0, 0) << '
';
 }
 signed main() {
   ios::sync_with_stdio(false);
   cin.tie(nullptr);
   cout.tie(nullptr);
   int t = 1;
   while (t--) solve();
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n, m;
   cin >> n >> m;
   vector<string> a(n);
   for (int i = 0; i < n; ++i) cin >> a[i];
   for (int j = 0; j < m; ++j) {
     vector<int> indx;
     for (int i = 0; i < n; ++i) {
       if (a[i][j] == '#') indx.push_back(i);
     }
     for (int i = 1; i < indx.size(); ++i) {
       if (a[indx[i]] != a[indx[0]]) {
         cout << 'No';
         return 0;
       }
     }
   }
   cout << 'Yes';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 2 * 1e5 + 100;
 vector<int> Prime;
 int main() {
   Prime.clear();
   int n, k;
   scanf('%d%d', &n, &k);
   int flag = 1;
   for (int i = 2; flag && i <= n; i++) {
     if (n % i == 0) {
       while (n % i == 0) {
         Prime.push_back(i);
         n /= i;
       }
       if (n == 1) {
         flag = 0;
       }
       if (n < i && n > 1) {
         flag = 0;
         Prime.push_back(n);
       }
     }
   }
   if (Prime.size() < k) {
     cout << -1 << endl;
   } else {
     for (int i = 0; i < k - 1; i++) {
       cout << Prime[i] << ' ';
     }
     int ans = 1;
     for (int i = k - 1; i < Prime.size(); i++) {
       ans *= Prime[i];
     }
     if (ans != 1) {
       cout << ans << endl;
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 char s[10];
 int n;
 int notzero[12], cnt[12], ten[10], ans;
 int main() {
   ten[0] = 1;
   for (int i = 1; i < 8; ++i) ten[i] = 10 * ten[i - 1];
   scanf('%d', &n);
   for (int i = 1; i <= n; ++i) {
     scanf('%s', s);
     int len = strlen(s);
     notzero[s[0] - 'a'] = 1;
     for (int j = 0; j < len; ++j) cnt[s[j] - 'a'] += ten[len - j - 1];
   }
   int zero = -1;
   for (int i = 0; i < 10; ++i) {
     if (notzero[i] == 0) {
       if (zero == -1 || cnt[i] > cnt[zero]) zero = i;
     }
   }
   vector<int> v;
   for (int i = 0; i < 10; ++i) {
     if (zero != i) v.push_back(cnt[i]);
   }
   sort(v.begin(), v.end());
   for (int i = 0; i < 9; ++i) {
     ans += (9 - i) * v[i];
   }
   printf('%d
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   long long n, k;
   cin >> n >> k;
   long long v[n + 10];
   for (long long i = 0; i < n; i++) {
     cin >> v[i];
   }
   sort(v, v + n);
   k--;
   long long div = k / n;
   long long cnt = 0;
   long long val = v[div];
   for (long long i = 0; i < n; i++) {
     if (v[i] == val) cnt++;
     if (v[i] < val) k = k - n;
   }
   long long amt = cnt;
   long long now = k / amt;
   cout << val << ' ' << v[now] << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct atom {
   long long f, w, where;
   void scan() { scanf('%I64d%I64d', &f, &w); }
 } x[310000];
 priority_queue<long long> A, B;
 int compare(atom k1, atom k2) {
   return k1.f > k2.f || (k1.f == k2.f && k1.where < k2.where);
 }
 int n, pre, ans, where;
 long long rem;
 void insert(long long k1) {
   if (!B.empty()) {
     long long k2 = B.top();
     if (k2 > k1) {
       A.push(-k2);
       B.pop();
       B.push(k1);
       rem += k2 - k1;
       return;
     }
   }
   A.push(-k1);
   pre++;
 }
 int main() {
   scanf('%d', &n);
   for (int i = 1; i <= n; i++) x[i].scan(), x[i].where = i;
   n++;
   x[n] = (atom){0, 0, n};
   sort(x + 1, x + n + 1, compare);
   pre = 0, ans = 0, where = 0;
   long long rem = 0;
   for (int i = 1; i <= n; i++)
     if (x[i].where == 1) where = i;
   ans = where;
   pre = where;
   for (int i = 1; i < where; i++) A.push(-(x[i].w - x[i].f + 1));
   for (int i = where + 1; i <= n; i++) {
     rem += x[i - 1].f - x[i].f;
     if (i - 1 != where) insert(x[i - 1].w - x[i - 1].f + 1);
     while (!A.empty()) {
       long long k1 = -A.top();
       if (k1 <= rem) {
         rem -= k1;
         pre--;
         A.pop();
       } else
         break;
     }
     ans = min(ans, pre);
   }
   printf('%d
', ans);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int n, m, x, y, z;
 string str[5][200];
 int blcks[5], whites[5];
 int ara[] = {0, 1, 2, 3};
 int main() {
   cin >> n;
   for (int i = 0; i < 4; i++) {
     for (int j = 0; j < n; j++) cin >> str[i][j];
     int tmp = 0;
     for (int j = 0; j < n; j++) {
       for (int k = 0; k < n; k++) {
         if ((j + k) % 2 == 0) {
           if (str[i][j][k] == '0')
             blcks[i]++;
           else
             whites[i]++;
         } else {
           if (str[i][j][k] == '1')
             blcks[i]++;
           else
             whites[i]++;
         }
       }
     }
   }
   int ans = 100000000;
   do {
     int tmp1 = whites[ara[0]] + blcks[ara[1]] + whites[ara[2]] + blcks[ara[3]];
     int tmp2 = whites[ara[1]] + blcks[ara[0]] + whites[ara[3]] + blcks[ara[2]];
     ans = min(ans, tmp1);
     ans = min(ans, tmp2);
   } while (next_permutation(ara, ara + 4));
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 struct SegTree {
   int sz;
   vector<int> dat;
   SegTree(int _sz = 1) {
     sz = 1;
     while (sz < _sz) sz <<= 1;
     dat.resize(sz << 1);
   }
   void upd(int pos, int val) {
     pos += sz - 1;
     dat[pos] = val;
     while (pos > 1) {
       pos >>= 1;
       dat[pos] = max(dat[pos << 1], dat[pos << 1 | 1]);
     }
   }
   int qry(int id, int l, int r, int ql, int qr) {
     if (qr < l || r < ql) return 0;
     if (ql <= l && r <= qr) return dat[id];
     return max(qry(id << 1, l, l + r >> 1, ql, qr),
                qry(id << 1 | 1, (l + r >> 1) + 1, r, ql, qr));
   }
   int qry(int l, int r) { return qry(1, 1, sz, l, r); }
 } segx, segy;
 int n, m, V;
 set<pair<int, int>> tph, thp;
 void plant(int p, int h) {
   vector<pair<int, int>> v;
   for (auto it = thp.begin(); it != thp.end() && it->first < h; it++) {
     v.emplace_back(it->second, it->first);
   }
   thp.emplace(h, p);
   tph.emplace(p, h);
   v.emplace_back(p, h);
   reverse(v.begin(), v.end());
   for (auto p : v) {
     segx.upd(p.first, 0);
   }
   for (auto p : v) {
     int lis = segx.qry(p.first + 1, n) + 1;
     segx.upd(p.first, lis);
     segy.upd(p.second, lis);
   }
 }
 void cutdown(int id) {
   vector<pair<int, int>> v;
   for (auto it = tph.begin(); id--; it++) {
     if (id)
       v.emplace_back(it->first, it->second);
     else {
       thp.erase(make_pair(it->second, it->first));
       segx.upd(it->first, 0);
       segy.upd(it->second, 0);
       tph.erase(it);
     }
   }
   reverse(v.begin(), v.end());
   for (auto p : v) {
     segy.upd(p.second, 0);
   }
   for (auto p : v) {
     int lis = segy.qry(p.second + 1, V) + 1;
     segx.upd(p.first, lis);
     segy.upd(p.second, lis);
   }
 }
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   cout.tie(0);
   cin >> n >> m;
   segx = SegTree(n);
   segy = SegTree(V = m + 10);
   while (m--) {
     int typ, x, y;
     cin >> typ >> x;
     if (typ == 1) {
       cin >> y;
       plant(x, y + m);
     } else {
       cutdown(x);
     }
     cout << segx.qry(1, n) << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   int n, k;
   cin >> n >> k;
   string s;
   cin >> s;
   string res = '';
   for (int i = 0; i < n; i++) {
     if ((s[i] - 'a') >= ('z' - s[i])) {
       if ((s[i] - 'a') <= k) {
         k -= (s[i] - 'a');
         res += 'a';
       } else {
         res += (char)(s[i] - k);
         k = 0;
       }
     } else {
       if (('z' - s[i]) <= k) {
         k -= ('z' - s[i]);
         res += 'z';
       } else {
         res += (char)(s[i] + k);
         k = 0;
       }
     }
   }
   if (k != 0) {
     cout << -1;
   } else {
     cout << res;
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int parent[300005], maxLen[300005];
 int findRoot(int u) {
   int root = u;
   while (parent[root] != root) {
     root = parent[root];
   }
   while (parent[u] != root) {
     int y = parent[u];
     parent[u] = root;
     u = y;
   }
   return root;
 }
 void unionNum(int u, int v) {
   int ru = findRoot(u), rv = findRoot(v);
   if (ru == rv) return;
   if (maxLen[ru] > maxLen[rv]) {
     parent[rv] = ru;
     maxLen[ru] = max(maxLen[ru], maxLen[ru] / 2 + maxLen[rv] / 2 +
                                      maxLen[ru] % 2 + maxLen[rv] % 2 + 1);
   } else {
     swap(ru, rv);
     parent[rv] = ru;
     maxLen[ru] = max(maxLen[ru], maxLen[ru] / 2 + maxLen[rv] / 2 +
                                      maxLen[ru] % 2 + maxLen[rv] % 2 + 1);
   }
 }
 int visited[300005] = {0};
 bool nVis(int &i, int n) {
   while (i <= n && visited[i] == 1) i++;
   if (i <= n) return true;
   return false;
 }
 void dfs(int node, vector<int> child[]) {
   map<int, int> v1, v2, d1, d2;
   stack<int> myStack;
   myStack.push(node);
   v1[node] = 1;
   d1[node] = 0;
   visited[node] = 1;
   while (!myStack.empty()) {
     int y = myStack.top();
     myStack.pop();
     for (size_t i = 0; i < child[y].size(); i++) {
       if (v1[child[y][i]] == 0) {
         v1[child[y][i]] = 1;
         d1[child[y][i]] = d1[y] + 1;
         myStack.push(child[y][i]);
         visited[child[y][i]] = 1;
       }
     }
   }
   for (map<int, int>::iterator it = d1.begin(); it != d1.end(); it++) {
     if (d1[node] < it->second) {
       node = it->first;
     }
   }
   myStack.push(node);
   d2[node] = 0;
   v2[node] = 1;
   while (!myStack.empty()) {
     int y = myStack.top();
     myStack.pop();
     parent[y] = node;
     for (size_t i = 0; i < child[y].size(); i++) {
       if (v2[child[y][i]] == 0) {
         v2[child[y][i]] = 1;
         d2[child[y][i]] = d2[y] + 1;
         maxLen[node] = max(maxLen[node], d2[child[y][i]]);
         myStack.push(child[y][i]);
       }
     }
   }
 }
 int main() {
   int n, m, q, a, b;
   scanf('%d%d%d', &n, &m, &q);
   for (int i = 0; i <= n; i++) {
     parent[i] = i;
     maxLen[i] = 0;
   }
   vector<int> child[n + 1];
   for (int i = 0; i < m; i++) {
     scanf('%d%d', &a, &b);
     child[a].push_back(b);
     child[b].push_back(a);
   }
   int i = 1;
   while (nVis(i, n)) {
     dfs(i, child);
   }
   while (q--) {
     int type;
     scanf('%d', &type);
     if (type == 1) {
       scanf('%d', &a);
       printf('%d
', maxLen[findRoot(a)]);
     } else {
       scanf('%d%d', &a, &b);
       unionNum(a, b);
     }
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int sum[1000005];
 int main() {
   int n, x;
   cin >> n;
   for (int i = 0; i < n; ++i)
     for (int j = 0; j < n; ++j) {
       scanf('%d', &x);
       if (i == j) sum[i] = x;
     }
   int all = 0;
   for (int i = 0; i < n; ++i) all += sum[i];
   int q, tipo;
   cin >> q;
   while (q--) {
     scanf('%d', &tipo);
     if (tipo == 3) {
       printf('%d', all % 2);
     } else {
       scanf('%d', &x);
       x--;
       if (sum[x] == 0) {
         sum[x] = 1;
         ++all;
       } else {
         sum[x] = 0;
         --all;
       }
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   long long int t;
   cin >> t;
   while (t--) {
     long long int n, s = 0;
     cin >> n;
     long long int* arr = new long long int[n];
     for (long long int i = 0; i < n; i++) {
       cin >> arr[i];
       s += arr[i];
     }
     if (s == 0)
       cout << 'NO'
            << '
';
     else if (s > 0) {
       cout << 'YES'
            << '
';
       sort(arr, arr + n, greater<long long int>());
       for (long long int i = 0; i < n; i++) cout << arr[i] << ' ';
       cout << '
';
     } else {
       cout << 'YES'
            << '
';
       sort(arr, arr + n);
       for (long long int i = 0; i < n; i++) cout << arr[i] << ' ';
       cout << '
';
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int N = 20, S = (1 << 16) + 10, V = 2048;
 bitset<V> dp[S];
 int a[N], id[N];
 priority_queue<pair<int, int> > q;
 int main() {
   int n, k, v = 0;
   scanf('%d%d', &n, &k);
   for (int i = 1; i <= n; ++i) scanf('%d', &a[i]), v += a[i];
   dp[0][0] = 1;
   int u = (1 << n) - 1;
   for (int s = 0; s <= u; ++s) {
     for (int i = v / k; i >= 1; --i)
       if (dp[s][i * k]) dp[s][i] = 1;
     for (int i = 1; i <= n; ++i)
       if (!(s >> i - 1 & 1)) dp[s | (1 << i - 1)] |= dp[s] << a[i];
   }
   puts(dp[u][1] ? 'YES' : 'NO');
   if (!dp[u][1]) return 0;
   int c = 1e9;
   for (int s = u, j = 1; s;)
     for (int t = j, ok = 0; !ok; t *= k, c -= !ok)
       for (int i = 1; i <= n; ++i) {
         if (t < a[i]) continue;
         if ((s >> i - 1 & 1) && dp[s ^ (1 << i - 1)][t - a[i]]) {
           id[i] = c, s ^= 1 << i - 1, j = t - a[i], ok = 1;
           break;
         }
       }
   for (int i = 1; i <= n; ++i) q.push(make_pair(-(id[i] - c), a[i]));
   while (q.size() > 1) {
     pair<int, int> x = q.top();
     q.pop();
     pair<int, int> y = q.top();
     q.pop();
     printf('%d %d
', x.second, y.second);
     int v = x.second + y.second, u = 0;
     while (v % k == 0) v /= k, ++u;
     q.push(make_pair(-(-x.first + u), v));
   }
   return 0;
 }


#include <bits/stdc++.h>
 #pragma comment(linker, '/stack:200000000')
 #pragma GCC optimize('Ofast')
 #pragma GCC target('sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native')
 using namespace std;
 long long n, k, s, h[200005];
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(0);
   cin >> n >> k >> s;
   h[0] = 1;
   if (k * (n - 1) < s || k > s) {
     cout << 'NO';
     return 0;
   }
   long long t = s / k, z = s % k;
   for (long long i = 1; i <= z; i++) {
     if (h[i - 1] + t + 1 <= n) {
       h[i] = h[i - 1] + t + 1;
     } else
       h[i] = h[i - 1] - t - 1;
   }
   for (long long i = z + 1; i <= k; i++) {
     if (h[i - 1] + t <= n) {
       h[i] = h[i - 1] + t;
     } else
       h[i] = h[i - 1] - t;
   }
   cout << 'YES
';
   for (long long i = 1; i <= k; i++) cout << h[i] << ' ';
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int inf = 0x3f3f3f3f;
 inline int Read() {
   int x = 0, f = 1;
   char c = getchar();
   while (c > '9' || c < '0') {
     if (c == '-') f = -1;
     c = getchar();
   }
   while (c >= '0' && c <= '9') {
     x = x * 10 + c - '0';
     c = getchar();
   }
   return x * f;
 }
 struct EDGE {
   int from, to, w, next;
 } E[400005];
 int n, m;
 int HEAD[200005];
 int dep[200005], CENT;
 int pa[200005][50], maxx[200005][50];
 void ADD(int from, int to, int w) {
   E[CENT].from = from;
   E[CENT].to = to;
   E[CENT].w = w;
   E[CENT].next = HEAD[from];
   HEAD[from] = CENT++;
 }
 void DFS(int u, int f) {
   pa[u][0] = f;
   for (int i = HEAD[u]; i != -1; i = E[i].next) {
     int to = E[i].to;
     if (to != f) {
       dep[to] = dep[u] + 1;
       maxx[to][0] = E[i].w;
       DFS(to, u);
     }
   }
 }
 int maxh;
 void deal() {
   int j;
   for (j = 1; (1 << j) < n; j++)
     for (int i = 1; i <= n; i++) {
       int t = pa[i][j - 1];
       pa[i][j] = pa[pa[i][j - 1]][j - 1];
       maxx[i][j] = max(maxx[i][j - 1], maxx[t][j - 1]);
     }
   maxh = j - 1;
 }
 int swim(int x, int k, int &ma) {
   ma = 0;
   for (int i = 0; i <= maxh; i++) {
     if (k >> i & 1) {
       ma = max(ma, maxx[x][i]);
       x = pa[x][i];
     }
   }
   return x;
 }
 int LCA(int x, int y) {
   if (dep[x] > dep[y]) swap(x, y);
   int ans = 0;
   y = swim(y, dep[y] - dep[x], ans);
   if (x == y) return ans;
   for (int i = maxh; i >= 0; i--) {
     if (pa[x][i] != pa[y][i]) {
       ans = max(ans, max(maxx[x][i], maxx[y][i]));
       x = pa[x][i], y = pa[y][i];
     }
   }
   return max(ans, max(maxx[x][0], maxx[y][0]));
 }
 const int N = 200005;
 struct edge {
   int from, to, w, next;
   int ty;
   bool operator<(const edge a) const { return w < a.w; }
 };
 int pre[N], head[N];
 int cnt;
 edge e[200005 + 5];
 void add(int from, int to, int w) {
   e[cnt].from = from;
   e[cnt].to = to;
   e[cnt].w = w;
   e[cnt].next = head[from];
   e[cnt].ty = 0;
   head[from] = cnt++;
 }
 int find(int x) {
   if (x == pre[x]) return x;
   return pre[x] = find(pre[x]);
 }
 void Kruskal() {
   for (int i = 1; i <= n; ++i) pre[i] = i;
   sort(e, e + cnt);
   for (int i = 0; i < cnt; ++i) {
     int u = find(e[i].from);
     int v = find(e[i].to);
     if (u != v) {
       pre[u] = v;
       ADD(e[i].from, e[i].to, e[i].w);
       ADD(e[i].to, e[i].from, e[i].w);
       e[i].ty = 1;
     }
   }
 }
 int main() {
   while (scanf('%d %d', &n, &m) != EOF) {
     CENT = cnt = 0;
     for (int i = 1; i <= n; i++) head[i] = HEAD[i] = -1;
     for (int i = 1; i <= m; i++) {
       int u = Read(), v = Read(), w = Read();
       add(u, v, w);
     }
     Kruskal();
     DFS(1, -1);
     deal();
     int ans = 0;
     for (int i = 0; i < cnt; i++) {
       if (!e[i].ty) {
         int u = e[i].from, v = e[i].to;
         int minn = LCA(u, v);
         if (minn == e[i].w) ans++;
       }
     }
     printf('%d
', ans);
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 pair<long long, long long> a[200005];
 signed main() {
   long long n, j;
   cin >> n >> j;
   j %= 2 * n;
   long long x0, y0;
   cin >> x0 >> y0;
   for (long long i = 0; i < n; i++) {
     cin >> a[i].first >> a[i].second;
   }
   for (long long i = 1; i <= j; i++) {
     x0 = 2 * a[(i - 1) % n].first - x0;
     y0 = 2 * a[(i - 1) % n].second - y0;
   }
   cout << x0 << ' ' << y0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   ios::sync_with_stdio(false);
   cin.tie(nullptr);
   int T;
   cin >> T;
   while (T--) {
     long long x, y;
     cin >> x >> y;
     if (y % x == 0LL) {
       cout << x << '
';
     } else if (x > y) {
       cout << x + y << '
';
     } else {
       cout << (y / x) * x + (y - (y / x) * x) / 2 << '
';
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn=1e5+10;

 int a[maxn];
 int f[maxn];
 int n,m,gcnt,ans;

 int main()
 {
 	scanf('%d',&n);
 	for (int i=1;i<=n;i++) scanf('%d',&a[i]),f[i]=n+1;
 	gcnt=1;
 	for (int i=2,prevmin=0;i<=n;i++)
 	{
 		if (a[i-1]!=a[i]) gcnt++;
 		f[a[i-1]]=min(f[a[i]]-1+(a[i-1]==a[i]),prevmin);
 		prevmin=min(prevmin,f[a[i-1]]);
 	}
 	ans=n;
 	for (int i=0;i<=n;i++) ans=min(ans,f[i]+gcnt);
 	printf('%d
',ans);
 }


#include <bits/stdc++.h>
 using namespace std;
 bool isPrime(int n) {
   if (n <= 1) return false;
   if (n <= 3) return true;
   if (n % 2 == 0 || n % 3 == 0) return false;
   for (int i = 5; i * i <= n; i = i + 6)
     if (n % i == 0 || n % (i + 2) == 0) return false;
   return true;
 }
 int nextPrime(int N) {
   if (N <= 1) return 2;
   int prime = N;
   bool found = false;
   while (!found) {
     prime++;
     if (isPrime(prime)) found = true;
   }
   return prime;
 }
 long long int powerexp(long long int x, long long int n) {
   long long int result = 1;
   while (n) {
     if (n & 1) result = result * x % 1000000007;
     n = n / 2;
     x = x * x % 1000000007;
   }
   return result;
 }
 int gcd(int a, int b) {
   if (a == 0) return b;
   if (b == 0) return a;
   if (a == b) return a;
   if (a > b) return gcd(a - b, b);
   return gcd(a, b - a);
 }
 int largest_divisor(long long int n) {
   long long int i;
   for (i = n / 2; i >= 1; i--) {
     if (n % i == 0) {
       return i;
     }
   }
   return 0;
 }
 bool checkPalindrome(string str) {
   int n = str.length();
   int count = 0;
   for (int i = 0; i < n / 2; ++i)
     if (str[i] != str[n - i - 1]) ++count;
   return (count <= 1);
 }
 bool countDivisors(long long int n) {
   int cnt = 0;
   for (int i = 3; i <= sqrt(n); i = i + 2) {
     if (n % i == 0) {
       return true;
     }
   }
   return false;
 }
 long long int factorial(long long int n) {
   if (n == 0) return 0;
   return n * factorial(n - 1);
 }
 long long int lcm(long long int a, long long int b) {
   return (a / gcd(a, b)) * b;
 }
 void fastIO() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
 }
 int main() {
   long long int t = 1;
   while (t--) {
     long long int n;
     cin >> n;
     long long int a[n];
     long long int z = 0, p = 0, neg = 0;
     long long int res = 0;
     for (long long int i = 0; i < n; i++) {
       cin >> a[i];
       if (a[i] == 0) {
         res++;
         z++;
       } else if (a[i] > 0) {
         res = res + (a[i] - 1);
         p++;
       } else {
         res = res + (-1 - a[i]);
         neg++;
       }
     }
     if (neg % 2 == 1 && z == 0) {
       res += 2;
     }
     cout << res << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int INF = (int)2e9;
 int main() {
   int k, n, m, q;
   cin >> k >> n >> m >> q;
   map<string, map<string, int>> rec;
   vector<string> art(n);
   map<int, map<string, int>> b;
   for (int(i) = (int)(0); (i) < (int)n; ++(i)) cin >> art[i];
   sort(art.begin(), art.end());
   string s;
   getline(cin, s);
   for (int(i) = (int)(0); (i) < (int)m; ++(i)) {
     getline(cin, s);
     int p = s.find(':');
     string ar = s.substr(0, p);
     s = s.substr(p + 2, s.size());
     s += ', ';
     while (s != '') {
       p = s.find(' ');
       string t = s.substr(0, p);
       s = s.substr(p + 1, s.size());
       p = s.find(',');
       string t2 = s.substr(0, p);
       s = s.substr(p + 2, s.size());
       int col = atoi(t2.c_str());
       rec[ar][t] = col;
     }
   }
   for (int(i) = (int)(0); (i) < (int)q; ++(i)) {
     int t;
     cin >> t;
     string st;
     cin >> st;
     b[t][st]++;
     for (map<string, map<string, int>>::iterator it1 = rec.begin();
          it1 != rec.end(); ++it1) {
       bool ok = true;
       for (map<string, int>::iterator it2 = it1->second.begin();
            it2 != it1->second.end(); ++it2) {
         if (b[t].find(it2->first) == b[t].end() ||
             b[t][it2->first] < it2->second) {
           ok = false;
           break;
         }
       }
       if (ok) {
         for (map<string, int>::iterator it2 = it1->second.begin();
              it2 != it1->second.end(); ++it2) {
           b[t][it2->first] -= it2->second;
           if (b[t][it2->first] == 0) b[t].erase(it2->first);
         }
         b[t][it1->first]++;
         break;
       }
     }
   }
   for (int(i) = (int)(1); (i) < (int)k + 1; ++(i)) {
     cout << b[i].size() << '
';
     for (map<string, int>::iterator it = b[i].begin(); it != b[i].end(); ++it)
       if (it->second) cout << it->first << ' ' << it->second << '
';
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int mx = 3005;
 const int N = 100005;
 int n, a[mx][mx], b[mx][mx], ans[N];
 pair<int, int> t[N];
 inline int check(int lx, int rx, int ly, int ry) {
   int cnt = b[rx][ry];
   if (lx) cnt -= b[lx - 1][ry];
   if (ly) cnt -= b[rx][ly - 1];
   if (lx && ly) cnt += b[lx - 1][ly - 1];
   int need = (rx - lx + 1) * (ry - ly + 1);
   if (cnt != need) return -1;
   if (ly)
     if (a[rx][ly] == a[rx][ly - 1]) return -1;
   if (lx)
     if (a[lx][ry] == a[lx - 1][ry]) return -1;
   for (int i = lx; i <= rx; i++)
     if (a[i][ry] == a[i][ry + 1]) return false;
   for (int i = ly; i <= ry; i++)
     if (a[rx][i] == a[rx + 1][i]) return false;
   return true;
 }
 int main() {
   int n;
   scanf('%d', &n);
   for (int q = (0); q <= ((n)-1); q++) {
     int x1, y1, x2, y2;
     scanf('%d%d%d%d', &x1, &y1, &x2, &y2);
     for (int i = x1; i < x2; i++)
       for (int j = y1; j < y2; j++) a[i][j] = q + 1, b[i][j] = 1;
     t[q] = make_pair(x1, y1);
   }
   for (int i = (0); i <= ((mx)-1); i++)
     for (int j = (0); j <= ((mx)-1); j++) {
       if (i - 1 >= 0) b[i][j] += b[i - 1][j];
       if (j - 1 >= 0) b[i][j] += b[i][j - 1];
       if (i - 1 >= 0 && j - 1 >= 0) b[i][j] -= b[i - 1][j - 1];
     }
   random_shuffle(t, t + n);
   for (int q = (0); q <= ((n)-1); q++) {
     int si = t[q].first, sj = t[q].second;
     int ei = si, ej = sj;
     while (ei < mx && ej < mx) {
       int ch = check(si, ei, sj, ej);
       if (ch == -1) break;
       if (ch == 1) {
         for (int i = si; i <= ei; i++)
           for (int j = sj; j <= ej; j++) ans[a[i][j]] = 1;
         vector<int> res;
         for (int i = 0; i < N; i++)
           if (ans[i]) res.push_back(i);
         printf('YES %d
', (int)((res).end() - (res).begin()));
         for (int i = (0); i <= (((int)((res).end() - (res).begin())) - 1);
              i++) {
           if (i) printf(' ');
           printf('%d', res[i]);
         }
         puts('');
         exit(0);
       }
       ei++;
       ej++;
     }
   }
   puts('NO');
   return 0;
 }


#include<bits/stdc++.h>
 using namespace std;

 #ifndef ONLINE_JUDGE
   #include 'Mohit.h'
 #endif

 typedef long long ll;
 typedef long double ld;
 #define endl '
'
 #define rep(i,n)  for(ll i = 0; i < (n); ++i)
 #define repA(i, a, n)  for(ll i = a; i <= (n); ++i)
 #define repD(i, a, n)  for(ll i = a; i >= (n); --i)
 #define trav(a, x) for(auto& a : x)
 #define all(x) x.begin(), x.end()
 #define ff first
 #define ss second
 #define pb push_back
 typedef vector<ll> vll;
 typedef vector<pair<ll,ll>> vpl;
 const ld PI = 4*atan((ld)1);
 const ll INF = 1e18;
 const ll mod = 1e9+7;

 int main()
 {
     ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
     ll tt=1;
         cin >> tt;
     repA(qq,1,tt){
         ll n; cin >> n;
         vll a(n);
         rep(i,n){
             cin >> a[i];
         }

         ll ans = INF;
         ll s1=a[0],s2=0,c1=1,c2=0;
         ll m1=a[0],m2=INF;

         repA(i,1,n-1){
             if(i&1){
                 m2 = min(m2,a[i]); s2 += a[i];
                 ll t1 = (s2-m2)+(n-(c2))*m2;
                 ll t2 = (s1-m1)+(n-(c1-1))*m1;
                 ans = min(ans,t1+t2);
                 c2++;
             }else{
                 m1 = min(m1,a[i]); s1 += a[i];
                 ll t1 = (s1-m1)+(n-(c1))*m1;
                 ll t2 = (s2-m2)+(n-(c2-1))*m2;
                 ans = min(ans,t1+t2);
                 c1++;
             }
         }

         cout << ans << endl;
     }
 }


#include <bits/stdc++.h>
 using namespace std;
 void solve() {
   long long n, z = 0;
   cin >> n;
   long long a[n], b[n];
   for (long long i = 0; i < n; i++) {
     cin >> a[i];
   }
   for (long long i = 0; i < n; i++) {
     cin >> b[i];
   }
   vector<long long> neg, pos;
   for (long long i = 0; i < n; i++) {
     if (a[i] == b[i]) z++;
     if ((a[i] - b[i]) >= 0)
       pos.push_back(a[i] - b[i]);
     else
       neg.push_back(b[i] - a[i]);
   }
   if (!pos.empty()) sort(pos.begin(), pos.end());
   if (!neg.empty()) sort(neg.begin(), neg.end());
   long long ans = 0, cnt = 0, j = (long long)neg.size() - 1,
             k = (long long)pos.size() - 1;
   ans += (k * (k + 1)) / 2;
   ans -= (z * (z - 1)) / 2;
   while (j >= 0 && !pos.empty()) {
     if (k >= 0 && neg[j] < pos[k]) {
       cnt++;
       k--;
     } else {
       ans += cnt;
       j--;
     }
   }
   cout << ans << endl;
   return;
 }
 int main() {
   long long t;
   t = 1;
   while (t--) {
     solve();
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int MAXN = 1e5 + 10;
 int a[MAXN];
 bool mark[MAXN];
 void add(int v, int u) {
   while (mark[u] == false) {
     cout << v << ' ' << u << endl;
     mark[u] = true;
     v = a[v];
     u = a[u];
   }
 }
 int dfs(int x) {
   int ret = 1;
   mark[x] = true;
   if (mark[a[x]] == false) ret += dfs(a[x]);
   return ret;
 }
 int main() {
   ios::sync_with_stdio(false);
   int n;
   cin >> n;
   for (int i = 1; i <= n; i++) cin >> a[i];
   for (int i = 1; i <= n; i++)
     if (a[i] == i) {
       cout << 'YES' << endl;
       for (int j = 1; j <= n; j++)
         if (j != i) cout << i << ' ' << j << endl;
       return 0;
     }
   int p = -1;
   for (int i = 1; i <= n; i++)
     if (mark[i] == false) {
       int x = dfs(i);
       if (x % 2) {
         cout << 'NO' << endl;
         return 0;
       } else if (x == 2)
         p = i;
     }
   if (p == -1) {
     cout << 'NO' << endl;
     return 0;
   }
   fill(mark, mark + MAXN, false);
   cout << 'YES' << endl;
   cout << p << ' ' << a[p] << endl;
   mark[p] = mark[a[p]] = true;
   for (int i = 1; i <= n; i++)
     if (!mark[i]) add(p, i);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 void task();
 int main() {
   srand(time(0));
   task();
   return 0;
 }
 const int INF = 0x3f3f3f3f;
 const int N = 1e6 + 10;
 const int M = 1e7 + 100;
 const int OLOLO = 0xFFFFFFFF;
 int n, k;
 long long int cnt[N];
 set<int> vals;
 long long int ans;
 vector<int> masks;
 void task() {
   cin >> n >> k;
   for (int i = 0; i < n; ++i) {
     int a;
     cin >> a;
     ++cnt[a];
     vals.insert(a);
   }
   for (int i = 0; i < (1 << 14); ++i) {
     if (__builtin_popcount(i) == k) {
       masks.push_back(i);
     }
   }
   for (int val : vals) {
     for (int mask : masks) {
       int result = val & (mask ^ OLOLO);
       result |= (val ^ OLOLO) & mask;
       if (result < N) {
         if (val == result) {
           ans += cnt[val] * (cnt[val] - 1);
         } else {
           ans += cnt[val] * cnt[result];
         }
       }
     }
   }
   cout << ans / 2;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int mx = 0, a = 0, n;
   string s;
   cin >> n;
   while (cin >> s) {
     for (char c : s)
       if (isupper(c)) a++;
     mx = max(mx, a), a = 0;
   }
   cout << mx << endl;
 }


#include <bits/stdc++.h>
 using namespace std;
 bool debug = 0;
 int n, m, k;
 int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
 long long ln, lk, lm;
 int x[105], y[105], r[105];
 string vs[2];
 void add(int id, int i, int x) {
   string str = '(((1-abs((t-';
   str += to_string(i);
   str += ')))+abs((abs((t-';
   str += to_string(i);
   str += '))-1)))*';
   str += to_string(x);
   str += ')';
   if (vs[id] == '')
     vs[id] = str;
   else {
     vs[id] = '(' + vs[id] + '+' + str + ')';
   }
 }
 void fmain() {
   scanf('%d', &n);
   for (int(i) = 0; (i) < (int)(2); (i)++) vs[i] = '';
   for (int(i) = 0; (i) < (int)(n); (i)++) {
     scanf('%d%d%d', x + i, y + i, r + i);
     add(0, i + 1, x[i] / 2);
     add(1, i + 1, y[i] / 2);
   }
   for (int(i) = 0; (i) < (int)(2); (i)++) cout << vs[i] << endl;
 }
 int main() {
   fmain();
   return 0;
 }