#include <bits/stdc++.h>
//#include <atcoder/modint>
//#include <atcoder/math>
//#include <boost/multiprecision/cpp_int.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPP(i, n) for(int i = 1; i <= n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define EPS (1e-9)
#define INF (1e17)
#define PI (acos(-1))
//const double PI = acos(-1);
//const double EPS = 1e-15;
//long long INF=(long long)1E17;
#define i_7 (long long)(1e9+7)
//#define i_7 998'244'353
long mod(long a){
    long long c = a % i_7;
    if(c >= 0) return c;
    return c + i_7;
}
long long po(long a, long b){
    if(b == 0) return 1;

    long long z = po(a , b / 2);
    z = mod(z * z);
    if(b % 2 != 0) z = mod(a * z);
    return z;
}

bool prime_(int n){
    if(n == 1){
        return false;
    }else if(n == 2){
        return true;
    }else{
        for(int i = 2; i <= std::sqrt(n); i++) if(n % i == 0) return false;
        return true;
    }
}

long long gcd_(long long a, long long b){
    if(a < b) std::swap(a,b);
    if(a % b == 0) return b;
    else return gcd_(b, a % b);
}

long long lcm_(long long x, long long y){
    return (x / gcd_(x,y)) * y;
}

using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//using mint = modint998244353;
//using namespace boost::multiprecision;
//using namespace __gnu_pbds;

vector<vector<int>> G;
vector<int> in_t, out_t;
vector<vector<int>> d_in_t;
int now;
void dfs(int v = 0, int depth = 0){
  in_t[v] = now++;
  d_in_t[depth].push_back(in_t[v]);
  for(int nxt: G[v]) dfs(nxt, depth + 1);
  out_t[v] = now++;
  return;
}

int main(){
  //using namespace std;

  int n;
  cin>>n;
  G.resize(n);
  int p[n];
  for(int i = 1; i < n; i++){
    cin>>p[i];
    p[i]--;
    G[p[i]].push_back(i);
  }
  in_t.resize(n); out_t.resize(n);
  d_in_t.resize(n);
  now = 0;
  dfs();
  REP(depth, n) sort(ALL(d_in_t[depth]));
  int Q;
  cin>>Q;
  REP(_, Q){
    int u, d;
    cin>>u>>d;
    u--;
    //sort(ALL(d_in_t[d]));
    int l = lower_bound(ALL(d_in_t[d]), in_t[u]) - d_in_t[d].begin();
    int r = lower_bound(ALL(d_in_t[d]), out_t[u]) - d_in_t[d].begin();
    int ans = r - l;
    cout << ans << endl;
  }
  return 0;
}

//REMEMBERING THE PAST , CAN AVOID REPEAT THE SAME
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define inf 10000000000000000
typedef long long ll;
#define mp make_pair
#define f first
#define s second
// DP o Trees::http://cfrp.azurewebsites.net/blog/entry/20935
ll GCD(ll a,ll b){
    ll maxi=max(a,b);
    ll mini=min(a,b);
    while(maxi && mini){
        ll t=mini;
        mini=maxi%mini;
        maxi=t;
    }
    return max(maxi,mini);
}
ll power(ll x,ll y){
    ll res=1;
    while (y){
        if (y & 1){
            res=(res*x);
        }
        x=(x*x);
        y=y>>1;
    }
    return res;
}
ll ncr(ll x, ll y){
 	ll ans=1;
 	y=(y > x-y? x-y: y);
 	for(ll i=0; i<y; i++){
 		ans=(ans*(x-i));
 		ans=(ans/(i+1));
 	}
 	return ans;
 }
int lower_bound(vector<int> &v,int x){
    int l=0,n=v.size(),r=n-1,mid;
    int ans=0;
    while (l<=r){
        mid=l+(r-l)/2;
        if (v[mid]>=x){
            ans=mid;
            r=mid-1;
        }
        else{
            l=mid+1;
        }
    }
    return ans;
}
int upper_bound(vector<int> &v,int x){
    int l=0,n=v.size(),r=n-1,mid;
    int ans=n;
    while (l<=r){
        mid=l+(r-l)/2;
        if (v[mid]>x){
            ans=mid;
            r=mid-1;
        }
        else{
            l=mid+1;
        }
    }
    return ans;
}
vector<int> tree[200005];
vector<int> tree1[200005];
int dist[200005];
int in[200005];
int out[200005];
int maxi=0;
int timer=0;
void dfs(int u,int par){
	if(par!=-1){
		dist[u]=dist[par]+1;
		maxi=max(maxi,dist[u]);
	}
	in[u]=++timer;
	tree1[dist[u]].push_back(u);
	for(int v:tree[u]){
		if(par!=v){
			dfs(v,u);
		}
	}
	out[u]=++timer;
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n,p;
	cin>>n;
	for(int i=2;i<=n;i++){
		cin>>p;
		tree[i].push_back(p);
		tree[p].push_back(i);
	}
	memset(dist,0,sizeof(dist));
	dfs(1,-1);
	int q;
	cin>>q;
	while(q--){
		int u,d;
		cin>>u>>d;
		if(d>maxi || dist[u]>d){
			cout<<"0\n";
		}
		else{
			vector<int> nodes=tree1[d];
			int n1=nodes.size(),l=0,r=n1-1,ans1=-1,ans2=-1,mid;
			while(l<=r){
				mid=l+(r-l)/2;
				if(in[nodes[mid]]>=in[u]){
					ans1=mid;
					r=mid-1;
				}
				else{
					l=mid+1;
				}
			}
			if(ans1==-1){
				cout<<"0\n";
				continue;
			}
			l=0;r=n1-1;
			while(l<=r){
				mid=l+(r-l)/2;
				if(in[nodes[mid]]<=out[u]){
					ans2=mid;
					l=mid+1;
				}
				else{
					r=mid-1;
				}
			}
			if(ans2==-1 || ans1>ans2){
				cout<<"0\n";
				continue;
			}
			cout<<ans2-ans1+1<<"\n";
		}
	}
	return 0;
}


#include <iostream>
using namespace std;

int main(){
    int n,m;
    cin >> n >> m;
    cout << (m % n? "No":"Yes") << endl;
}

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

int n;
string s;

int solve();

int main() {
  cin >> n >> s;
  cout << solve() << endl;
  return 0;
}

int solve() {
  if (s[0] != s[n - 1]) return 1;
  for (int i = 2; i < n; ++i)
    if (s[0] != s[i - 1] && s[0] != s[i]) return 2;
  return -1;
}


#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FILL0(x) memset(x, 0, sizeof(x))
#define FILL1(x) memset(x, -1, sizeof(x))

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    int h, w, n, m;
    cin >> h >> w >> n >> m;

    int grid[h][w];
    FILL0(grid);

    rep(i, n)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        grid[a][b] = 1;
    }
    rep(i, m)
    {
        int c, d;
        cin >> c >> d;
        c--;
        d--;
        grid[c][d] = -1;
    }

    int grid2[h][w];
    rep(i, h)
    {
        rep(j, w)
        {
            grid2[i][j]=grid[i][j];
        }
    }

    rep(i, h)
    {
        ll light = 0;
        rep(j, w)
        {
            if (grid2[i][j] == 0)
            {
                grid2[i][j] = light;
            }
            else if (grid2[i][j] == 1)
            {
                light = 1;
            }
            else //-1のとき
            {
                light = 0;
            }
        }
        light = 0;
        for (int j = w - 1; j > -1; j--)
        {
            if (grid2[i][j] == 0)
            {
                grid2[i][j] = light;
            }
            else if (grid2[i][j] == 1)
            {
                light = 1;
            }
            else //-1のとき
            {
                light = 0;
            }
        }
    }

    rep(j, w)
    {
        ll light = 0;
        rep(i, h)
        {
            if (grid[i][j] == 0)
            {
                grid[i][j] = light;
            }
            else if (grid[i][j] == 1)
            {
                light = 1;
            }
            else //-1のとき
            {
                light = 0;
            }
        }
        light = 0;
        for (int i = h - 1; i > -1; i--)
        {
            if (grid[i][j] == 0)
            {
                grid[i][j] = light;
            }
            else if (grid[i][j] == 1)
            {
                light = 1;
            }
            else //-1のとき
            {
                light = 0;
            }
        }
    }

    ll ans = 0;

    rep(i, h)
    {
        rep(j, w)
        {
            if (grid[i][j] == 1 || grid2[i][j]==1)
            {
                ans++;
            }
        }
    }

    cout << ans << endl;

    return 0;
}

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<cstdint>
#include<cstddef>
#include<vector>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using usize = uint_fast64_t;
template<typename T>
using vec = vector<T>;
#define rep(i, n) for (i64 i = 0; i < (i64)(n); ++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using P = pair<i64,i64>;
void solve(){
}
vec<P> edges[111111];
vec<i64> ans;
i64 n;
void dfs(i64 now){
    for(auto v:edges[now]){
        i64 to = v.first;
        i64 val = v.second;
        if(ans[to] != 0)continue;
        if(val != ans[now]){
            ans[to] = val;
        }else{
            i64 V = val - 1;
            if(V == 0)V = n;
            ans[to] = V;
        }
        dfs(to);
    }
}
int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    i64 m;
    cin >> n >> m;
    ans.resize(n);
    rep(i,m){
        i64 a,b,c;
        cin >> a >> b >> c;
        a--;
        b--;
        edges[a].push_back({b,c});
        edges[b].push_back({a,c});
    }
    ans[0] = 1;
    dfs(0);
    rep(i,n){
        cout << ans[i] << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
int a,b,ans;
int main() {
	scanf("%d %d",&a,&b);
	for(int i=1;i<=200000;i++) {
		int k=a/i,now;
		if(a%i==0) now=i*k;
		else now=i*(k+1);
		if(a<=now&&now<=b&&a<=now+i&&now+i<=b) ans=i;
	}
	printf("%d\n",ans);
}

// Author : Jad Isaac
// ID: jadDebugs
// TASK: -----
// LANG: C++

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

#define ll long long
#define f first
#define s second
#define pii pair<int, int>

int getMaxGCD(vector<int> arr, int n) {
    int high = 0;

    for (int i = 0; i < n; i++)
        high = max(high, arr[i]);

    int divisors[high + 1] = {0};
    for (int i = 0; i < n; i++) {
        for (int j = 1; j*j <= arr[i]; j++) {
            if (arr[i] % j == 0) {
                divisors[j]++;
            if (j != arr[i] / j)
                divisors[arr[i] / j]++;
            }
        }
    }

        for (int i = high; i >= 1; i--) {
            if (divisors[i] > 1)
                return i;
        }
    return -1;
}

void setIO(string name = "") {
    ios_base::sync_with_stdio(0); cin.tie(0);
}

int main()
{
    setIO("");

    int a, b; cin >> a >> b;
    vector<int> arr(b-a+1);

    for (int i = a; i <= b; i++)
        arr[i-a] = i;

    cout << getMaxGCD(arr, b-a+1);
    return 0;
}


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

void solve(vector<int64_t> p) {
    const int64_t N = p.size();
    vector<int64_t> ans;
    while (true) {
        int64_t target = 0;
        while (target < N && p[target] == target) {
            //すでに整頓済みだったら終わり
            target++;
            continue;
        }

        if (target == N) {
            break;
            ;
        }

        //整列
        //現在位置を探す
        int64_t curr_pos = 0;
        while (p[curr_pos] != target) {
            curr_pos++;
        }

        if (curr_pos == target) {
            break;
        }

        if (ans.size() % 2 == 0) {
            //偶数番目の位置を選んでスワップ可能
            if (curr_pos % 2 == 0) {
                int64_t i = (p.size() % 2 == 0 ? p.size() - 2 : p.size() - 3);
                swap(p[i], p[i + 1]);
                ans.push_back(i + 1);
            } else {
                curr_pos--;
                swap(p[curr_pos], p[curr_pos + 1]);
                ans.push_back(curr_pos + 1);
            }
        } else {
            //奇数番目の位置を選んでスワップ可能
            if (curr_pos % 2 == 1) {
                int64_t i = (p.size() % 2 == 0 ? p.size() - 3 : p.size() - 2);
                swap(p[i], p[i + 1]);
                ans.push_back(i + 1);
            } else {
                curr_pos--;
                swap(p[curr_pos], p[curr_pos + 1]);
                ans.push_back(curr_pos + 1);
            }
        }
    }

    for (int64_t i = 0; i < N; i++) {
        assert(p[i] == i);
    }
    cout << ans.size() << endl;
    assert(ans.size() <= N * N);
    for (int64_t i = 0; i < ans.size(); i++) {
        cout << ans[i] << " \n"[i == ans.size() - 1];
        assert(i % 2 == 1 || (ans[i] % 2 == 1 && 1 <= ans[i] && ans[i] <= N - 1));
        assert(i % 2 == 0 || (ans[i] % 2 == 0 && 2 <= ans[i] && ans[i] <= N - 1));
    }
}

int main() {
    int64_t T;
    cin >> T;
    while (T--) {
        int64_t N;
        cin >> N;
        vector<int64_t> p(N);
        for (int64_t& i : p) {
            cin >> i;
            i--;
        }
        solve(p);
    }
}

#include<bits/stdc++.h>

#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))
#define int long long int

#define pb push_back
#define mpp make_pair
#define F first
#define S second
#define vec vector <int>
#define pr vector<pair<int,int>>
#define vecv vector <vec>
#define vecs vector <string>
#define vecc vector <char>

#define pi 3.141592653589793238
#define mod 1000000007
#define Mod 998244353
#define inf 1e16
#define precise(i) cout<<fixed << setprecision(i);
#define endl "\n"
#define yes cout<<"Yes"<<endl;
#define no cout<<"No"<<endl;
#define one cout<<-1<<endl;

#define err1(a) cout<<#a<<" "<<a<<endl;
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl;
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl;

#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define all(a) a.begin(),a.end()
#define show(a) for(auto xyz:a)cout<<xyz<<" ";cout<<endl;
#define show_nl(a) for(auto xyz:a)cout<<xyz<<endl;
#define loop(a) for(auto xyz:a)
#define f(i,a,b) for(int i=a;i<b;i++)
#define rf(i,a,b) for(int i=b;i>=a;i--)

const long double eps = 1e-8;

#define lb lower_bound  //THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS >= OUR VALUE
#define ub upper_bound  //THIS GIVES THE ITR TO THE ELEMENT IN RANGE [SI,EI) THAT IS > OUR VALUE
using namespace std;

void solve(){
    int n;
    cin>>n;
    int arr[n];
    f(i,0,n)cin>>arr[i];
    sort(arr,arr+n);
    f(i,1,n+1){
        if(arr[i-1]!=i){
            no;
            return;
        }
    }
    yes;
}

signed main() {
    fastio;
    int t=1;
    while(t--){
        solve();
    }
}


#include <bits/stdc++.h>
#define pb push_back
#define sz size()

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <long long> vll;

const int MOD = 1e9 + 7;
const int maxn = 2e5 + 7;

bool cmp(pii a, pii b){
    if(a.first > b.first)return true;
    else if(a.first == b.first){
        if(a.second >= b.second)return false;
        else return true;
    }
    else return false;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m, q;
    cin >> n >> m >> q;
    vector <pii> uw(n);
    for(int i = 0; i < n; i++){
        cin >> uw[i].second >> uw[i].first;
    }
    vector <int> x(m);
    for(int i = 0; i < m; i++){
        cin >> x[i];
    }
    sort(uw.begin(), uw.end());
    // for(int i = 0; i < n; i++){
    //     cout << uw[i].first << ' ' << uw[i].second << '\n';
    // }
    while(q--){
        int l, r;
        cin >> l >> r;
        l--;
        r--;
        vector <bool> used(m, false);
        int ans = 0;
        for(int i = n - 1; i >= 0; i--){
            int cursz = uw[i].second;
            int curw = uw[i].first;
            int mn = MOD;
            int mn_ind = -1;
            for(int j = 0; j < m; j++){
                if(!used[j] && !(j >= l && j <= r) && x[j] >= cursz){
                    if(mn > x[j]){
                        mn_ind = j;
                        mn = x[j];
                    }
                }
            }
            if(mn_ind != -1){
                ans += curw;
                used[mn_ind] = true;
            }
        }
        cout << ans << '\n';
    }
    return 0;
}

#include <bits/stdc++.h>

using ull = unsigned long long int;
using ll = long long;
using ld = long double;
using pii = std::pair<int,int>;
using pll = std::pair<ll, ll>;
using vi = std::vector<int> ;
using vvi = std::vector<vi> ;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vd = std::vector<double> ;
using vvd = std::vector<vd> ;
using qi = std::queue<int> ;
using vpii = std::vector<std::pair<int, int> >;
using vpll = std::vector<pll>;
using namespace std;

#define rep(i,j) for(int (i)=0;(i)<(j);(i)++)
#define drep(i,j) for(int (i)=(j);(i) >= 0;(i)--)

template<class T1, class T2> inline void chmin(T1 &a, T2 b){if(a > b) a = b;}
template<class T1, class T2> inline void chmax(T1 &a, T2 b){if(a < b) a = b;}
template<class T> inline void pri(T a){cout << a << endl;}
template<class Z> using vec = vector<Z>;
template<class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

inline void IN(void){
  return;
}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
  cin >> first;
  IN(rest...);
  return;
}

