#include <bits/stdc++.h>
 using namespace std;
 const int mn = 201000, inf = 1 << 30;
 struct node {
   int x, y;
 } pt[mn], sub[mn];
 struct point {
   int k, x, y, r;
 } e[mn * 2];
 int s1[mn * 2], s2[mn * 2];
 int xs[mn * 2], dis[mn], sa[mn], L;
 int n, m, i, j, step, lo, hi, mid;
 bool cmp(node a, node b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
 bool cmp1(point a, point b) { return a.x < b.x || (a.x == b.x && a.k < b.k); }
 bool subway(int i, int j) { return dis[i] > dis[j]; }
 struct tree {
   int tt, rt[mn], s[mn * 20], le[mn * 20], ri[mn * 20];
   int get(const int &p, int l, int r, int x, int y) {
     if (x <= l && y >= r) return s[p];
     int m = (l + r) / 2, res = 0;
     if (x <= m) res += get(le[p], l, m, x, y);
     if (y > m) res += get(ri[p], m + 1, r, x, y);
     return res;
   }
   void add(const int &t, int &p, int l, int r, int k) {
     s[p = ++tt] = s[t] + 1;
     if (l == r) return;
     int m = (l + r) / 2;
     if (k <= m)
       add(le[t], le[p], l, m, k), ri[p] = ri[t];
     else
       add(ri[t], ri[p], m + 1, r, k), le[p] = le[t];
   }
   void ins(int i, int v) { add(rt[i - 1], rt[i], 1, L, v); }
   int query(int x, int y, int l, int r) {
     if (x > y || l > r || x > m) return 0;
     return get(rt[y], 1, L, l, r) - get(rt[x - 1], 1, L, l, r);
   }
 } T;
 point eva(int k, int x, int y, int r) {
   point res;
   res.k = k, res.x = x, res.y = y, res.r = r;
   return res;
 }
 void cross(int &l, int &r, int &x, int &y, int i, int t) {
   l = max(l, pt[i].x - t), x = max(x, pt[i].y - t);
   r = min(r, pt[i].x + t), y = min(y, pt[i].y + t);
 }
 void add(int *s, int p, int v) {
   for (; p <= L; p += p & (-p)) s[p] = max(s[p], v);
 }
 int query(int *s, int p) {
   int res = -inf;
   for (; p; p -= p & (-p)) res = max(res, s[p]);
   return res;
 }
 void get(point p) {
   int y = lower_bound(xs + 1, xs + 1 + L, p.y) - xs;
   if (p.k == 0)
     add(s1, y, p.x + p.y), add(s2, L - y + 1, p.x - p.y);
   else {
     int t1 = p.x + p.y - query(s1, y), t2 = p.x - p.y - query(s2, L - y + 1);
     dis[p.r] = min(dis[p.r], min(t1, t2));
   }
 }
 void ugly(node &p) {
   int X = p.x - p.y, Y = p.x + p.y;
   p.x = X, p.y = Y;
 }
 void nearest() {
   int i, tt = 0;
   for (i = 1; i <= n; ++i) xs[++L] = pt[i].y;
   for (i = 1; i <= m; ++i) xs[++L] = sub[i].y;
   sort(xs + 1, xs + 1 + L);
   L = unique(xs + 1, xs + 1 + L) - xs - 1;
   for (i = 1; i <= n; ++i) e[++tt] = eva(1, pt[i].x, pt[i].y, i);
   for (i = 1; i <= m; ++i) e[++tt] = eva(0, sub[i].x, sub[i].y, i);
   sort(e + 1, e + 1 + tt, cmp1);
   fill(dis + 1, dis + 1 + n, inf);
   fill(s1 + 1, s1 + L + 1, -inf), fill(s2 + 1, s2 + 1 + L, -inf);
   for (i = 1; i <= tt; ++i) get(e[i]);
   for (i = 1; i <= tt; ++i) e[i].x = -e[i].x;
   sort(e + 1, e + 1 + tt, cmp1);
   fill(s1 + 1, s1 + L + 1, -inf), fill(s2 + 1, s2 + 1 + L, -inf);
   for (i = 1; i <= tt; ++i) get(e[i]);
 }
 int main() {
   scanf( %d%d , &n, &m);
   for (i = 1; i <= n; ++i) scanf( %d%d , &pt[i].x, &pt[i].y);
   for (i = 1; i <= m; ++i) scanf( %d%d , &sub[i].x, &sub[i].y);
   nearest();
   for (i = 1; i <= n; ++i) ugly(pt[i]);
   for (i = 1; i <= m; ++i) ugly(sub[i]);
   sort(sub + 1, sub + 1 + m, cmp);
   L = 0;
   for (i = 1; i <= n; ++i) xs[++L] = pt[i].y;
   for (i = 1; i <= m; ++i) xs[++L] = sub[i].y;
   sort(xs + 1, xs + 1 + L), L = unique(xs + 1, xs + 1 + L) - xs - 1;
   for (i = 1; i <= m; ++i)
     T.ins(i, lower_bound(xs + 1, xs + 1 + L, sub[i].y) - xs);
   for (i = 1; i <= n; ++i) sa[i] = i;
   sort(sa + 1, sa + 1 + n, subway);
   lo = 0, hi = dis[sa[1]];
   int mix, mxx, miy, mxy, ct, l, r, x, y;
   node su;
   while (lo < hi) {
     int mid = (lo + hi) / 2;
     mix = miy = -inf, mxx = mxy = inf;
     for (i = 1; i <= n; ++i) {
       if (dis[sa[i]] > mid)
         cross(mix, mxx, miy, mxy, sa[i], mid);
       else {
         ct = mid - dis[sa[i]];
         su.y = -inf, su.x = mix - ct;
         l = lower_bound(sub + 1, sub + 1 + m, su, cmp) - sub;
         su.y = inf, su.x = mxx + ct;
         r = lower_bound(sub + 1, sub + 1 + m, su, cmp) - sub - 1;
         x = lower_bound(xs + 1, xs + 1 + L, miy - ct) - xs,
         y = lower_bound(xs + 1, xs + 1 + L, mxy + ct + 1) - xs - 1;
         if (T.query(l, r, x, y))
           break;
         else
           cross(mix, mxx, miy, mxy, sa[i], mid);
       }
       if (mix > mxx || miy > mxy) break;
     }
     if (mix <= mxx && miy <= mxy)
       hi = mid;
     else
       lo = mid + 1;
   }
   printf( %d
 , lo);
   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;
 }
 inline void gn(long long &first) {
   int sg = 1;
   char c;
   while (((c = getchar()) < '0' || c > '9') && c != '-')
     ;
   c == '-' ? (sg = -1, first = 0) : (first = c - '0');
   while ((c = getchar()) >= '0' && c <= '9') first = first * 10 + c - '0';
   first *= sg;
 }
 inline void gn(int &first) {
   long long t;
   gn(t);
   first = t;
 }
 inline void gn(unsigned long long &first) {
   long long t;
   gn(t);
   first = t;
 }
 inline void gn(double &first) {
   double t;
   scanf( %lf , &t);
   first = t;
 }
 inline void gn(long double &first) {
   double t;
   scanf( %lf , &t);
   first = t;
 }
 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;
 }
 int N, M, E, W;
 int dis[101000];
 queue<int> Q;
 int pre[101000], edg[101000][3], cnt[101000], val[101000];
 map<pair<int, int>, int> state, flag;
 vector<int> adj[101000];
 int len = 0x3f3f3f3f, tot;
 void Dijk(int s) {
   memset(dis, 0x3f, sizeof(dis));
   Q.push(s);
   dis[s] = 0;
   cnt[s] = 0;
   val[s] = W;
   while (!Q.empty()) {
     int u = Q.front();
     Q.pop();
     for (int i = 0; i < adj[u].size(); i++) {
       int v = adj[u][i], k;
       if (!state[pair<int, int>(u, v)])
         k = val[u] + 1;
       else
         k = val[u] - 1;
       if (dis[v] > dis[u] + 1) {
         dis[v] = dis[u] + 1;
         pre[v] = u;
         val[v] = k;
         Q.push(v);
       } else if (dis[v] == dis[u] + 1 && val[v] > k) {
         val[v] = k;
         pre[v] = u;
         Q.push(v);
       }
     }
   }
 }
 int main() {
   int u, v, w;
   scanf( %d%d , &N, &M);
   for (int i = 0; i < M; i++) {
     scanf( %d%d%d , &u, &v, &w);
     adj[u].push_back(v);
     adj[v].push_back(u);
     state[pair<int, int>(u, v)] = w;
     state[pair<int, int>(v, u)] = w;
     edg[i][0] = u;
     edg[i][1] = v;
     edg[i][2] = w;
     W += w;
   }
   Dijk(1);
   u = N;
   while (u != 1) {
     flag[pair<int, int>(u, pre[u])] = 1;
     flag[pair<int, int>(pre[u], u)] = 1;
     u = pre[u];
   }
   tot = 0;
   for (int i = 0; i < M; i++) {
     u = edg[i][0];
     v = edg[i][1];
     w = edg[i][2];
     if (!flag[pair<int, int>(u, v)] && w == 1)
       tot++;
     else if (flag[pair<int, int>(u, v)] && w == 0)
       tot++;
   }
   printf( %d
 , tot);
   for (int i = 0; i < M; i++) {
     u = edg[i][0];
     v = edg[i][1];
     w = edg[i][2];
     if (!flag[pair<int, int>(u, v)] && w == 1) {
       printf( %d %d 0
 , u, v);
     } else if (flag[pair<int, int>(u, v)] && w == 0) {
       printf( %d %d 1
 , u, v);
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 const int maxn = 35005;
 struct Node {
   int l, r;
   int lazy;
   int Max;
 } node[maxn << 2];
 int n, k;
 int a[maxn];
 int pre[maxn];
 int dp[maxn];
 void push_down(int root) {
   node[root << 1].lazy += node[root].lazy;
   node[root << 1 | 1].lazy += node[root].lazy;
   node[root << 1].Max += node[root].lazy;
   node[root << 1 | 1].Max += node[root].lazy;
   node[root].lazy = false;
 }
 void push_up(int root) {
   node[root].Max = max(node[root << 1].Max, node[root << 1 | 1].Max);
 }
 void build(int l, int r, int root) {
   node[root].l = l, node[root].r = r;
   node[root].lazy = 0;
   if (l == r) {
     node[root].Max = dp[l];
     return;
   }
   int mid = (l + r) >> 1;
   build(l, mid, root << 1);
   build(mid + 1, r, root << 1 | 1);
   push_up(root);
 }
 void update(int l, int r, int root) {
   if (node[root].l >= l && node[root].r <= r) {
     node[root].lazy += 1;
     node[root].Max += 1;
     return;
   }
   if (node[root].lazy) push_down(root);
   int mid = (node[root].l + node[root].r) >> 1;
   if (l <= mid) update(l, r, root << 1);
   if (r > mid) update(l, r, root << 1 | 1);
   push_up(root);
 }
 int query(int l, int r, int root) {
   if (node[root].l >= l && node[root].r <= r) {
     return node[root].Max;
   }
   if (node[root].lazy) push_down(root);
   int t1 = 0, t2 = 0;
   int mid = (node[root].l + node[root].r) >> 1;
   if (l <= mid) t1 = query(l, r, root << 1);
   if (r > mid) t2 = query(l, r, root << 1 | 1);
   push_up(root);
   if (t1 > t2) return t1;
   return t2;
 }
 int main() {
   scanf( %d%d , &n, &k);
   for (int i = 1; i <= n; i++) scanf( %d , &a[i]);
   memset(dp, 0, sizeof(dp));
   for (int i = 1; i <= k; i++) {
     build(0, n, 1);
     for (int j = 0; j < maxn; j++) pre[j] = 0;
     for (int j = 1; j <= n; j++) {
       update(pre[a[j]], j - 1, 1);
       pre[a[j]] = j;
       dp[j] = query(0, j - 1, 1);
     }
   }
   printf( %d
 , dp[n]);
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 void small(long long m, long long s) {
   vector<long long> v;
   long long p = (s % 9 == 0) ? s / 9 - 1 : s / 9;
   for (long long i = 0; i < p; i++) {
     v.push_back(9);
     s -= 9;
   }
   if (m - p == 1) {
     v.push_back(s);
   }
   if (m - p >= 2) {
     s--;
     v.push_back(s);
     for (long long i = 0; i < m - p - 2; i++) {
       v.push_back(0);
     }
     v.push_back(1);
   }
   for (long long i = v.size() - 1; i >= 0; i--) {
     cout << v[i];
   }
   cout <<    ;
 }
 void great(long long m, long long s) {
   vector<long long> v;
   long long k = s / 9;
   for (long long i = 0; i < k; i++) {
     v.push_back(9);
     s -= 9;
   }
   if (s > 0) {
     v.push_back(s);
     k++;
   }
   for (long long i = 0; i < m - k; i++) {
     v.push_back(0);
   }
   for (long long i = 0; i < v.size(); i++) {
     cout << v[i];
   }
 }
 int main() {
   long long m, s, h;
   string k;
   vector<int> v;
   cin >> m >> s;
   if (s % 9 == 0) {
     long long t = 0;
     if (m == 1 && s == 0) {
       cout <<  0
            <<  0 ;
       t = 1;
     }
     if (t == 0) {
       if (m < s / 9 || s == 0) {
         cout <<  -1
              <<  -1 ;
       } else {
         small(m, s);
         great(m, s);
       }
     }
   } else if (s % 9 != 0) {
     long long t = 0;
     if (m == 1 && s == 0) {
       cout <<  0
            <<  0 ;
       t = 1;
     }
     if (t == 0) {
       if (m < s / 9 + 1 || s == 0) {
         cout <<  -1
              <<  -1 ;
       } else {
         small(m, s);
         great(m, s);
       }
     }
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 long long int mod = 1e9 + 7;
 long long int inf = 1e9 + 7;
 void solve() {
   long long int n;
   cin >> n;
   if (n <= 2) {
     cout <<  -1  << endl;
   } else {
     long long int i;
     for (i = n; i >= 1; i--) {
       cout << i <<    ;
     }
     cout << endl;
   }
 }
 int main() {
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   long long int t = 1;
   while (t--) {
     solve();
   }
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int n;
   cin >> n;
   string s, t;
   cin >> s >> t;
   int cost = 0;
   for (int i = 0; i < n;) {
     if (s[i] != t[i]) {
       if (i + 1 < n && s[i] != s[i + 1] && s[i + 1] != t[i + 1]) {
         i += 2;
       } else {
         i++;
       }
       cost++;
     } else {
       i++;
     }
   }
   cout << cost << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 inline long long read() {
   long long x = 0, f = 1;
   char ch = getchar();
   while (ch < '0' || ch > '9') {
     if (ch == '-') f = -1;
     ch = getchar();
   }
   while (ch >= '0' && ch <= '9') {
     x = x * 10 + (ch ^ 48);
     ch = getchar();
   }
   return x * f;
 }
 const long long N = 100010;
 struct edge {
   long long v, nxt;
 } e[N << 1];
 long long head[N], cnt, n, x, y, a[N], dp[N][2], f[N], g[N], h[N];
 inline void add(long long u, long long v) {
   e[++cnt].v = v;
   e[cnt].nxt = head[u];
   head[u] = cnt;
 }
 inline void dfs(long long u, long long fa) {
   dp[u][0] = dp[u][1] = f[u] = g[u] = a[u];
   h[u] = 0;
   for (long long i = head[u]; i; i = e[i].nxt) {
     long long v = e[i].v;
     if (v == fa) continue;
     dfs(v, u);
     dp[u][0] = max(dp[u][0], max(dp[v][0], dp[u][1] + dp[v][1]));
     dp[u][0] = max(dp[u][0], max(f[u] + g[v], f[v] + g[u]));
     dp[u][1] = max(dp[u][1], max(dp[v][1], f[u] + f[v]));
     g[u] = max(g[u], a[u] + g[v]);
     g[u] = max(g[u], max(f[u] + dp[v][1], f[v] + a[u] + h[u]));
     h[u] = max(h[u], dp[v][1]);
     f[u] = max(f[u], f[v] + a[u]);
   }
 }
 inline void mian() {
   n = read();
   for (long long i = 1; i <= n; ++i) a[i] = read();
   for (long long i = 1; i <= n - 1; ++i) {
     x = read();
     y = read();
     add(x, y);
     add(y, x);
   }
   dfs(1, 0);
   printf( %lld
 , dp[1][0]);
 }
 signed main() {
   long long qwq = 1;
   while (qwq--) mian();
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int main() {
   int t;
   cin >> t;
   while (t--) {
     string s;
     string t;
     cin >> s >> t;
     int ans = 1;
     int last = INT_MIN;
     vector<int> v[26];
     int dp[2][26];
     for (int i = 0; i < s.length(); i++) {
       v[s[i] - 'a'].push_back(i);
     }
     for (int i = 0; i < 26; i++) {
       dp[0][i] = v[i].size();
       dp[1][i] = 0;
     }
     for (int i = 0; i < t.length(); i++) {
       if (v[t[i] - 'a'].empty()) {
         ans = -1;
         break;
       } else {
         int temp = dp[1][t[i] - 'a'];
         vector<int>::iterator lower, l;
         l = v[t[i] - 'a'].begin() + temp;
         lower = lower_bound(l, v[t[i] - 'a'].end(), last);
         if (lower - v[t[i] - 'a'].begin() == dp[0][t[i] - 'a']) {
           ans++;
           last = v[t[i] - 'a'][0];
           for (int j = 0; j < 26; j++) dp[1][j] = 0;
           dp[1][t[i] - 'a'] = 1;
         } else {
           last = v[t[i] - 'a'][lower - v[t[i] - 'a'].begin()];
           dp[1][t[i] - 'a'] = lower - v[t[i] - 'a'].begin() + 1;
         }
       }
     }
     cout << ans << endl;
   }
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 string s;
 stack<long long> num;
 stack<char> fuhao;
 vector<int> pos;
 void calc() {
   char cc = fuhao.top();
   fuhao.pop();
   long long a = num.top();
   num.pop();
   long long b = num.top();
   num.pop();
   long long ans;
   if (cc == '*')
     ans = a * b;
   else
     ans = a + b;
   num.push(ans);
 }
 long long solve(string p) {
   int len = p.length();
   for (int i = 0; i < len; i++) {
     if (p[i] >= '0' && p[i] <= '9') {
       num.push(p[i] - '0');
     } else if (p[i] == '(') {
       fuhao.push(p[i]);
     } else if (p[i] == ')') {
       while (fuhao.top() != '(') calc();
       fuhao.pop();
     } else if (p[i] == '*') {
       fuhao.push(p[i]);
     } else if (p[i] == '+') {
       while (!fuhao.empty() && fuhao.top() == '*') {
         calc();
       }
       fuhao.push(p[i]);
     }
   }
   while (!fuhao.empty()) calc();
   long long ans = num.top();
   return ans;
 }
 int main() {
   cin >> s;
   int len = s.length();
   pos.push_back(-1);
   for (int i = 1; i < len; i += 2)
     if (s[i] == '*') pos.push_back(i);
   pos.push_back(len);
   long long ans = 0;
   for (int i = 0; i < pos.size() - 1; i++) {
     for (int j = i + 1; j < pos.size(); j++) {
       string temp = s;
       temp.insert(pos[i] + 1, 1, '(');
       temp.insert(pos[j] + 1, 1, ')');
       ans = max(ans, solve(temp));
     }
   }
   cout << ans << endl;
   return 0;
 }


#include <bits/stdc++.h>
 using namespace std;
 int dx[4] = {1, 0, -1, 0};
 int dy[4] = {0, 1, 0, -1};
 long long n, m, k;
 long long arr[300001];
 long long arr2[300001];
 int main() {
   std::ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int t;
   cin >> t;
   cin.ignore();
   for (int test_case = 0; test_case < t; ++test_case) {
     int flg = 0;
     long long ans1 = 0, ans2 = 0;
     string ans[3] = { NO ,  YES ,  R };
     string s1, s2 =   ;
     cin >> n >> k;
     for (int i = 0; i < n; ++i) {
       cin >> arr[i];
     }
     int i;
     long long mn = 10000000000;
     long long mx = 0;
     for (i = 0; i < n; ++i) {
       if (mn < arr[i]) {
         if (mn != 10000000000) {
           ans1 += mx - mn;
           mx = 0;
           mn = 10000000000;
         }
       }
       mx = max(mx, arr[i]);
       mn = min(mn, arr[i]);
     }
     ans1 += mx;
     cout << ans1;
     cout << '
';
   }
   return 0;
 }


#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;
 }