inline void OUT(void){
 cout << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
cout << first << " " ;
OUT(rest...);
   return;
}
const int max_n = 3 * (1e5) + 1;
const int max_m = 83 * (1e5) + 1;
const int INF = int(1e9);
const long long int INFL = (long long int)1e18;

int n,m,k;
string S;
int ans;

void solve()
{
  int N,M,Q; IN(N,M,Q);
  vpii lug(N);
  vi box(M);
  rep(i,N)
  {
    IN(lug[i].second,lug[i].first);
  }
  rep(i,M)IN(box[i]);
  sort(lug.begin(),lug.end());
  reverse(lug.begin(),lug.end());
  bool used[M+1];

  rep(_,Q)
  {
    ll ans = 0;
    int l,r;
    IN(l,r);
    l--;
    memset(used,0,sizeof(used));
    for(int i=l;i<r;i++)used[i] = 1;
    rep(i,N)
    {
      int id = -1;
      ll w = INF;
      rep(j,M)
      {
        if(used[j])continue;
        if(lug[i].second <= box[j])
        {
          if(w > box[j])
          {
            w = box[j];
            id = j;
          }
        }
      }
      if(id == -1)
      {
        continue;
      }
      ans += lug[i].first;
      used[id] = 1;
    }
    cout << ans << endl;
  }
}

signed main (int argc, char* argv[])
{
   cin.tie(0);
   ios::sync_with_stdio(false);
   int cases=1;
   //IN(cases);
   while(cases--)solve();
   //pri(ans);
   //for(auto c : ans){cout << c << endl;}
   //cout << fixed << setprecision(15) << ans << endl;
   return 0;
}


#include <iostream>
using namespace std;
int main(){
	int a,b;
	cin >> a >> b;
	cout << (a+b)/2 << " " << (a-b)/2 << "\n";
	}

/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>

#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long ll;
typedef long double ld;

int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}

ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}

ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll conbinationMemo[100][100];

void cmemoInit(){
    rep(i, 100){
        rep(j, 100){
            conbinationMemo[i][j] = -1;
        }
    }
}

ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}

ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
//-----------------------ここから-----------
int main(void){
    ll a, b;
    cin >> a >> b;

    for(ll i = -1000; i < 1000; i++){
        for(ll j = -1000; j < 1000; j ++){
            if(i + j == a && i - j == b){
                cout << i << " " << j << endl;
                return 0;
            }
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define rep2(i,n) for (int i=0; i<(n)-1; i++)
using ll = long long;
using P = pair<int,int>;
int main() {
  int t; cin >> t;
  rep(i,t) {
    ll n; cin >> n;
    if(n%4 == 0) cout << "Even" << endl;
    else if(n%4 == 2) cout << "Same" << endl;
    else cout << "Odd" << endl;
  }
}

#define _USE_MATH_DEFINES
#include<iostream>
#include<iomanip>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<climits>
#include<bitset>
#include<unordered_map>
#include<unordered_set>
#include<random>
#include<list>
#include<functional>

using namespace std;

#define MAX(A,B) ((A)>(B)?(A):(B))
#define MIN(A,B) ((A)<(B)?(A):(B))
#define LP(I,S,G) for (long long int I = S; I < G; I++)
#define IN(X) 	for (int i = 0; i < X.size(); i++)cin >> X[i]
#define OUT(X) 	for (int i = 0; i < X.size(); i++)cout << X[i]<<" "
#define SORT(X) sort(X.begin(), X.end())
#define CSORT(X,Y) sort(X.begin(), X.end(),Y)
#define COPY(X,Y) copy(X.begin(), X.end(), Y.begin())
#define ALL(X,Y) for (auto X = Y.begin(); X != Y.end(); X++)

template<class I1, class I2>
istream& operator>>(istream& s, pair<I1, I2>& in) {
	s >> in.first >> in.second;
	return s;
}

long long int M = pow(10, 9) + 7;

int main(void) {
	string s;
	cin >> s;
	stack<char> ans;
	LP(i, 0, s.size()) {
		switch (s[i]) {
		case '0':
			ans.push('0');
			break;
		case '1':
			ans.push('1');
			break;
		case '6':
			ans.push('9');
			break;
		case '8':
			ans.push('8');
			break;
		case '9':
			ans.push('6');
		}
	}

	while (ans.size() != 0) {
		cout << ans.top();
		ans.pop();
	}
	return 0;
}


/*                                بِسْمِ اللَّـهِ الرَّحْمَـٰنِ الرَّحِيمِ

                                    ( إِنِ الْحُكْمُ إِلَّا لِلَّهِ )

*/
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<pair<int,int> > vpii;
typedef unsigned long long ull;
typedef long double ld;
const long double pi=acos(-1);
#define x           first
#define y           second
#define read(n,a)   for(int i=0;i<n;i++)cin>>a[i];
#define rep(i,z,n)  for(ll i=z;i<n;i++)
#define rrep(i,z,n) for(ll i=z-1;i>=n;i--)
#define all(v)      v.begin(),v.end()
#define clr(v,d)    memset(v,d,sizeof(v));

ll  a[3002][5];
ll  b[200002];

//int dx[] = {0, 0, 1,1, 1,-1,-1,-1};
//int dy[] = {1,-1, 0,1,-1, 0,1 ,-1};
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};

string s, ss;

ll n,m,k,w;
ll  ans ,ans1;
ll mod = 1e9 +7;

bool can(ll x)
{
    set<int>s;
    rep(i,0,n)
    {
        int bit = 0;
        rep(j,0,5)bit<<=1 , bit|=a[i][j]>=x;
        s.insert(bit);
    }
    for(auto i : s)for(auto j : s )for(auto k : s)if( (i|j|k) == 31)return 1;
    return 0;
}
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int t=1;
    //cin>>t;
    while(t--)
    {
        cin>>n;
        rep(i,0,n)rep(j,0,5)cin>>a[i][j];

        ll l = 0 , r= 1e9 , md;
        while(l<=r)
        {
            md=(l+r)/2;
            if(can(md))ans=md,l=md+1;
            else r=md-1;
        }
        cout<<ans;
    }
    return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
#define pdd pair<double,double>
#define pii pair<int,int>
#define pll pair<LL,LL>
#define mp make_pair
#define LL long long
#define ULL unsigned long long
#define sqr(x) ((x)*(x))
#define pi acosl(-1)
#define MEM(x) memset(x,0,sizeof(x))
#define MEMS(x) memset(x,-1,sizeof(x))
using namespace std;
#define Point pdd
#define Polygon vector<Point>
#define Line pair<Point,Point>

void solve() {
    LL A, B, W;
    cin >> A >> B >> W;
    W *= 1000;
    LL mn = INT_MAX, mx = INT_MIN;
    for(LL i = 1; i <= W; i++){
        LL avg = W / i, left = W % i;
        if(avg < A || (left && avg >= B) || avg > B){
            continue;
        }
        mn = min(i, mn);
        mx = max(i, mx);
    }
    if(mn == INT_MAX){
        cout << "UNSATISFIABLE";
    }else{
        cout << mn << ' ' << mx;
    }
}

int main() {
//    solve();
    int T = 1;
    //cin >> T;
    for (int i = 0; i < T; ++i) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define sz(a) ((int)a.size())
#define re return
#define all(a) a.begin(),a.end()
#define int long long
#define Type int
#define rept(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,a) rept(i,0,a)
using namespace std;
const int dx[4]={-1,1,0,0};
const int dy[4]={0,0,-1,1};
vector<int>val;
int l[200005],r[200005],cst[200005];
int n,c;
int use[400005],un[400005],st[400005],tot[400005];
int ans;
void input(){
	cin>>n>>c;
	rep(i,n){
		cin>>l[i]>>r[i]>>cst[i];
		val.pb(l[i]),val.pb(r[i]+1);
	}
    sort(all(val));
    val.erase(unique(all(val)),val.end());
    rep(i,n){
    	l[i]=lower_bound(all(val),l[i])-val.begin();
    	r[i]=lower_bound(all(val),r[i]+1)-val.begin();
    	st[l[i]]+=cst[i];
    	st[r[i]]-=cst[i];
	}
	int k=st[0];
	rept(i,1,sz(val)){
		int n_cst=min(c,k)*(val[i]-val[i-1]);
		k+=st[i];
		ans+=n_cst;
	}
	cout<<ans;
}
signed main()
{
	input();
	re 0;
}

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

#define ll long long
#define mod 1000000007
#define endl "\n"

void solve(){
    ll n,c;
    cin>>n>>c;
    vector<pair<ll,ll>> arr;
    for(ll i=0;i<n;i++){
        ll a,b,k;
        cin>>a>>b>>k;
        arr.push_back({a,k});
        arr.push_back({b+1,-k});
    }
    sort(arr.begin(),arr.end());
    ll ans=0,sum=0,t=0;
    for(auto [x,y]:arr){
        if(x!=t){
            ans += min(c,sum)*(x-t);
            t=x;
        }
        sum+=y;
    }
    cout<<ans<<endl;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ll tc=1;
    // cin>>tc;
    for(ll t=1;t<=tc;t++){
        // cout<<"Case #"<<t<<": ";
        solve();
    }
}


#include <bits/stdc++.h>

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define ps(x, y) fixed << setprecision(y) << x
#define int long long

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vp;

// Global

const int INF = 1e18;

void solve() {
    int n;
    cin >> n;
    vi res;
    for(int i = 1; i <= sqrt(n); i++) {
        if(n % i == 0) {
            res.emplace_back(i);
            if(n / i != i)
                res.emplace_back(n / i);
        }
    }
    sort(all(res));
    for(auto &i: res)
        cout << i << '\n';
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    solve();
}

//Coded By Vishal Mourya - The Legendary Coder
#include<bits/stdc++.h>
#define ll long long int
#define vec vector<ll>
#define f(var,a,b) for(ll var = a ; var < b ; var++ )
#define fr(var,a,b) for(ll var = a ; var > b ; var-- )
#define fasthoja ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
int main(void){
    fasthoja;
    ll x; cin >> x;
    if( x >= 0 ) cout << x << "\n";
    else cout << "0\n";
    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include "bits/stdc++.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define int long long int
#define SYNC std::ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define FRE freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
typedef vector<ii>   vii;
//typedef tree<int, null_type, less<int>, rb_tree_tag,
//             tree_order_statistics_node_update>
//    ost;
#define rep(i,l,r)   for (int i = (l); i < (r); i++)
#define here cout << " Hey!!\n";
#define  pb push_back
#define  F  first
#define  S  second
#define all(v) (v).begin(),(v).end()
#define sz(a) (int)((a).size())
#define sq(x) ((x)*(x))
template <typename T>
void etch(T V) {
    for (auto x : V)    cout << x << ' ' ;
    cout << '\n';
}
vector<string> vec_splitter(string s) {
    s += ',';
    vector<string> res;
    while(!s.empty()) {
        res.push_back(s.substr(0, s.find(',')));
        s = s.substr(s.find(',') + 1);
    }
    return res;
}
void debug_out(
        vector<string> __attribute__ ((unused)) args,
        __attribute__ ((unused)) int idx,
        __attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
    if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
    stringstream ss; ss << H;
    cerr << args[idx] << " = " << ss.str();
    debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
const int MOD = 1e9+7;
const int MOD1 = 998244353;
const int N = 1500+5;
const int INF = 1000111000111000111LL;
const ld EPS = 1e-12;
const ld PI = 3.141592653589793116;

int a[N][N], vis[N][N][2], col[N][N];
int h, w, n, m;

void travH (int x, int y)
{
    for (int ny = y + 1; ny < w; ny++) {
        if (vis[x][ny][0] or a[x][ny] == -1) {
            break;
        } else {
            vis[x][ny][0] = col[x][ny] = 1;
        }
    }
    for (int ny = y - 1; ny >= 0; ny--) {
        if (vis[x][ny][0] or a[x][ny] == -1) {
            break;
        } else {
            vis[x][ny][0] = col[x][ny] = 1;
        }
    }
}

void travV (int x, int y)
{
    for (int nx = x - 1; nx >= 0; nx--) {
        if (vis[nx][y][1] or a[nx][y] == -1) {
            break;
        } else {
            vis[nx][y][1] = col[nx][y] = 1;
        }
    }
    for (int nx = x + 1; nx < h; nx++) {
        if (vis[nx][y][1] or a[nx][y] == -1) {
            break;
        } else {
            vis[nx][y][1] = col[nx][y] = 1;
        }
    }
}

int32_t main()
{
    SYNC
    cin >> h >> w >> n >> m;
    rep(i,0,n) {
        int x, y; cin >> x >> y;
        x--, y--;
        a[x][y] = 1;
    }
    rep(i,0,m) {
        int x, y; cin >> x >> y;
        x--, y--;
        a[x][y] = -1;
    }

    rep(i,0,h) {
        rep(j,0,w) {
            if (a[i][j] == 1) {
                col[i][j] = 1;
                travV(i, j);
                travH(i, j);
            }
        }
    }

    // rep(i,0,h) {
    //     rep(j,0,w) {
    //         cout << a[i][j] << ' ';
    //     } cout << '\n';
    // }
    // cout << '\n';
    // rep(i,0,h) {
    //     rep(j,0,w) {
    //         cout << col[i][j] << ' ';
    //     } cout << '\n';
    // }

    int ans = 0;
    rep(i,0,h) {
        rep(j,0,w) {
            ans += col[i][j];
        }
    }
    cout << ans;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
bool _DD = false;
#define _D(a)if(_DD)cout<<#a<<':'<<a<<" | ";
#define DBG(a){_D(a);if(_DD)cout<<'\n';}
#define DBG2(a,b){_D(a);_D(b);if(_DD)cout<<'\n';}
#define DBG3(a,b,c){_D(a);_D(b);_D(c);if(_DD)cout<<'\n';}
#define DBG4(a,b,c,d){_D(a);_D(b);_D(c);_D(d);if(_DD)cout<<'\n';}
#define DBG5(a,b,c,d,e){_D(a);_D(b);_D(c);_D(d);_D(e);if(_DD)cout<<'\n';}
#define DBG6(a,b,c,d,e,f){_D(a);_D(b);_D(c);_D(d);_D(e);_D(f);if(_DD)cout<<'\n';}
#define PB push_back
#define EB emplace_back
#define btoe(v) (v).begin(),(v).end()
#define etob(v) (v).rbegin(),(v).rend()
#define F first
#define S second
#define SZ size()

typedef unsigned long long int ulli;
typedef long long int lli;
typedef long double ldbl;
#define f(i,a,b) for(int i=(a); i<(b); ++i)
#define fr(i,a,b) for(int i=(a); i>(b); --i)
#define fl(i,a,b) for(lli i=(a); i<(b); ++i)
#define flr(i,a,b) for(lli i=(a); i>(b); --i)
#define w(x) lli x; cin>>x; while(x--)
#define inpn(n,v,T) T n; cin>>n; vector<T> v(n); fl(i,0,n) cin>>v[i];
#define inp(n,v,T) vector<T> v(n); fl(i,0,n) cin>>v[i];

typedef pair<int,int> pii;typedef vector<int> vi;typedef vector<vi> vvi;typedef vector<pii> vpii;typedef vector<vector<pii>> vvpii;typedef pair<lli,lli> plli;typedef vector<lli> vlli;typedef vector<vlli> vvlli;typedef vector<plli> vplli;typedef vector<vector<plli>> vvplli;typedef vector<bool> vb;typedef vector<vb> vvb;typedef vector<string> vs;typedef vector<vector<string>> vvs; typedef vector<char> vc; typedef vector<vc> vvc;
#define unmp unordered_map

const lli PRIME = 1000000007ll;
const lli PRIME2 = 998244353ll;

inline void debugORfast(bool a=0,bool b=1){
    if(a) _DD=1;
    if(b){ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}
}

//Question Link

int main(int argc,char* argv[])
{
    if(argc>1 and strcmp(argv[1],"1")==0) debugORfast(1,1);
    else debugORfast(0,1);

	int n; cin>>n;
	int len = (1<<n);
	inp(len,v,int);
	vb vis(len,false);
	f(i,0,n-1){
		int fir = -1;
		int ptr = 0;
		while(ptr<len){
			if(vis[ptr]==false){
				if(fir==-1){
					fir=ptr;
				}else{
					int val1 = v[fir];
					int val2 = v[ptr];
					if(val1>val2){
						vis[ptr] = true;
					}else{
						vis[fir] = true;
					}
					fir=-1;
				}
			}
			++ptr;
		}
	}
	//f(i,0,vis.SZ) cout<<vis[i]<<' '; cout<<endl;
	int ansid=-1,ansval=1e9+1;
	f(i,0,len){
		if(!vis[i] and v[i]<ansval){
			ansval = v[i];
			ansid = i;
		}
	}
	cout<<ansid+1<<endl;
    return 0;
}


#include <bits/stdc++.h>

namespace {
    using namespace std;
#define int long long

    template<typename T>
    inline bool whitespace(const vector<T> &) { return false; }

    template<typename T>
    inline bool whitespace(const T &) { return true; }

    inline bool whitespace(const char) { return false; }

    template<typename F, typename S>
    inline bool whitespace(const pair<F, S> &) { return false; }

    template<typename F, typename S>
    ostream &operator<<(ostream &os, const pair<F, S> &p) {
        os << p.first << (whitespace(p.second) ? " " : "") << p.second << "\n";
        return os;
    }

    template<typename F, typename S>
    istream &operator>>(istream &is, pair<F, S> &p) {
        is >> p.first >> p.second;
        return is;
    }

    template<typename T>
    istream &operator>>(istream &is, vector<T> &v) {
        for (T &t : v) is >> t;
        return is;
    }

    template<typename T>
    ostream &operator<<(ostream &os, const vector<T> &v) {
        for (const T &t : v) os << t << (whitespace(t) ? " " : "");
        os << "\n";
        return os;
    }

    template<typename T>
    istream &operator>>(istream &is, deque<T> &v) {
        for (T &t : v) is >> t;
        return is;
    }

    template<typename T>
    ostream &operator<<(ostream &os, const deque<T> &v) {
        for (const T &t : v) os << t << (whitespace(t) ? " " : "");
        os << "\n";
        return os;
    }

    class exit_exception : exception {
    };

    void answer() {}

    template<typename First, typename... Args>
    void answer(First &&val, Args &&... ans) {
        cout << val << "\n";
        answer(ans...);
        throw exit_exception();
    }

    struct autoint {
        int x;

        autoint(int i) : x(i) {}

        autoint() { cin >> x; }

        operator int() { return x; }

        int operator--() { return --x; }

        int operator--(signed) { return x--; }

        int operator++() { return ++x; }

        int operator++(signed) { return x++; }
    };
}

void solve();

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t = 1;
//    cin >> t;
    do { try { solve(); } catch (exit_exception &) {}} while (--t);
    return 0;
}

void solve() {
    autoint h, w;
    vector<vector<pair<int, int>>> graph(h * w + 26);
    vector<vector<char>> board(h, vector<char>(w));
    cin >> board;
    int start = 0, finish = 0;
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            if (board[i][j] == 'S') start = i * w + j;
            if (board[i][j] == 'G') finish = i * w + j;
            vector<int> dx{-1, 0, 1, 0};
            vector<int> dy{0, -1, 0, 1};
            for (int k = 0; k < 4; ++k) {
                int x = i + dx[k], y = j + dy[k];
                if (x < 0 || x == h || y < 0 || y == w) continue;
                if (board[x][y] == '#' || board[i][j] == '#') continue;
                graph[i * w + j].emplace_back(x * w + y, 2);
                graph[x * w + y].emplace_back(i * w + j, 2);
            }
            if ('a' <= (board[i][j]) && board[i][j] <= 'z') graph[i * w + j].emplace_back(h * w + board[i][j] - 'a', 1);
            if ('a' <= (board[i][j]) && board[i][j] <= 'z') graph[h * w + board[i][j] - 'a'].emplace_back(i * w + j, 1);
        }
    }
    queue<int> bfs;
    bfs.push(start);
    vector<int> dist(graph.size(), 1e9);
    dist[start] = 0;
    while (!bfs.empty()) {
        int cur = bfs.front();
        bfs.pop();
        for (auto next : graph[cur]) {
            if (dist[next.first] > dist[cur] + next.second) {
                dist[next.first] = dist[cur] + next.second;
                bfs.push(next.first);
            }
        }
    }
    cout << (dist[finish] == 1e9 ? -1 : dist[finish] / 2) << '\n';
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
const int INF = 5e6;

int di[4] = {1, 0, -1, 0};
int dj[4] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  rep(i,h) cin >> s[i];
  // vector dist(h, vector(w, INF));
  vector<vector<int>> dist(h, vector<int>(w, INF));
  queue<P> q;
  rep(i,h)rep(j,w) {
    if (s[i][j] == 'S') {
      q.emplace(i,j);
      dist[i][j] = 0;
    }
  }
  vector<vector<P>> warps(256);
  rep(i,h)rep(j,w) warps[s[i][j]].emplace_back(i,j);

  while (!q.empty()) {
    auto [i,j] = q.front(); q.pop();
    if (s[i][j] == 'G') {
      cout << dist[i][j] << endl;
      return 0;
    }
    rep(v,4) {
      int ni = i + di[v], nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
      if (s[ni][nj] == '#') continue;
      if (dist[ni][nj] != INF) continue;
      dist[ni][nj] = dist[i][j] + 1;
      q.emplace(ni,nj);
    }

    if (islower(s[i][j])) {
      for (P p : warps[s[i][j]]) {
        int ni = p.first, nj = p.second;
        if (dist[ni][nj] != INF) continue;
        dist[ni][nj] = dist[i][j] + 1;
        q.emplace(ni,nj);
      }
      warps[s[i][j]] = vector<P>();
    }
  }
  cout << -1 << endl;
  return 0;
}


/*

    author: Taka007

    2021.04.11 start

*/

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

int64_t Fac(int k)         // Definition of "Factorial"
{
  return(k==0 ? 1 : k*Fac(k-1));
}

int   sum(int l)        // Definition of "Sum total"

{
  return l*(l+1)/2;
}

int64_t Abs(int m)        // Definition of "Absolute value"

{
    if(m>=0)
  {
    return m;
  }

    else
  {
    return m*(-1);
  }

}

int main(){

  int n;

  cin>>n;

  int answer;

  answer=(n-1);

  cout<<answer<<endl;

return 0;

}

#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define ll long long
using namespace std;
using P = pair<int, int>;

int main(){
    int n;
    cin >> n;
    if(n==1) cout << 0 << endl;
    else{
        cout << n-1 << endl;
    }
}


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

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

int main(){
   int n,L;
   cin>>n>>L;
   vector<int> a(n+2),b(n+2);
   rep(i,1,n+1)cin>>a[i];
   rep(i,1,n+1)cin>>b[i];
   a[n+1]=a[n+1]=L+1;

   map<int,vector<int>> pos;
   rep(i,0,n+2)pos[a[i]-i].push_back(i);

   ll res=0;
   rep(i,1,n+1){
      if(a[i]==b[i])continue;
      int x=b[i]-i;
      if(x<0 or pos[x].empty()){
         puts("-1");
         return 0;
      }
      if(a[i]<b[i]){
         if(b[i]==b[i+1]-1){
            res++;
            continue;
         }
         int j=pos[x].front();
         res+=j-i;
      }
      else{
         if(b[i]==b[i-1]+1){
            res++;
            continue;
         }
         int j=pos[x].back();
         res+=i-j;
      }
   }
   cout<<res<<'\n';
   return 0;
}

#include<bits/stdc++.h>

using namespace std;
using ll = long long int;
using lc = complex<double>;

template<class T>bool chmin(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }

int main(void) {
    constexpr ll MOD = 1e9 + 7;
    constexpr double PI = acos(-1);
    cout << fixed << setprecision(32);
    cin.tie(0); ios::sync_with_stdio(false);

    ll n, l;
    cin >> n >> l;
    vector<ll> a(n+2), b(n+2);
    for(ll i=0; i<n; i++) cin >> a[i+1];
    for(ll i=0; i<n; i++) cin >> b[i+1];
    a[0] = b[0] = 0;
    a[n+1] = b[n+1] = l+1;

    n+=2;

    ll res = 0;
    /* { */
    /*     ll i = 0; */
    /*     while(i<n && a[i]>=b[i]) i++; */
    /*     while(i<n && a[i]<=b[i]) i++; */
    /*     if(i != n) goto ng; */
    /* } */

    cerr << "1st" << endl;

    {
        for(ll i=0; i<n-1; i++) {
            if(a[i] < b[i]) {
                ll j = upper_bound(a.begin(), a.end(), b[i]) - a.begin();
                while(j<n && !(j-i == a[j]-b[i])) j++;
                if(j == n) goto ng;
                cerr << i << ' ' << j << endl;
                res += j-i;

                while(i<j && b[i+1] == b[i]+1) i++;
            }
        }
    }

    cerr << "2nd" << endl;

    {
        for(ll i=0; i<n; i++)
            a[i] = l+1 - a[i], b[i] = l+1 - b[i];
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());

        for(ll i=0; i<n-1; i++) {
            if(a[i] < b[i]) {
                ll j = upper_bound(a.begin(), a.end(), b[i]) - a.begin();
                while(j<n && !(j-i == a[j]-b[i])) j++;
                if(j == n) goto ng;
                cerr << i << ' ' << j << endl;
                res += j-i;

                while(i<j && b[i+1] == b[i]+1) i++;
            }
        }
    }

    cout << res << endl;

    return 0;

ng:
    cout << -1 << endl;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
#define ll long long
using namespace std;

int main() {
	ios::sync_with_stdio(false);

    ll n; cin >> n;
    ll sum = 0;
    vector<ll> a(n); rep(i, n) cin >> a[i];

    sort(ALL(a), greater<int>());

    rep(i, n - 1){
    	sum += a[i] * (n - i - 1);
    	sum -= a[i + 1] * (i + 1);
    }

    cout << sum << "\n";

	return 0;
}

// #define _GLIBCXX_DEBUG

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) std::begin(x), std::end(x)

using namespace std;
void dump_func()
{
    cerr << endl;
}
template <class Head, class... Tail>
void dump_func(Head &&h, Tail &&... t)
{
    cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...);
}
#define dump(...)                                                                                \
    cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl, dump_func(__VA_ARGS__), \
        cerr << "*/\n\n";
typedef long long ll;

const double G = 0.577215;
const ll INF = 10000000;
ll solve(ll n, ll m, vector<ll> a)
{
    map<ll, int> count;
    ll tmp = 0;
    auto reset = [&]() {
        while (count.find(tmp) != count.end())
            tmp++;
    };
    auto decl = [&](ll t) {
        assert(count.find(t) != count.end());
        count[t] -= 1;
        if (count[t] == 0) {
            count.erase(t);
            if (t < tmp)
                tmp = t;
        }
    };
    auto incl = [&](ll t) {
        count[t] += 1;
        if (t == tmp)
            reset();
    };
    REP(i, m)
    {
        incl(a[i]);
    }
    ll res = tmp;
    REP3(i, m, n)
    {
        if (res == 0)
            break;
        incl(a[i]);
        decl(a[i - m]);
        res = min(tmp, res);
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n);
    REP(i, n) cin >> a[i];
    ll res = solve(n, m, a);
    cout << res << '\n';
    return 0;
}


#include <stdio.h>

int main(void) {
    int n, i;
    scanf("%d", &n);

    for (i = 1; i <= n; ++i) {
        printf("%d %d\n",((i << 1) - 1) % n + 1, (i << 1) % n + 1);
    }

    return 0;
}

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//optimal subsequences-2 technocup round-3 2020
using namespace std;
using namespace __gnu_pbds;
const int N=2e5+10;
//#define d double
#define F first
#define S second
#define ll long long int
#define FastIO ios_base::sync_with_stdio(false),cin.tie(0)
#define fix std::fixed<<setprecision(6)
const int M=1e9+7;
//const int M=998244353;
#define f(n) for(int i=0;i<n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define vi vector<int>
#define pb push_back
//#define si set<int>
#define cl(n,m) (ceil(double(n)/double(m)))
#define T() int t; cin>>t; while(t--)
#define pi pair<ll,ll>
//int MOD=998244353;
//const int MOD=1e9+7;
#define endl "\n"
const double PI = 3.141592653589793238460;
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)

//ll d1=304933,d2=4395853;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

//bitset<N>bs;
//bitset<512>bb[512];
//vector<vector<int>>arr;
//#ifndef ONLINE_JUDGE
//freopen("input.txt", "r" , stdin);
//freopen("output.txt", "w", stdout);
//#endif

ll MOD;

ll power(ll a,ll b)
{
    ll res=1;
    if(a==1)return 1;
    for(;b>0;b>>=1)
    {
        if(b&1)res=(res*a);
        if(res>MOD)res%=MOD;
        a=(a*a);
        if(a>MOD)a=a%MOD;
    }
    return res;
}

bool isPrime[3000000+1];
vector<ll>prime;

vector<int>divisor;

void seive2(int limit)
{
    divisor=vector<int>(limit+1,0);

    for(int i=2;i<=limit;i++)
    {
        if(divisor[i]==0)
        {
            divisor[i]=i;
            if(i*1ll*i<=limit)
            for(int j=i*i;j<=limit;j+=i)divisor[j]=i;
        }
    }
}

void seive(int limit)
{
    isPrime[0]=isPrime[1]=1;

    for(int i=2;i*i<=limit;i++)
    {
        if(!isPrime[i])
        for(int j=i*i;j<=limit;j+=i)
        {
            isPrime[j]=1;
        }
    }
    for(int i=2;i<=limit;i++)if(!isPrime[i])prime.pb(i);
}

#define pii pair<ll,pi>
ll pt[200010],sz[200010],wt[200010];

int finds(int x)
{
    while(pt[x]!=x)
    {
        pt[x]=pt[pt[x]];
        x=pt[x];
    }
    return pt[x];
}

bool unions(int x,int y)
{
    int px=finds(x),py=finds(y);

    if(px==py)return 0;

    if(sz[px]>sz[py])swap(px,py);

    pt[px]=py;
    sz[py]+=sz[px];
    wt[py]+=wt[px];

    return 1;
}

ll bit[200100];

ll query(ll bit[],int x)
{
    ll ans=0;
    for(int i=x;i>0;i-=(i&(-i))){
        ans+=bit[i];
    }
    return ans;
}

void update(ll bit[],int x,ll val)
{
    for(int i=x;i>0;i-=(i&(-i)))
        bit[i]+=val;
}

void update2(ll bit[],int x,ll val)
{
    for(int i=x;i<=600005;i+=(i&(-i)))
        bit[i]+=val;
}

ll query2(ll sum[],int x)
{
    ll ans=0;
    for(int i=x;i<=200005;i+=(i&(-i)))
        ans+=sum[i];
    return ans;
}

void dfs(int s,vector<int>&v,vector<int>&P,int visited[])
{
    visited[s]=1;
    v.pb(s);

    int x=P[s];

    if(!visited[x])
    {
        dfs(x,v,P,visited);
    }
}

set<ll>st;

void recur(int i,ll no)
{
    if(no>=10000)return;

    if(i==prime.size())
    {
        st.insert(no);
        return;
    }

    ll p=no;

    recur(i+1,p);

    for(int j=0;j<=20;j++)
    {
        p*=prime[i];
        if(p>10000)break;
        recur(i+1,p);
    }
}

void solve()
{
    ll n;cin>>n;
    ll sv=n;

    seive(100000);

    st.insert(6);
    st.insert(10);
    st.insert(15);

    vector<ll>v;

    v.pb(6);
    v.pb(10);
    v.pb(15);

    recur(0,6);
    recur(0,10);
    recur(0,15);

    //cout<<st.size()<<endl;

    for(auto it:st)
    {
        if(it!=6 and it!=10 and it!=15)
        {
            v.pb(it);
        }
    }

    for(int i=0;i<n;i++)cout<<v[i]<<' ';

}

int main()
{
    //FastIO;

    int t=1;
//    cin>>t;

	for(int i=1;i<=t;i++)
    {
       // cout<<"Case #"<<i<<": ";
        solve();
        //if(i!=t)cout<<endl;
    }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define pii pair<int,int>
#define all(a) a.begin(),a.end()
int bp(int x, int n){
	int res = 1;
	while(n){
		if(n&1) res = res * x;
		x *= x; n >>= 1;
	}
	return res;
}
void solve(){
	int n; cin >> n;
	for(int i = 1; bp(3,i) <= n; i++){
		int x = bp(3,i);
		for(int j = 1; bp(5,j) <= n; j++){
			int y = bp(5,j);
			if(x + y == n){
				cout << i << ' ' << j;
				return ;
			}
		}
	}
	cout << -1;
}
int32_t main(){
    ios::sync_with_stdio(false);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);freopen("answer.txt", "w", stdout);
	#endif
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}

//#include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include<cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <array>
#include <unordered_map>
#include<unordered_set>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include<iomanip>
#include<complex>
#include<fstream>
#include<assert.h>

#include<stdio.h>

using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;

template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;

const double EPS = 1e-16;

signed main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;

	vector<pii> F(N);
	rep(i, 0, N)
	{
		cin >> F[i].first >> F[i].second;
	}

	int add = 0;
	int UP = INF, DOWN = -INF;
	int cons = INF;

	rrep(i, 0, N)
	{
		if (F[i].second == 1)
		{
			UP -= F[i].first;
			DOWN -= F[i].first;
			add += F[i].first;
		}
		if (F[i].second == 2)
		{
			chmax(DOWN, F[i].first);
			if (UP <= DOWN)
			{
				cons = UP + add;
				break;
			}
		}
		if (F[i].second == 3)
		{
			chmin(UP, F[i].first);
			if (UP <= DOWN)
			{
				cons = DOWN + add;
				break;
			}
		}
	}

	int Q;
	cin >> Q;

	rep(i, 0, Q)
	{
		int x;
		cin >> x;
		if (cons != INF)
		{
			cout << cons << endl;
		}
		else
		{
			cout << min(UP, max(DOWN, x)) + add << endl;
		}
	}

	return 0;
}


#include <bits/stdc++.h>

#define fi first
#define se second
#define DB double
#define U unsigned
#define P std::pair
#define LL long long
#define LD long double
#define pb emplace_back
#define MP std::make_pair
#define SZ(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl

const int MAXN = 2e5 + 5;
int n,m,mid,ans;
std::vector<int> G[MAXN];
int f[MAXN],g[MAXN];

inline void dfs(int v,int fa=0){
    f[v] = 1e9;g[v] = -1e9;
    for(auto x:G[v]){
        if(x == fa) continue;
        dfs(x,v);
        f[v] = std::min(f[v],f[x]+1);
        g[v] = std::max(g[v],g[x]+1);
    }
    if(f[v] > mid) g[v] = std::max(g[v],0);
    if(f[v]+g[v] <= mid) g[v] = -1e9;
    if(g[v] == mid) ++ans,g[v] = -1e9,f[v] = 0;
}

inline bool chk(int x){
    mid = x;ans = 0;dfs(1);
    if(g[1] != -1e9) ++ans;
    return ans <= m;
}

int main(){
    scanf("%d%d",&n,&m);
    FOR(i,2,n){
        int u,v;scanf("%d%d",&u,&v);
        G[u].pb(v);G[v].pb(u);
    }
    int l = 0,r = n,ans = -1;
    while(l <= r){
        int mid = (l + r) >> 1;
        if(chk(mid)) ans = mid,r = mid-1;
        else l = mid+1;
    }
    printf("%d\n",ans);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

const int N = 200001;

int n, k;
vector <int> v[N];

pair <int, int> dfs(int node, int pnode, int f){
    int cnt = 0;
    int neg = 0, pos = 0;
    for(auto &i : v[node]){
        if(i == pnode) continue;
        auto c = dfs(i, node, f);
        cnt += c.first;
        if(c.second < 0) neg = max(neg, -c.second);
        else pos = max(pos, c.second);
    }
    if(pos <= neg - 1) return make_pair(cnt, -(neg - 1));
    if(pos == f) return make_pair(cnt + 1, -f);
    return make_pair(cnt, pos + 1);
}
int calc(int mid){
    auto p = dfs(1, 0, mid);
    return p.first + (p.second > 0);
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> k;
    for(int i = 0 ; i < n - 1 ; i++){
        int x, y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    int l = 1, r = n;
    while(l <= r){
        int mid = (l + r) / 2;
        if(calc(mid) <= k) r = mid - 1;
        else l = mid + 1;
    }
    cout << l << endl;
}


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

int main() {

    int A, B;
    cin >> A >> B;

    cout << 100*(1.0-(double)B/A) << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main()
{
     ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    ll sum=0;
    while (t--)
    {
        ll a, b;
        cin>>a>>b;
        ll s1=0, s2=0;
        if(a!=1)
        s1=(a*(a-1))/2;
        s2=(b*(b+1))/2;
        sum+=(s2-s1);
    }
    cout<<sum;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
string b[100000];
int n,a[100000],c[10000],m;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>b[i];
	    cin>>a[i];
		c[i]=a[i];
	}
	sort(c,c+n);
		for(int j=1;j<=n;j++){
			if(c[n-2]==a[j]){
				m=j;
			}
		}
		cout<<b[m]<<endl;;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define reps(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) reps(i, 0, n)
#define deps(i, a, n) for (int i = (a); i >= (n); --i)
#define dep(i, n) deps(i, n, 0)

signed main(void)
{
	int n; cin >> n;
	int ans[3000] = {6, 10, 15};
	int i = 3;
	reps (j, 16, 1000000)
	{
		if (j%6&&j%10&&j%15) continue;
		ans[i] = j;
		i++;
		if (i > 2505) break;
	}

	rep (j, n) cout << ans[j] << ' ';
	cout << endl;
}


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

void solve() {
	vector<vector<int> > v;
	for (int i = 0; 8 * i < 1000; i++) {
		vector<int> temp(10, 0);
		int cnt = 3, cur = 8 * i;
		while (cnt)
			temp[cur % 10]++, cur /= 10, cnt--;
		v.push_back(temp);
	}
	string s;
	cin >> s;
	if (s.size() < 3) {
		int cur = stoi(s);
		if (cur < 10) {
			if (cur == 8)
				cout << "Yes";
			else cout << "No";
			return ;
		}
		int first = cur / 10, second = cur % 10;
		if ((first * 10 + second) % 8 == 0 || (second * 10 + first) % 8 == 0) {
			cout << "Yes";
			return ;
		}
		cout << "No";
		return ;
	}
	vector<int> cnt(10, 0);
	for (auto u : s)
		cnt[u - '0']++;
	for (auto u : v) {
		bool ok = 1;
		for (int i = 0; i < 10; i++)
			ok &= (u[i] <= cnt[i]);
		if (ok) {
			cout << "Yes";
			return ;
		}
	}
	cout << "No";

}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	// int t; cin >> t;
	// while (t--)
	solve();

}

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
// #include<bits/stdc++.h>
#include<iostream>
#include<string>
using namespace std;

string dfs(int n,string s){
    if(n == 1)return s;
    while(n%3!=1){
        string t;
        for(int i=1;i<n;i++){
            if(s[i]==s[i-1])t+=s[i];
            else {
                int x = 'R'+'W'+'B';
                x -= s[i] + s[i-1];
                t += (char)x;
            }
        }
        swap(t,s);
        n--;
    }
    string t;
    for(int i=0;i<n;i+=3)t+=s[i];
    return dfs((int)t.size(),t);
}
int main(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    cout << dfs(n,s) << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=5e3+5;
const int mod=998244353;
int n,m;
int dp[15][N];
int fac[N],invf[N];
inline int po(int x,int k){
	int ret=1;
	while(k){
		if(k&1) ret=1ll*ret*x%mod;
		x=1ll*x*x%mod;
		k>>=1;
	}
	return ret;
}
inline int C(int x,int k){
	if(x<k) return 0;
	return 1ll*fac[x]*invf[k]%mod*invf[x-k]%mod;
}
int main(){
	int i,j,k;
	fac[0]=1;
	for(i=1;i<N;i++) fac[i]=1ll*fac[i-1]*i%mod;
	invf[N-1]=po(fac[N-1],mod-2);
	for(i=N-2;i>=0;i--) invf[i]=ll(i+1)*invf[i+1]%mod;
	cin>>n>>m;
	dp[0][0]=1;
	for(i=0;(1<<i)<=m;i++){
		for(j=0;j*(1<<i)<=m&&j<=n;j+=2){
			for(k=j*(1<<i);k<=m;k++)
				dp[i+1][k]=(dp[i+1][k]+1ll*dp[i][k-j*(1<<i)]*C(n,j))%mod;
		}
	}
	cout<<dp[i][m];
	return 0;
}

#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 998244353
#define inf 1000000000000000
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;

ll powa(ll a, ll b, ll c)
{
    a%=c;
    if(a<0)
	a+=c;
    ll res = 1;
    while(b>0)
    {
        if(b&1)
            res*=a, res%=c;
        a*=a, a%=c;
        b>>=1;
    }
    return res;
}

ll root = 31;
ll root_1;
ll root_pw = (1ll << 23);

void fft(vector<ll> & a, bool invert) {
    ll n = a.size();

    for (ll i = 1, j = 0; i < n; i++) {
        ll bit = n >> 1;
        for (; j & bit; bit >>= 1)
            j ^= bit;
        j ^= bit;

        if (i < j)
            swap(a[i], a[j]);
    }

    for (ll len = 2; len <= n; len <<= 1) {
        ll wlen = invert ? root_1 : root;
        for (ll i = len; i < root_pw; i <<= 1)
            wlen = (ll)(1LL * wlen * wlen % mod);

        for (ll i = 0; i < n; i += len) {
            ll w = 1;
            for (ll j = 0; j < len / 2; j++) {
                ll u = a[i+j], v = (ll)(1LL * a[i+j+len/2] * w % mod);
                a[i+j] = u + v < mod ? u + v : u + v - mod;
                a[i+j+len/2] = u - v >= 0 ? u - v : u - v + mod;
                w = (ll)(1LL * w * wlen % mod);
            }
        }
    }

    if (invert) {
        ll n_1 = powa(n,mod-2,mod);
        for (ll & x : a)
            x = (ll)(1LL * x * n_1 % mod);
    }
}

vector<ll> multiply(vector<ll> const& a, vector<ll> const& b) {
    vector<ll> fa(a.begin(), a.end()), fb(b.begin(), b.end());
    ll n = 1;
    while (n < a.size() + b.size())
        n <<= 1;
    fa.resize(n);
    fb.resize(n);

    while(root_pw>n)
    {
         root_pw>>=1;
         root*=root;
         root%=mod;
    }
    //cout<<"root = "<<root<<"\n";
    root_1 = powa(root,mod-2,mod);

    fft(fa, false);
    fft(fb, false);
    for (ll i = 0; i < n; i++)
        fa[i] *= fb[i], fa[i]%=mod;
    fft(fa, true);
    root = 31;
    root_pw = (1ll << 23);
    return fa;
}

ll norm(ll a)
{
    a%=mod;
    if(a<0)
        a+=mod;
    return a;
}

ll add(ll a, ll b)
{
    a = norm(a);
    b = norm(b);
    return norm(a+b);
}

ll mul(ll a, ll b)
{
    a = norm(a);
    b = norm(b);
    return norm(a*b);
}

ll sub(ll a, ll b)
{
    a = norm(a);
    b = norm(b);
    return norm(a-b);
}

ll divi(ll a, ll b)
{
    b = powa(b,mod-2,mod);
    return mul(a,b);
}

#define N 200005

ll fact[N];
ll ifact[N];

ll ncr(ll n, ll r)
{
    if(r>n)
        return 0;
    ll ans = fact[n];
    ans*=ifact[r];
    ans%=mod;
    ans*=ifact[n-r];
    ans%=mod;
    return ans;
}

#define N1 14

int main()
{
  FAST/**/

   fact[0] = 1;
   for(ll i=1;i<N;i++)
        fact[i] = fact[i-1]*i, fact[i]%=mod;
   for(ll i=0;i<N;i++)
        ifact[i] = powa(fact[i],mod-2,mod);

   ll n,m;
   cin>>n>>m;

   vector<ll> poly(1,1);
   for(ll i=0;i<14;i++)
   {
       vector<ll> poly1(m+1,0);
       ll temp = powa(2,i,mod);
       for(ll cnt = 0; cnt <= n && cnt <= m/temp; cnt += 2)
       {
           ll id = cnt*temp;
           poly1[id] = ncr(n,cnt);
           //cout<<"coeff = "<<poly1[id]<<'\n';
       }

       poly = multiply(poly, poly1);
       while(poly.size()>1 && poly.size()>m+1)
            poly.pop_back();
   }

   ll ans = 0;
   if(poly.size()>m)
        ans = poly[m];
   cout<<ans<<"\n";

   return 0;
}


// I SELL YOU...!
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
const ll INF = 1e15;
ll n,m,maxw=0;
signed main(){
  init_io();
  cin >> n >> m;
  vector<ll> w(n),l(m+1),v(m+1),use(n);
  vector<P> lv(m);
  for(int i=0;i<n;i++){
    cin >> w[i];
    maxw = max(maxw,w[i]);
    use[i] = i;
  }
  for(int i=0;i<m;i++){
    cin >> lv[i].first >> lv[i].second;
  }
  sort(lv.begin(),lv.end());
  for(int i=m-1;i>=0;i--){
    l[i+1] = lv[i].first;
    v[i] = lv[i].second;
    if(i!=m-1){
      v[i] = min(v[i],v[i+1]);
    }
  }
  l[0] = 0;
  v[m] = INF;
  if(maxw > v[0]){
    cout << -1 << endl;
    return 0;
  }
  ll ans = INF;
  do{
    vector<ll> d(n,0);
    for(int i=1;i<n;i++){
      ll sum = w[use[i]];
      for(int j=i-1;j>=0;j--){
        sum += w[use[j]];
        auto itr = lower_bound(v.begin(),v.end(),sum);
        ll idx = distance(v.begin(),itr);
        d[i] = max(d[i],d[j]+l[idx]);
      }
    }
    ans = min(ans,d[n-1]);
  }while(next_permutation(use.begin(),use.end()));
  cout << ans << endl;
}

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <cmath>
#include <climits>
#include <iomanip>
#include <queue>
#include <stack>
#include <ctype.h>

using namespace std;

typedef long long ll;

const int INF = (1<<30)-1;
const ll LINF = 1e18;

#define rep(i, n) for (int i = 0; i < n; i++)

template<class T>
bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;}

template<class T>
bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;}

vector<vector<ll> > memo;
map<ll,ll> mp;
set<pair<ll,ll> > st;

ll func(int i, int j, vector<ll> &s, set<pair<ll,ll> > &st) {
    if (memo[i][j] != -1)
        return memo[i][j];
    auto ite = st.lower_bound(make_pair(s[j+1]-s[i], 0));
    ite--;
    ll res = mp[ite->first];
    for (int k = i+1; k < j; k++)
        chmax(res, func(i, k, s, st)+func(k, j, s, st));
    return memo[i][j] = res;
}

int main() {
    int n, m;   cin >> n >> m;
    vector<ll> w(n), l(m), v(m);
    ll mx = 0;
    rep(i, n) {
        cin >> w[i];
        chmax(mx, w[i]);
    }

    rep(i, m) {
        cin >> l[i] >> v[i];
        st.insert(make_pair(v[i], l[i]));
        if (mp.count(v[i]))
            mp[v[i]] = max(mp[v[i]], l[i]);
        else
            mp[v[i]] = l[i];
        if (v[i] < mx) {
            cout << -1 << endl;
            return 0;
        }
    }
    mp[0] = 0;
    auto pre = mp.begin();
    for (auto ite = mp.begin(); ite != mp.end(); ite++) {
        if (ite->first == 0)    continue;
        ite->second = max(ite->second, pre->second);
        pre++;
    }
    st.insert(make_pair(0,0));
    sort(w.begin(), w.end());
    ll ans = LINF;

    do {
        vector<ll> s(n+1, 0);
        memo.assign(n, vector<ll>(n, -1));
        rep(i, n)   s[i+1] = s[i] + w[i];
        chmin(ans, func(0, n-1, s, st));
    } while (next_permutation(w.begin(), w.end()));
    cout << ans << endl;
    return 0;
}

//小数点精度
//cout << fixed << std::setprecision(15) << y << endl;


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define vi vector<int>
const int mod = 1e9 + 7;
//const int mod = 998244353;
const int inf = 1e9;

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int m, h;
    cin >> m >> h;
    if (h % m) cout << "No\n";
    else cout << "Yes\n";
}

#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
#include <queue>
#include <math.h>
#include <bitset>
#define INF 100000000000
#define N 200000
#define ll long long
using namespace std;

int main(){
    int n,w;
    cin>>n>>w;
    int ans = n / w;
    cout<<ans<<endl;
}


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

#define cm (1 << 17)
char cn[cm], *ci = cn + cm, ct;

inline char getcha() {
	if (ci - cn == cm) {
		fread(cn, 1, cm, stdin);
		ci = cn;
	}
	return *ci++;
}

inline int getint() {
	int A = 0;
	while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
	return A;
}

static struct FastOutput {
	static constexpr int BUF_SIZE = 1 << 20;
	char buf[BUF_SIZE];
	size_t buf_pos = 0;
	static constexpr int TMP_SIZE = 1 << 20;
	char tmp[TMP_SIZE];
	FILE* out = stdout;

	inline void put_char(char c) {
		buf[buf_pos++] = c;
		if (buf_pos == BUF_SIZE) {
			fwrite(buf, 1, buf_pos, out);
			buf_pos = 0;
		}
	}

	~FastOutput() { fwrite(buf, 1, buf_pos, out); }

	inline FastOutput& operator<<(char c) {
		put_char(c);
		return *this;
	}

	inline FastOutput& operator<<(const char* s) {
		while (*s) {
			put_char(*s++);
		}
		return *this;
	}

	inline FastOutput& operator<<(const string& s) {
		for (int i = 0; i < (int)s.size(); i++) {
			put_char(s[i]);
		}
		return *this;
	}

	template <typename T>
	inline char* integer_to_string(T n) {
		// beware of TMP_SIZE
		char* p = tmp + TMP_SIZE - 1;
		if (n == 0) {
			*--p = '0';
		} else {
			bool is_negative = false;
			if (n < 0) {
				is_negative = true;
				n = -n;
			}
			while (n > 0) {
				*--p = (char)('0' + n % 10);
				n /= 10;
			}
			if (is_negative) {
				*--p = '-';
			}
		}
		return p;
	}

	template <typename T>
	inline typename enable_if<is_integral<T>::value, char*>::type stringify(T n) {
		return integer_to_string(n);
	}

#if !defined(_WIN32) || defined(_WIN64)
	inline char* stringify(__int128 n) { return integer_to_string(n); }
#endif

	template <typename T>
	inline typename enable_if<is_floating_point<T>::value, char*>::type stringify(T n) {
		sprintf(tmp, "%.17f", n);
		return tmp;
	}

	template <typename T>
	inline FastOutput& operator<<(const T& n) {
		auto p = stringify(n);
		for (; *p != 0; p++) {
			put_char(*p);
		}
		return *this;
	}
} fast_output;

#define cout fast_output

int main() {
//	cin.tie(0)->sync_with_stdio(0);
	ll n = getint();
	vector<ll> a(n), b(n);
	vector<vector<ll>> g(n);
	for (ll i = 0; i < n - 1; i++) {
		a[i] = getint() - 1;
		b[i] = getint() - 1;
		g[a[i]].push_back(b[i]);
		g[b[i]].push_back(a[i]);
	}
	vector<ll> ans(n), depth(n);
	function<void(ll, ll)> Dfs = [&](ll u, ll d) {
		depth[u] = d;
		for (ll e : g[u]) {
			if (e != d) {
				ans[e] += ans[u];
				Dfs(e, u);
			}
		}
	};
	Dfs(0, 0);
	ll q = getint();
	while (q--) {
		ll t = getint(), e = getint() - 1, x = getint();
		ll u = a[e], v = b[e];
		if (t == 2) swap(u, v);
		if (depth[u] == v) {
			ans[u] += x;
		} else {
			ans[0] += x;
			ans[v] -= x;
		}
	}
	Dfs(0, 0);
	for (ll e : ans) {
		cout << e << '\n';
	}
}

// 4-10-2020
// FUN WITH CODE -> GON
// Don't be frustrated
// codeforces
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define nes ios_base :: sync_with_stdio(0); cin.tie(0); cout<<fixed; cout.precision(20);
#define f4(i,k,n) for(int i=k;i<n;++i)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define inf LLONG_MAX
#define dbvi vector<vector<int>>
#define dbvl vector<vector<ll>>
using namespace std;
ll const mo=1e9+7;
ll mod(ll k){
	return (k+mo)%mo;
}
ll addmod(ll a,ll b){
	return mod(mod(a)+mod(b));
}
ll mulmod(ll a,ll b){
	return mod(mod(a)*mod(b));
}
ll submod(ll a,ll b){
	return mod(a%mo-b%mo+mo)%mo;
}
int cntpw(ll n){
	int cnt=0;
	while(n>0){
		++cnt;
		n/=10;
	}
	return cnt;
}
void solve(){
	int n;
	cin>>n;
	vector<int>v(n);
	for(int i=0;i<n;++i) cin>>v[i];
	set<int>se;
	int pr=0;
	for(int i=0;i<n;++i){
		if(pr!=v[i]){
			se.insert(v[i]);
			cout<<pr<<"\n";
		}
		else{
			++pr;
			while(se.count(pr)){
				++pr;
			}
			cout<<pr<<"\n";
			//se.insert(pr);
		}
	}
}
int main(){
	nes;
	int t=1;
	//cin>>t;
	for(int i=1;i<=t;++i){
		//cout<<"Case #"<<i<<": ";
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define watch(x) cout<<(#x)<<"="<<(x)<<'\n'
#define mset(d,val) memset(d,val,sizeof(d))
#define setp(x) cout<<fixed<<setprecision(x)
#define forn(i,a,b) for(int i=(a);i<(b);i++)
#define fore(i,a,b) for(int i=(a);i<=(b);i++)
#define pb push_back
#define F first
#define S second
#define pqueue priority_queue
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef long double ld;
template<typename T>
using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void amin(ll &a, ll b){ a=min(a,b); }
void amax(ll &a, ll b){ a=max(a,b); }
void YES(){cout<<"YES\n";} void NO(){cout<<"NO\n";}
void SD(int t=0){ cout<<"PASSED "<<t<<endl; }
const ll INF = ll(1e18);
const int MOD = 998244353;

const bool DEBUG = 0;
const int MAXN = 100005;

ii solve(ll n, ll m)
{
	if(n==0) return {0,1};
	if(n&1)
	{
		ii tmp=solve(n-1, m);
		ll k=tmp.F, r=tmp.S;
		return {((10*k)%m + (10*r/m)%m)%m, (10*r)%m};
	}
	else
	{
		ii tmp=solve(n/2, m);
		ll k=tmp.F, r=tmp.S;
		return {((2LL*k*r)%m + (r*r/m)%m)%m, (r*r)%m};
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);

	ll n,m; cin>>n>>m;

	cout<<solve(n,m).F<<'\n';

	return 0;
}

/* in the name of Anton */

/*
  Compete against Yourself.
  Author - Aryan (@aryanc403)
  Atcoder library - https://atcoder.github.io/ac-library/production/document_en/
*/

#ifdef ARYANC403
    #include <header.h>
#else
    #pragma GCC optimize ("Ofast")
    #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
    #pragma GCC optimize ("-ffloat-store")
    #include<bits/stdc++.h>
    #define dbg(args...) 42;
#endif

using namespace std;
#define fo(i,n)   for(i=0;i<(n);++i)
#define repA(i,j,n)   for(i=(j);i<=(n);++i)
#define repD(i,j,n)   for(i=(j);i>=(n);--i)
#define all(x) begin(x), end(x)
#define sz(x) ((lli)(x).size())
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define endl "\n"

typedef long long int lli;
typedef long double mytype;
typedef pair<lli,lli> ii;
typedef vector<ii> vii;
typedef vector<lli> vi;

const auto start_time = std::chrono::high_resolution_clock::now();
void aryanc403()
{
#ifdef ARYANC403
auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end_time-start_time;
    cerr<<"Time Taken : "<<diff.count()<<"\n";
#endif
}

const lli INF = 0xFFFFFFFFFFFFFFFL;

lli seed;
mt19937 rng(seed=chrono::steady_clock::now().time_since_epoch().count());
inline lli rnd(lli l=0,lli r=INF)
{return uniform_int_distribution<lli>(l,r)(rng);}

class CMP
{public:
bool operator()(ii a , ii b) //For min priority_queue .
{    return ! ( a.X < b.X || ( a.X==b.X && a.Y <= b.Y ));   }};

void add( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt==m.end())         m.insert({x,cnt});
    else                    jt->Y+=cnt;
}

void del( map<lli,lli> &m, lli x,lli cnt=1)
{
    auto jt=m.find(x);
    if(jt->Y<=cnt)            m.erase(jt);
    else                      jt->Y-=cnt;
}

bool cmp(const ii &a,const ii &b)
{
    return a.X<b.X||(a.X==b.X&&a.Y<b.Y);
}

const lli mod = 1000000007L;
// const lli maxN = 1000000007L;

    lli T,n,i,j,k,in,cnt,l,r,u,v,x,y;
    lli m;
    string s;
    vi a;
    //priority_queue < ii , vector < ii > , CMP > pq;// min priority_queue .

int main(void) {
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    // freopen("txt.in", "r", stdin);
    // freopen("txt.out", "w", stdout);
// cout<<std::fixed<<std::setprecision(35);
// cin>>T;while(T--)
{

    cin>>n>>m;
    vector<vi> a(n,vi(m,0));
    vector<vi> dp(n,vi(m,0));
    fo(i,n)
    {
        cin>>s;
        fo(j,m){
            if(s[j]=='+')
                a[i][j]=1;
            else
                a[i][j]=-1;
        }
    }
    dbg(a);
    repD(i,n-1,0)   repD(j,m-1,0){
        if(i==n-1&&j==m-1)
            continue;
        if(i==n-1){
            dp[i][j]=a[i][j+1]-dp[i][j+1];
            continue;
        }
        if(j==m-1){
            dp[i][j]=a[i+1][j]-dp[i+1][j];
            continue;
        }
        dp[i][j]=max(a[i+1][j]-dp[i+1][j],a[i][j+1]-dp[i][j+1]);
    }
    dbg(a);
    const lli ans=dp[0][0];
    if(ans==0){
        cout<<"Draw"<<endl;
        return 0;
    }
    if(ans>0){
        cout<<"Takahashi"<<endl;
        return 0;
    }
    if(ans<0){
        cout<<"Aoki"<<endl;
        return 0;
    }
}   aryanc403();
    return 0;
}


#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <map>
#include <vector>
#define REP_(i, a_, b_, a, b, ...) \
    for (int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(obj) obj.begin(), obj.end()

using namespace std;
using Int = int64_t;

int main() {
    Int N;
    cin >> N;
    vector<Int> vec;
    Int sum = 0;

    REP(i, N) {
        Int a, b;
        cin >> a >> b;
        vec.push_back(2 * a + b);
        sum -= a;
    }

    sort(ALL(vec));
    reverse(ALL(vec));
    REP(i, N) {
        sum += vec[i];
        if (sum > 0) {
            cout << i + 1 << endl;
            break;
        }
    }

    return 0;
}

// #include<vector>
// #include<iostream>
// #include<stdio.h>
// #include<string>
// #include<algorithm>
#include<bits/stdc++.h>
using namespace std;

int main() {
	int a,b;
	cin >> a >> b;
	if ( b >= 8 && a+b >= 15) {
		cout << 1 << endl;
		return 0;
	}
	if ( b >= 3 && a+b >= 10) {
		cout << 2 << endl;
		return 0;
	}
	if ( a+b >= 3) {
		cout << 3 << endl;
		return 0;
	}
	cout << 4 << endl;
}


#include<bits/stdc++.h>
#define            pb            push_back
#define            pl            pair<ll,ll>
#define            pll           pair<ll,pair<ll,ll>>
#define            ll            long long
#define            vl            vector<ll>
#define            fastio        ios_base::sync_with_stdio(false); cin.tie(NULL);
#define            mp            make_pair
#define            w(t)          int t;cin>>t;while(t--)
#define            inf           1e18
#define            fi(a,n)       for(ll i=a;i<n;i++)
#define            fr(i,a,b)     for(ll i=a;i>=b;i--)
#define            fj(a,n)       for(ll j=a;j<n;j++)
#define            k(a,n)        for(ll k=a;k<n;k++)
#define            endl          "\n"
#define            all(v)        (v).begin(),(v).end()
#define            set1(x)       __builtin_popcount(x)
#define            gcd(a,b)      __gcd(a,b)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
using namespace std;
const ll mod= 1000000007;
const ll N =1000005;
ll v[N];
vl m(N,1);
ll f[N];
void seive()
{
  m[1]=1;
  fi(2,N)
  {
    if(!v[i])
    {
      for(ll j=i;j<N;j+=i)
      {

        if(j%(i*i)==0)m[j]=0;
        m[j]=-m[j];
        v[j]=1;
      }
    }
  }
}
void solve()
{
  ll l,r;
  cin>>l>>r;
  fi(l,r+1)
  {
    f[i]++;
  }
  ll ans=0;
  // fi(1,10)cout<<m[i]<<endl;
  fi(1,r+1)
  {
    if(m[i]==0)continue;
    ll temp=0;
    for(ll j=i;j<r+1;j+=i)
    {
       temp+=(f[j]);
    }
    // cout<<i<<" "<<temp<<endl;
    ans+=(temp*(temp-1)/2*m[i]);
  }
  ll x=r-l+1;
  ans=((x*(x-1))/2)-ans;
  // cout<<ans<<endl;
  ll kk=0;
  fi(max(2LL,l),r+1)
  {
    for(ll j=i;j<r+1;j+=i)
    {
       if(i==j)continue;
       else kk++;
    }
  }

  cout<<2*(ans-kk)<<endl;
}
int main()
{
  ios_base::sync_with_stdio(false);
    cin.tie(NULL);
  #ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output.txt", "w", stdout);
#endif
      // w(t)
       seive();
      solve();

}

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
#define pb push_back
#define ll long long int
#define pll pair<ll,ll>
#define mp make_pair
# define fio ios_base::sync_with_stdio(false); cin.tie(NULL)

const int MAX_N = 2e6+1;

ll mod = 1e9+7;

int main()
{
    fio;

    int l,r;
    cin>>l>>r;

    ll store[r+1];
    ll ans = 0;

    for(int i=r;i>=2;i--){
        store[i]  =0;
        for(int j=i;j<=r;j+=i){
            store[i] -= store[j];
        }
        ll low = (l-1)/i;
        ll high = r/i;
        store[i] += (high-low) * (high-low);
        ans += store[i];
    }

    for(int i=max(2,l);i<=r;i++){
        ll high = r/i;
        ans -= (2*high - 1);
    }
    cout<<ans;
    return 0;
}


#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip>
#include <stdio.h>
//end of libraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (ll)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <ll,ll>
#define rep(i,n) for(ll i = 0 ; i < n ; i++)
#define drep(i,n) for(ll i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(ll i = x ; i < n ; i++)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);

const ll max_n = 300000;

using namespace std;
ll n,m,k;
ll dp[max_n];
ll mod = 998244353;

ll binpow(ll a, ll b) {
  a %= mod; ll res = 1;
  while (b) { if (b%2) res = res * a % mod; a = a * a % mod; b /= 2; }
  return res % mod;
}

ll sub(ll a , ll b){
	a%=mod; b%=mod;
	return (((a-b)%mod)+mod)%mod;
}
ll prod(ll a , ll b){
	a%=mod; b%=mod;
	return ((a*b)%mod);
}

ll add(ll a , ll b){
	a%=mod; b%=mod;
	return (a+b)%mod;
}

int main(){
fcin;

	cin >> n >> m >> k;

	dp[1]=1;

	ll s = 1;
	crep(i,2,k+1) {
		dp[i] = sub(binpow(i,n),s);
		s+=dp[i];
	}

	ll ans = 0;

	crep(i,1,k+1) {
		ans = add(prod(binpow(k-i+1,m),dp[i]),ans);
	}

	if(n == 1 or m == 1){
		if(n==1) cout << binpow(k,m) << "\n";
		else cout << binpow(k,n) << "\n";
		return 0;
	}

	cout << ans << "\n";
/*
*/

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr LL mod = 998244353;
LL power(LL a, LL r){
    LL res = 1;
    for(; r; r >>= 1, a = a * a % mod)
        if(r & 1) res = res * a % mod;
    return res;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    LL N, M, K, ans = 0;
    cin >> N >> M >> K;
    for(int i = 1; i <= K; i += 1)
        ans += (power(i, N) - power(i - 1, N) + mod) * power(K - i + 1, M) % mod;
    if(N == 1) ans = power(K, M);
    if(M == 1) ans = power(K, N);
    cout << ans % mod;
    return 0;
}


//PochurSolveKortaHobaa
#include<bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int a,b,c;
    cin>>a>>b>>c;

    if(c==0){
        while(1){
            a--;
            if(a<=0){
                    cout<<"Aoki";
                    break;
            }
            b--;
            if(b<=0){
                    cout<<"Takahashi";
                    break;
            }
        }
    }
    else{
            while(1){
                 b--;
                 if(b<=0){
                        cout<<"Takahashi";
                         break;
                 }
                a--;
                if(a <=0){
                        cout<<"Aoki";
                        break;
                }
            }
    }
    /*if(c==0){
        if(a>b)cout<<"Takahashi";
        else cout<<"Aoki";
    }
    else{
        if(a>b)cout<<"Aoki";
        else cout<<"Takahashi";
    }*/
}
/*
          -NEVER GIVE UP.
          -Recite QURAN.
          -The harder it looks the easier it'll be.
          -Remember how you started.
          -Why you doing this?                          */

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1000000000
#define endl "\n"
#define all(s) s.begin(),s.end()
#define pb push_back
#define pi 3.1415926536
#define dig(a) fixed<<setprecision(a)
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL);
const int M = 1e9+7;
const int maxx = 1e7+5;
ll power(ll a, ll n) {
ll res = 1;
while (n) {
if (n % 2) {res = (res * a) % M;}
n >>= 1; a = (a * a) % M;}
return res;
}
const int m = 105;
double dp[m][m][m];
double solve(int a, int b, int c){
	if(a>=100 || b>=100 || c>=100) return 0;
	if(dp[a][b][c]) return dp[a][b][c];
	double res = 0;
	res+=solve(a+1,b,c)*a;
	res+=solve(a,b+1,c)*b;
	res+=solve(a,b,c+1)*c;
	res = res/(a+b+c)+1;
	dp[a][b][c] = res;
	return res;
}
int main(){
	fast();
	int a,b,c;
	cin>>a>>b>>c;
	cout<<dig(9)<<solve(a,b,c);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,srt,end) for(ll i = (srt); i < (ll)(end); i++)
ll MOD=1000000007;
ll mod=998244353;
int inf=1000001000;
ll INF=1e18+5;
//cout << std::fixed << std::setprecision(15) << y << endl;

// For debug
// Ref: https://qiita.com/ysuzuki19/items/d89057d65284ba1a16ac
#define dump(var)  do{std::cerr << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cerr << e << "\n";}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << "\n";}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cerr << "\n"; for(const auto& v : vv){ view(v); } }
template<typename T> void dump_cout(const T& v) { for(long long i = 0; i < v.size(); i++) std::cout << v[i] << (i == v.size()-1 ? "\n" : " "); }

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll x;
    cin >> x;
    ll ans = (x/100+1)*100 - x;
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using lint         = long long;
constexpr lint inf = 1LL << 60;
constexpr lint mod = 1000000007;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int x;
    cin >> x;
    cout << 100 - x % 100 << endl;
    return 0;
}


#include <bits/stdc++.h>

#define se second
#define sz(x) (int)(x.size())
#define ll long long
#define rep(i,x,y) for(int i = x; i <= y; ++i)
#define repr(i,x,y) for(int i = x; i >= y; --i)
#define pb push_back
#define mp make_pair
#define fi first
#define ull unsigned ll

#define pi 3.14159265358979

using namespace std;

const int N = (int)(1e6) + 322;
const ll INF = 2e18 + 17;
const int inf = 2e9;
const int mod = 1000000007;
const double eps = 1e-9;

template<typename T>
void pop_front(vector<T>& vec)
{
    assert(!vec.empty());
    vec.erase(vec.begin());
}

int gcd(int a, int b) { return b ? gcd (b, a % b) : a; }

int nbr_bits(int a){
	return log2(a)+1;
}

int fastpow(int a, int n, int mod) {
    if(n==0)
    	return 1;
    if(n%2==0){
    	int res = fastpow(a,n/2,mod);
    	res*=res;
    	res%=mod;
    	return res;
	}
	int res = (fastpow(a,n-1,mod)*a)%mod;
	return res;

}

// ---------------------------------------------------
int p[N];
stack<string> answers;

int find(int u){
	if(p[u]==u) return u;
	return p[u] = find(p[u]);
}

bool bubleSort(vector<int> v, int n){

  int ans = 0;
  int m = (n * (n -1)) / 2;
  rep(i, 0, n-1){

    rep(j, 0, n-2){

      if(v[j] > v[j + 1]){

		  swap(v[j], v[j + 1]);
		  ans++;

		}

	 }

  }
  if(ans < m) return true;
  else return false;

}

int n;

bool isPalinadrome(string str){

	  if(str.size() == 1) return true;
	  rep(i, 0, (str.size() / 2) - 1 ) {

	    if(str[i] == str[str.size() - i - 1]) continue;
	    return false;

	  }

	  return true;

}

ll fact(ll n) {

  if(n == 0 || n == 1 ) return 1;
  else return (n * fact(n-1)) % mod;

}

signed main()
{

	ios_base::sync_with_stdio(false);

	int a, b, c;
	cin >> a >> b >> c;
	int a2 = fastpow(a, 2, mod);
	int b2 = fastpow(b, 2, mod);
	int c2 = fastpow(c, 2, mod);
	int sum = a2 + b2;
	if(sum < c2) cout << "Yes" << endl;
	else cout << "No" << endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using Graph = vector<vector<int>>;
const long long INF = 1LL << 60;
const int SINF = 1LL << 29;
const ll mod = 1000000000+7;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main() {
    // N:日, C:プライムの１日の値段
    ll N, C;
    cin >> N >> C;
    // a~b日目の終わりまでサービスを使う, プライムでないときは1日あたりc円
    vector<ll> a(N), b(N), c(N);
    rep(i, N) cin >> a[i] >> b[i] >> c[i];
    map<ll, ll> mp;
    rep(i, N) {
        mp[a[i]] += c[i];
        mp[b[i]+1] += -c[i];
    }

    vector<pair<ll, ll>> vp;
    for(auto m:mp) {
        vp.push_back(make_pair(m.first, m.second));
    }
    ll X = 0, ans = 0;

    // rep(i, vp.size()){
    //     cout << vp[i].first << " " << vp[i].second << endl;
    // }
    rep(i, vp.size()-1) {
        X += vp[i].second;
        if(X < C) ans += (vp[i+1].first - vp[i].first) * X;
        else ans += (vp[i+1].first-vp[i].first) * C;
    }
    cout << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
typedef pair<ll,ll> P;
#define mod 1000000007

ll gcd(ll x,ll y) {return y ? gcd(y,x%y) : x;}
ll lcm(ll x,ll y) {return x/gcd(x,y)*y;}

int main(){
    ll n,m;
    cin >> n >> m;
    vector<ll> a(n,0);
    rep(i,n){
        cin >> a[i];
    }

    vector<ll> kazu(n+1,0);

    rep(i,m){
        kazu[a[i]]++;
    }

    ll now;

    for(ll i=0;i<=n;i++){
        if(kazu[i]==0){
            now=i;
            break;
        }
    }

    for(ll i=0;i<n-m;i++){
        kazu[a[i]]--;
        kazu[a[i+m]]++;
        if(kazu[a[i]]==0&&i<now){
            now=i;
        }

    }

    cout << now << endl;

    return 0;
}


#include<bits/stdc++.h>
#include <stdio.h>
#include <algorithm>

#define all(x) x.begin(),x.end()
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define LL long long
#define LD long double
#define pb push_back
#define F first
#define S second

const double PI=3.1415926535897932384626433;
const int KL=1e6;
const LL MOD=1e9+7;

using namespace std;
/*
typedef complex<double> point;
#define x real()
#define y imag()
*/

LL q,x,y,z,n,m,mn=1e9,mx=-1e9,ans,k;
string t;
bool a[2*KL+10];
map <LL,LL> mp;

int main()
{
    scl(n);
    for(LL i=1;i<=n;i++){
        scl(x);
        mp[x]++;
        ans+= (i-mp[x]);
    }
    cout<<ans<<endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define mp   make_pair
#define pb   push_back
#define eb   emplace_back
#define f first
#define s second
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define int  long long
#define endl "\n"
int solve(){
	int N,tot=0,sq=0;
	cin >> N;
	vector<int> A(N),suff(N);
	for(int i=0;i<N;i++){
		cin >> A[i];
		sq += A[i]*A[i];
	}
	suff[N-1]=A[N-1];
	for(int i=N-2;i>=0;i--) suff[i]=suff[i+1]+A[i];
	int temp=0;
	for(int i=0;i<N-1;i++) temp+=A[i]*suff[i+1];
	return (N-1)*sq-2*temp;
}

int32_t main(){
	fast;
	int t;
  	//cin >> t;
	t=1;
  	while(t--){
		cout << solve() << endl;
  	}
}


#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>

using namespace std;

struct Dog {
	int pos;
	long long cute;
	int type;	//0--R, 1--G, 2--B
	bool isUsed;
};

std::vector<Dog> dog[3];
int r[3];
int n;
int totalN;
long long minDist;

void clearAll() {
	for (int i = 0 ; i < 3 ; i++) {
		for (int j = 0 ; j < dog[i].size() ; j++) {
			dog[i][j].isUsed = false;
		}
	}
}

bool compareDog(const Dog &a, const Dog &b) {
	return a.cute < b.cute;
}

long long check(int a, int b, int d) {
	std::vector<Dog> curDogs;

	for (int i = 0 ; i < dog[a].size() ; i++) {
		if (!dog[a][i].isUsed) {
			curDogs.push_back(dog[a][i]);
		}
	}
	for (int i = 0 ; i < dog[b].size() ; i++) {
		if (!dog[b][i].isUsed) {
			curDogs.push_back(dog[b][i]);
		}
	}
	sort(curDogs.begin(), curDogs.end(), compareDog);

	long long minDist = -1;
	int minPosA, minPosB;

	int startPos, endPos;
	if (d == 1) {
		startPos = 0;
		endPos = curDogs.size() - 1;
	} else {
		startPos = curDogs.size() - 2;
		endPos = -1;
	}

	for (int i = startPos ; i != endPos ; i = i + d) {
		if (curDogs[i].type != curDogs[i + 1].type) {
			long long curDist = abs(curDogs[i].cute - curDogs[i + 1].cute);
			if ((minDist == -1) || (minDist > curDist)) {
				minDist = curDist;

				if (curDogs[i].type == a) {
					minPosA = curDogs[i].pos;
					minPosB = curDogs[i + 1].pos;
				} else {
					minPosB = curDogs[i].pos;
					minPosA = curDogs[i + 1].pos;
				}
			}
		}
	}

	dog[a][minPosA].isUsed = true;
	dog[b][minPosB].isUsed = true;

	return minDist;
}

int main(void)
{
	scanf("%d", &n);
	totalN = n * 2;
	for (int i = 0 ; i < totalN ; i++) {
		long long cute;
		char ch;
		int type;
		scanf("%lld %c", &cute, &ch);

		if (ch == 'R') {
			type = 0;
		} else if (ch == 'G') {
			type = 1;
		} else if (ch == 'B') {
			type = 2;
		}

		Dog newDog;
		newDog.cute = cute;
		newDog.type = type;
		newDog.isUsed = false;
		dog[type].push_back(newDog);
	}
	for (int i = 0 ; i < 3 ; i++) {
		for (int j = 0 ; j < dog[i].size() ; j++) {
			dog[i][j].pos = j;
		}
	}

	int oddCount = 0;
	std::vector<int> oddPos;
	int evenPos;
	for (int i = 0 ; i < 3 ; i++) {
		r[i] = dog[i].size() % 2;
		if (r[i] == 1) {
			oddCount++;
			oddPos.push_back(i);
		} else {
			evenPos = i;	//如果只有一个偶数，后面要用
		}
	}

	if (oddCount == 0) {
		printf("0\n");
		return 0;
	}

	long long curMin;
	//先做下两个奇数的
	minDist = check(oddPos[0], oddPos[1], 1);
	clearAll();

	if (dog[evenPos].size() >= 2) {
		long long numA, numB;
		numA = check(oddPos[0], evenPos, 1);
		numB = check(oddPos[1], evenPos, 1);
		clearAll();
		curMin = numA + numB;
		if (minDist > curMin) minDist = curMin;

		numA = check(oddPos[1], evenPos, 1);
		numB = check(oddPos[0], evenPos, 1);
		clearAll();
		curMin = numA + numB;
		if (minDist > curMin) minDist = curMin;

		numA = check(oddPos[0], evenPos, -1);
		numB = check(oddPos[1], evenPos, -1);
		clearAll();
		curMin = numA + numB;
		if (minDist > curMin) minDist = curMin;

		numA = check(oddPos[1], evenPos, -1);
		numB = check(oddPos[0], evenPos, -1);
		clearAll();
		curMin = numA + numB;
		if (minDist > curMin) minDist = curMin;
	}

	printf("%lld\n", minDist);

	return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define rep(i,n) for(int i =0;i<(n);++i)

using namespace std;
using ll = long long;

struct DataType {
    ll val;
    char col;
};
constexpr ll ll_max {1001001001001001};

int main(int argc, char** argv) {
    int n; cin >> n;

    std::vector<vector<ll>> a(3);

    rep(i,2*n) {
        DataType tmp;
        cin >> tmp.val >> tmp.col;
        if (false);
        else if (tmp.col == 'R') a[0].push_back(tmp.val);
        else if (tmp.col == 'G') a[1].push_back(tmp.val);
        else if (tmp.col == 'B') a[2].push_back(tmp.val);
    }

    std::sort(a[0].begin(), a[0].end());
    std::sort(a[1].begin(), a[1].end());
    std::sort(a[2].begin(), a[2].end());

    int a_mod = a[0].size() % 2;
    int b_mod = a[1].size() % 2;
    int c_mod = a[2].size() % 2;
    if (a_mod == 0 && b_mod == 0 && c_mod == 0) {
        cout << "0\n";
    } else {
        int idx_1, idx_2;
        if (a_mod == 1) idx_1 = 0;
        else if (b_mod == 1) idx_1 = 1;
        if (c_mod == 1) idx_2 = 2;
        else if (b_mod == 1) idx_2 = 1;
        int idx_3 = 3 - idx_1 - idx_2;

        ll min_val {ll_max};

        rep(i, a[idx_1].size()) {
            auto it = std::lower_bound(a[idx_2].begin(), a[idx_2].end(), a[idx_1][i]);
            if (it != a[idx_2].end()) min_val = std::min(min_val, *it - a[idx_1][i]);
            if (it != a[idx_2].begin()) min_val = std::min(min_val, std::abs(*(--it) - a[idx_1][i]));
        }

        ll min_val_tmp1 {ll_max};

        rep(i, a[idx_1].size()) {
            auto it = std::lower_bound(a[idx_3].begin(), a[idx_3].end(), a[idx_1][i]);
            if (it != a[idx_3].end()) min_val_tmp1 = std::min(min_val_tmp1, *it - a[idx_1][i]);
            if (it != a[idx_3].begin()) min_val_tmp1 = std::min(min_val_tmp1, std::abs(*(--it) - a[idx_1][i]));
        }
        ll min_val_tmp2 {ll_max};
        rep(i,a[idx_2].size()) {
            auto it = std::lower_bound(a[idx_3].begin(), a[idx_3].end(), a[idx_2][i]);
            if (it != a[idx_3].end()) min_val_tmp2 = std::min(min_val_tmp2, *it - a[idx_2][i]);
            if (it != a[idx_3].begin()) min_val_tmp2 = std::min(min_val_tmp2, std::abs(*(--it) - a[idx_2][i]));
        }

        cout << std::min(min_val, min_val_tmp1 + min_val_tmp2) << '\n';
    }
}


#include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007

int main(){

  int n;
  cin >> n;
  cout << 3*5*7*11 << " ";
  n--;
  if(n){
    cout << 2*3 << " ";
    n--;
  }
  if(n){
    cout << 2*5 << " ";
    n--;
  }
  if(n){
    cout << 2*7 << " ";
    n--;
  }
  if(n){
    cout << 2*11 << " ";
    n--;
  }
  if(n){
    cout << 2*6 << " ";
    n--;
  }
  if(n){
    cout << 2*9 << " ";
    n--;
  }
  for(int i = 12;i <= 5000;i++){
    if(n == 0)break;
    if(i%3 == 0 || i%5 == 0 || i%7 == 0 || i%11 == 0){
      n--;
      cout << 2*i << " \n"[n == 0];
    }
  }

  return 0;
}

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

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int n = s.size();
  vector<char> cs(n*2+1,'0');
  int l = n-1;
  int r = n;
  int nl = 0;
  for (int i=0;i<n;i++) {
    if (s[i] == 'R') {
      nl = 1-nl;
      continue;
    }
    if (nl == 1) {
      cs[l] = s[i];
      if (cs[l] == cs[l+1] && cs[l] != '0') {
        cs[l] = '0';
        cs[l+1] = '0';
        l++;
      }
      else l--;
    }
    if (nl == 0) {
      cs[r] = s[i];
      if (cs[r] == cs[r-1] && cs[r] != '0') {
        cs[r] = '0';
        cs[r-1] = '0';
        r--;
      }
      else r++;
    }
  }
  string ans = "";
  for (int i=0;i<=2*n;i++) {
    if (cs[i] != '0') {
      ans += cs[i];
    }
  }
  if (nl == 1) reverse(ans.begin(),ans.end());
  cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
signed main()
{
  int n,sum=0;
  cin>>n;
  int arr1[n];
  int arr2[n];
  for(int i=0;i<n;i++)
  {
      cin>>arr1[i];
  }
  for(int i=0;i<n;i++)
  {
      cin>>arr2[i];
  }
  for(int i=0;i<n;i++)
  {
      sum+=arr1[i]*arr2[i];
  }
  if(sum==0)cout<<"Yes"<<endl;
  else cout<<"No"<<endl;

}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <math.h>
#include <vector>
using namespace std;
using ll = long long;
using Graph = vector<vector<int> >;
#define INF 500010

int main(){
    int n, A[INF], B[INF], C[INF];
    cin>>n;
    for(int i=1;i<=n;i++) cin>>A[i];
    for(int i=1;i<=n;i++) cin>>B[i];
    for(int i=1;i<=n;i++) cin>>C[i];

    ll ac[INF], bc[INF];
    memset(ac, 0, sizeof(ac));
    memset(bc, 0, sizeof(bc));

    for(int i=1;i<=n;i++)
        ac[A[i]]++;
    for(int i=1;i<=n;i++)
        bc[B[C[i]]]++;
    ll s=0;
    for(int i=1;i<=n;i++){
        s+=ac[i]*bc[i];
    }
    cout<<s<<endl;
    return 0;
}