#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 2000000010;
constexpr ll INF= 2000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int,int> P;

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

ll mod(ll val) {
    ll res = val % MOD;
    if(res < 0) {
        res += MOD;
    }
    return res;
}

// N^P mod M（ただしM == -1の時はmodを取らない）
template<typename T>
T RS(T N, T P, T M){
    if(P == 0) {
        return 1;
    }
    if(P < 0) {
        return 0;
    }
    if(P % 2 == 0){
        ll t = RS(N, P/2, M);
        if(M == -1) return t * t;
        return t * t % M;
    }
    if(M == -1) {
        return N * RS(N,P - 1,M);
    }
    return N * RS(N, P-1, M) % M;
}

int N;
bool flag[310][310][310];
double dp[310][310][310];

double solve(int a,int b,int c) {
    if(flag[a][b][c]) {
        return dp[a][b][c];
    }
    flag[a][b][c] = true;
    if(a == 0 && b == 0 && c == 0) {
        return dp[a][b][c] = 0;
    }
    double fans = 1 / ( 1 - (double)(N - a - b - c) / N);
    if(a > 0) {
        fans += solve(a - 1,b,c) * a / N / (1 - (double)(N - a - b - c) / N);
    }
    if(b > 0) {
        fans += solve(a + 1,b - 1,c) * b / N / (1 - (double)(N - a - b - c) / N);
    }
    if(c > 0) {
        fans += solve(a,b + 1,c - 1) * c / N / (1 - (double)(N - a - b - c) / N);
    }
    return dp[a][b][c] = fans;
}

int main() {
    cin >> N;
    int one = 0;
    int two = 0;
    int three = 0;
    for(int i = 0;i < N;i++) {
        int A;
        cin >> A;
        if(A == 1) {
            one++;
        }
        else if(A == 2) {
            two++;
        }
        else {
            three++;
        }
    }
    for(int i = 0;i < 310;i++) {
        for(int j = 0;j < 310;j++) {
            for(int k = 0;k < 310;k++) {
                flag[i][j][k] = false;
            }
        }
    }
    cout << fixed;
    cout << setprecision(12) << solve(one,two,three) << endl;
}

// @author: khangtq1
#include <bits/stdc++.h>

using namespace std;

#define fto(i, a, b) for(int i = a; i < b; ++i)
#define fdto(i, a, b) for(int i = b - 1; i >= a; --i)
#define rep(i, n) for(int i = 0; i < n; ++i)
#define fill(arr, val) memset(arr, val, sizeof(arr))
#define what_is(x) cerr << #x << " is " << x << "\n"
#define setpre(x) fixed << setprecision(x)

#define all(arr) (arr).begin(), (arr).end()
#define pb push_back
#define mp make_pair
#define pqueue priority_queue
#define unmap unordered_map //hashmap
#define unset unordered_set //hashset
#define mtset multiset
#define endl '\n'

using ll = long long;
using db = double;
using ii = pair<int, int>;
using vi = vector<int>;

const db PI = acos(-1);

#define MOD 1000000007

void solve();

/// ================================ main ==============================

int main() {
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);
	int tt =1;
	//cin >> tt;
	while(tt--) {
		solve();
	}
	return 0;
}

/// ================================ solve =============================

const int MAXN = 305;
double p[MAXN][MAXN][MAXN], ev[MAXN][MAXN][MAXN	];

void solve() {
	int n;
    scanf("%d", &n);
    vector<int> cnt(4);
    for(int i = 0; i < n; ++i) {
        int x;
        scanf("%d", &x);
        ++cnt[x];
    }
    p[cnt[1]][cnt[2]][cnt[3]] = 1;
    for(int c = n; c >= 0; --c) {
        for(int b = n; b >= 0; --b) {
            for(int a = n; a >= 0; --a) {
                if(a == 0 && b == 0 && c == 0) {
                    continue;
                }
                if(a + b + c > n) {
                    continue;
                }
                // dp[a][b][c]

                double p_waste = (double) (n - (a + b + c)) / n;
                // p_waste + p_waste^2 + ... = S
                // 1 + S = S / p_waste
                double ev_waste = p_waste / (1 - p_waste) + 1;
                ev[a][b][c] += ev_waste * p[a][b][c];

                // remove 1 -> dp[a-1][b][c]
                // we will eventually choose one of a+b+c
                if(a != 0) {
                    double p_go = (double) a / (a + b + c);
                    p[a-1][b][c] += p[a][b][c] * p_go;
                    ev[a-1][b][c] += ev[a][b][c] * p_go;
                }

                // remove 2 -> dp[a+1][b-1][c]
                if(b != 0) {
                    double p_go = (double) b / (a + b + c);
                    p[a+1][b-1][c] += p[a][b][c] * p_go;
                    ev[a+1][b-1][c] += ev[a][b][c] * p_go;
                }

                // remove 3
                if(c != 0) {
                    double p_go = (double) c / (a + b + c);
                    p[a][b+1][c-1] += p[a][b][c] * p_go;
                    ev[a][b+1][c-1] += ev[a][b][c] * p_go;
                }
            }
        }
    }
    printf("%.10lf\n", ev[0][0][0]); // + cnt[1] + 2 * cnt[2] + 3 * cnt[3]);
}

/*
==================================+
INPUT:                            |
3
1 1 1
==================================+
OUTPUT:                           |
5.5
==================================+
*/


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
//#pragma warning(disable : 4996)

#ifdef _MSC_VER
#include <intrin.h>

#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
//using namespace atcoder;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << endl
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i, v.size()) cout << v[i] << " \n"[i==(int)v.size()-1]
template <typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
void Yes(bool cond) { cout << (cond ? "Yes" : "No") << '\n'; }
void YES(bool cond) { cout << (cond ? "YES" : "NO") << '\n'; }

void solve() {
	int D, T, S; cin >> D >> T >> S;
	Yes(S*T >= D);
}

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

	//int q;
	//cin >> q;
	//while (q--)
	solve();

}

/*
  Contest 111
  B - AtCoder Beginner Contest 111
  Rakesh Kumar --> 27/09/2020
 */

#include <bits/stdc++.h>

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

    std::vector<int> table;
    for (int i = 1; i <= 9; ++i)
        table.emplace_back(i * 111);
    int n = 0;
    std::cin >> n;
    if (n % 111 == 0)
        std::cout << n << std::endl;
    else
        std::cout << table[n / 111] << std::endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define FOR(i,s,e) for((i)=(s);(i)<(int)(e);(i)++)
#define REP(i,e) FOR(i,0,e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

const int INF = 1000 * 1000 * 1000;
const int N = 100;
int n;
int c[N][N];
int d[N];
bool used[N];

int prim() {
  int i, res = 0;
  for (i=0; i<n; i++) {
    d[i] = INF;
    used[i] = false;
  }

  d[0] = 0;
  while (1) {
    int v = -1;
    for (i=0; i<n; i++)
      if (!used[i] && (v==-1 || (v>=0 && d[i]<d[v])))
        v = i;

    if (v==-1) break;
    used[v] = true;
    res += d[v];
    for (i=0; i<n; i++)
      d[i] = min(d[i], c[v][i]);
  }

  return res;
}

int main() {
  int i, j;
  scanf("%d ", &n);
  for (i=0; i<n; i++)
    for (j=0; j<n; j++) {
      scanf("%d ", &(c[i][j]));
      if (c[i][j]==-1) c[i][j] = INF;
    }
  printf("%d\n", prim());
  return 0;
}


#include<stdio.h>
int main(){
	char nama1[102], nama2[102];
	scanf("%s %s", nama1, nama2);
	printf("%s%s\n", nama2, nama1);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#include <time.h>
#include <chrono>
#include <random>
//#include <math.h>
#include <cmath>
#include <fstream>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <stdlib.h>
#include <iomanip>
#include <cstdio>
#include <cassert>
#include <bitset>

#pragma comment(linker, "/STACK:36777216")
using namespace std;

#define flu fflush(stdout)
#define gg return 0
#define mp make_pair

typedef long double ptype;

long long mod = /*27449*//*998244353*/ 1e9 + 7 /*1e9 + 9*/;
int inf = 1e9;
double eps = 1e-8;
long double pi = acosl(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ifstream in("input.txt");
ofstream out("output.txt");

void solve()
{
	long long ans = 0;
	int n;
	cin >> n;
	int mx = 0;
	for (int i = 0; i < n; i++)
	{
		int t;
		cin >> t;
		if (t < mx)
		{
			ans += mx - t;
		}
		else
		{
			mx = t;
		}
	}
	cout << ans;
}

int main()
{
	cout << setprecision(20) << fixed;
	cin.tie();
	cout.tie();
	clock_t t1 = clock();
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int t;
	//cin >> t;
	t = 1;
	while (t--) {
		solve();
		//test();
	}
	//cout << (clock() - t1) * 1.0 / CLOCKS_PER_SEC;
	gg;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
using i64 = int64_t;
using ll = long long;
int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  cin >> A.at(0);
  ll ans = 0;
  rep(i,N-1){
    cin >> A.at(i+1);
    if(A.at(i+1) < A.at(i)){
      ans += A.at(i) - A.at(i+1);
      A.at(i+1) = A.at(i);
    }
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
typedef unsigned long long ull;
#define vvi std::vector<std::vector<int> >
#define vvs std::vector<std::vector<string> >
#define MODs 1000000007;
typedef long long int ll;
using namespace std;
int main(int argc, char const *argv[]) {
  int N;
  ll ans = 0;
  string s, S, t, T="";
  std::cin >> N;
  std::cin >> s;
  S = s.substr(0, N);
  t = s.substr(N);
  for(int i=0;i<N;i++) T+=t[N-1-i];
  std::vector<int> num(26, 0);
  for(int i=0;i<N;i++) num[S[i]-'a']++, num[T[i]-'a']--;
  bool flag = true;
  for(int i=0;i<26;i++) if(num[i]!=0) flag = false;
  vvs str = vv(0, 2, "", string);
  for(int i=0;i<(1<<N);i++){
    int n = i+(1<<20), count=0;
    string ah = "", bh = "";
    while(count<N){
      if(n%2==1) ah += T[count];
      if(n%2==0) bh += T[count];
      count++;
      n/=2;
    }
    str.push_back(std::vector<string> {ah, bh});
  }
  sort(str.begin(), str.end());
  for(int i=0;i<(1<<N);i++){
    int n = i + (1<<20), count=0;
    string ah="", bh="";
    while(count<N){
      if(n%2==1) ah += S[count];
      if(n%2==0) bh += S[count];
      count++;
      n/=2;
    }
    int k = upper_bound(str.begin(), str.end(), std::vector<string> {ah,bh}) - lower_bound(str.begin(), str.end(), std::vector<string> {ah, bh});
    ans += k;
  }
  std::cout << (!flag?0:ans) << '\n';
  return 0;
}

#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using P = pair<ll,ll>;
using um =unordered_map<pair<string,string>,ll>;
#define fl cout<<flush;
#define endl '\n'
template <typename T> inline void prt(T v){cout<<v<<'\n';}
template <typename T> inline bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template <typename T> inline bool chmin(T &a, const T &b){if (a>b){a=b;return 1;}return 0;}
const ll INF=1LL<<60;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ld pi=3.14159265358979323846;
const ld eps=1e-10;

#define debug(v) cout<<#v<<": ";prt(v);
template <typename A,typename B>
inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";}
template <typename A,typename B,typename C>
inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";}
inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';}
template <typename T>
inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}

signed main(void){
    cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    string S;
    cin >> S;
    reverse(S.begin()+N, S.end());
    unordered_map<string,unordered_map<string,ll>> M;
    for(ll i=0;i<(1LL<<N);++i){
        string s="";
        string t="";
        for(ll j=0;j<N;++j){
            if(i&(1LL<<j))s+=S[j];
            else t+=S[j];
        }
        M[s][t]++;
    }
    S=S.substr(N,N);
    ll ans=0;
    for(ll i=0;i<(1LL<<N);++i){
        string s="";
        string t="";
        for(ll j=0;j<N;++j){
            if(i&(1LL<<j))s+=S[j];
            else t+=S[j];
        }
        if(M[s][t]){ans+=M[s][t];}
    }
    prt(ans);

    return 0;
}


#include<iostream>
#include<vector>
#include<queue>

using namespace std;

struct pos{
    int x;
    int y;
};
pos dir[] = {
    {1, 0},
    {-1, 0},
    {0, 1},
    {0, -1}
};

int main(){
    int H, W;
    int depth_max = 0;
    vector<vector<int>> depth;
    queue<pos> dest;
    cin >> H >> W;
    depth.resize(H, vector<int>(W, -1));
    for (int i = 0; i < H; i++){
        string str;
        cin >> str;
        for (int j = 0; j < W; j++){
            char c = str[j];
            if(c == '#'){
                dest.push({i, j});
                depth[i][j] = 0;
            }
        }
    }

    while(dest.size() != 0){
        pos now = dest.front();
        dest.pop();
        for (int i = 0; i < 4; i++){
            pos next = {now.x + dir[i].x, now.y + dir[i].y};
            if(next.x < 0 || H <= next.x || next.y < 0 || W <= next.y){
                continue;
            }
            if(depth[next.x][next.y] != -1){
                if(depth[next.x][next.y] > depth[now.x][now.y] + 1){
                    depth[next.x][next.y] = depth[now.x][now.y] + 1;
                    dest.push(next);
                }
                continue;
            }
            depth[next.x][next.y] = depth[now.x][now.y] + 1;
            dest.push(next);
        }
    }

    for (int i = 0; i < H; i++){
        for (int j = 0; j < W; j++){
            if(depth[i][j] > depth_max){
                depth_max = depth[i][j];
            }
        }
    }
    cout << depth_max << endl;
}

//https://codeforces.com/blog/entry/64250?#comment-576206

#include<bits/stdc++.h>
#define ll double

ll dp[301][301][301];
ll solve(int one, int two, int three, int& n)
{
    if(one < 0 || two < 0 || three < 0)
        return 0;
    if(three == 0 && two == 0 && one == 0)
        return 0;

    if(dp[one][two][three] > 0)
        return dp[one][two][three];

    int remaining = one + two + three;
    ll exp_val = n + one * solve(one - 1, two, three, n)
                   + two * solve(one + 1,two - 1, three, n)
                   + three * solve(one, two + 1, three - 1, n);

    return dp[one][two][three] = exp_val / remaining;
}

int main()
{
    using namespace std;
    int n;

    cin >> n;

    int one = 0, two = 0, three = 0;

    for(int i = 0; i < n; i++) {
      int x;
      cin >> x;
      if(x == 1) one++;
      else if(x == 2) two++;
      else three++;
    }

    memset(dp, -1, sizeof(dp));

    cout << fixed << setprecision(9) << solve(one, two, three, n);

    return 0;
}


#include <stdio.h>
#include<bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define mx 1000008

#define pii pair<int,int>
#define pi acos(-1.0)
#define pb push_back
#define ss second
#define ff first
typedef long long int ll;
///adjacent move
int fx[4]={0,+1,0,-1};
int fy[4]={+1,0,-1,0};
///knight
ll prime[mx];
void seive()
{
    for(int i=2;i<mx;i+=2) prime[i]=2;
    for(int i=3;i<mx;i+=2) prime[i]=i;
    ll sq=sqrt(mx);
    for(int i=3;i<=sq;i+=2){
        if(prime[i]==i){
            for(int j=i*i;j<mx;j+=i){
                if(prime[j]==j) prime[j]=i;
            }
        }
    }
}
int a[1000004];
ll f[2000004];
ll po[2000004];
int main()
{
    //seive();
    int t=1;
    //cin>>t;

    int c=0;
    vector<ll>vec;
    for(int i=2;i<mx;i++){
        if(prime[i]==i){
            vec.push_back(i);
        }
    }
    while(t--){
        ll n,m;

        cin>>n;

        ll a[n*2+7];
        for(int i=1;i<=2*n;i++) cin>>a[i];
        ll sum=0;
        sort(a+1,a+2*n+1);
        for(int i=1;i<=2*n;i++){
            if(i&1) sum+=a[i];
        }
        cout<<sum<<endl;
    }
}

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

int main() {
    int N;
    cin >> N;

    set<ll> numbers;
    map<ll, ll> map_numbers;
    REP(i, N) {
        ll tmp;
        cin >> tmp;
        numbers.insert(tmp);
        map_numbers[tmp]++;
    }

    ll ans = 0;
    FORR(t, 37, 1) {
        ll x = pow(2, t-1);
        ll y = pow(2, t);

        if(numbers.size() == 0) break;
        ll max = *numbers.rbegin();
        if(max < x) continue;

        set<ll> to_remove;
        for(auto a = numbers.rbegin(); a != numbers.rend(); ++a) {
            if(map_numbers[*a]==0) continue;
            if(*a < x) break;
            if(map_numbers[y - *a]>0 && *a != y - *a) {
                ll num_pair = min(map_numbers[*a], map_numbers[y-*a]);
                ans+=num_pair;
                map_numbers[y-*a]-=num_pair;
                map_numbers[*a]-=num_pair;
                if(map_numbers[*a] == 0)
                    to_remove.insert(*a);
                if(map_numbers[y-*a] == 0)
                    to_remove.insert(y-*a);
            } else if(map_numbers[y - *a]>=2 && *a == y - *a) {
                ll num_pair = map_numbers[*a] / 2;
                ans+=num_pair;
                map_numbers[*a]-=num_pair;
                if(map_numbers[*a] == 0)
                    to_remove.insert(*a);
            } else {
                map_numbers[*a] = 0;
                to_remove.insert(*a);
            }
        }

        for(ll r: to_remove) {
            numbers.erase(numbers.find(r));
        }
    }

    cout << ans << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 100100;
#define ll long long
#define fr(i,a,n) for (int i=a;i<n;i++)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vipb(v) int x;cin>>x;v.pb(x);
#define vllpb(v) ll x;cin>>x;v.pb(x);
#define in(x) cin>>x;
#define in2(x,y) cin >> x >> y;
#define in3(x,y,z) cin>>x>>y>>z;
#define in4(a,x,y,z) cin>>a>>x>>y>>z;
#define in5(a,b,x,y,z) cin>>a>>b>>x>>y>>z;

typedef vector<int> VI;
typedef vector<ll> VL;

string s;
ll n, m, k;

int main() {
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		if (i + k -1<= n)
			m++;
	}
	cout << m << endl;
	return 0;
}

#include<iostream>
int main(){
int n,k;
std::cin >> n >> k;
std::cout << n-k+1;
return 0;
}


#include <stdio.h>

int main() {
    int A, B, pos = 0;
    char S[20];
    scanf("%d %d %s", &A, &B, S);
    for (int i = 0; S[i] != '\0'; i++) {
        if (S[i] == '-') {
            pos += i;
        }
    }
    pos == A ? puts("Yes") : puts("No");
    return 0;
}

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

int main(){
    string s;
    int a,b;
    cin>>a>>b>>s;
    bool flag=true;
    for(int i=0;i<a;i++){
        int num=s[i]-'0';
        if(num<0||num>9)flag=false;
    }
    if(s[a]!='-')flag=false;
    for(int i=a+1;i<a+b+1;i++){
        int num=s[i]-'0';
        if(num<0||num>9)flag=false;
    }
    if(flag)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvi vector<vector<int> >
#define vvl vector<vector<ll> >
#define vll vector<ll>
#define rep(c, a, b) for(ll c=a;c<b;c++)
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;
int main(int argc, char const *argv[]) {
  string s;std::cin >> s;
  ll n;std::cin >> n;
  for(int i=0;i<s.size();i+=n) std::cout << s[i];
  std::cout << '\n';
}

#include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
#define ll long long
#define rep(i,n) for (int i = 0; i < n; i++)
#define rrep(i,n) for (int i = n-1; i >= 0; i--)
#define MOD (1000000007)
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vvi vector<vi>
#define vvl vector<vl>
#define pii pair<int, int>
#define pli pair<ll, int>a
#define pb push_back
#define sz(x) int(x.size())

#define mp make_pair
#define all(a) (a).begin(),(a).end()

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

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

const ll INF = 1LL << 60;

const int dh[4] = {1,0,-1,0};
const int dw[4] = {0,1,0,-1};

class UnionFind{
  public:
  vi Parent;
  UnionFind(int n){
    Parent = vi(n,-1);
  }

  int root(int a){
    if(Parent[a] < 0) return a;
    else return Parent[a] = root(Parent[a]);
  }

  int size(int a){
    return -Parent[root(a)];
  }

  bool merge(int a, int b){
    a = root(a);
    b = root(b);
    if(a == b) return false;

    if(size(a) < size(b)) swap(a,b);
    Parent[a] += Parent[b];
    Parent[b] = a;

    return true;
  }
};

class Factrial{
  public:
  vl Fac;
  Factrial(int MAX){
    Fac = vl(MAX+1);
    rep(i,MAX) {
      if(i == 0) Fac[i+1] = 1;
      else Fac[i+1] = ((i+1)*Fac[i])%MOD;
    }
  }
};

int main(){
  string s; int w; cin >> s >> w;
  string ans;
  for(int i = 0; i < sz(s); i += w) {
    ans += s[i];
  }
  cout << ans << endl;
}


#include <iostream>
#include<string>
#include<map>
using namespace std;

int main(){
  string s; cin >> s;
  int check[26] = {};
  for(int i = 0; i < s.size(); i++){
    int t = s[i]-'a';
    check[t] = 1;
  }
  for(int i = 0; i < 26; i++){
    if(check[i] == 0) {
      char ans = 'a' + i;
      cout << ans << endl;
      return 0;
    }
  }
  cout << "None" << endl;
  return 0;
}

#include <iostream>

using namespace std;

int harshad(int n)  {
    int res = 0;
    while (n > 0)   {
        res += n % 10;
        n /= 10;
    }
    return res;
}

int main()  {
    int n;
    cin >> n;
    int sum_of_digits = harshad(n);
    if (n % sum_of_digits == 0) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}


#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <cstring>
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <numeric>
#include <float.h>
#include <math.h>
#include <sstream>
#include <iterator>
using namespace std;
using Graph = vector<vector<int>>;
// repマクロの定義
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
// allマクロの定義
#define all(v) v.begin(), v.end()
// 大きい順
#define rall(v) v.rbegin(), v.rend()
#define vi vector<int>
#define vl vector<ll>
const  long long INF = 1LL << 60;
typedef  long long ll;
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int ctoi(char c) {
 	switch (c) {
		case '0': return 0;
		case '1': return 1;
		case '2': return 2;
		case '3': return 3;
		case '4': return 4;
		case '5': return 5;
		case '6': return 6;
		case '7': return 7;
		case '8': return 8;
		case '9': return 9;
		default: return 0;
	}
}

bool is_prime(const unsigned n){
    switch(n){
        case 0: // fall-through
        case 1: return false;
        case 2: // fall-through
        case 3: return true;
    } // n > 3 が保証された

    if(n % 2 == 0 || n % 3 == 0) return false;
    // n は 2 と 3 のいずれの倍数でもないことが保証された
    // これより n は (6の倍数)-1 か (6の倍数)+1 である

    // 6の倍数前後の数を使って試し割りをする
    for(unsigned i = 5; i * i <= n; i += 6) {
        if(n % i     == 0) return false; // (6の倍数)-1
        if(n % (i+2) == 0) return false; // (6の倍数)+1
    }

    return true;
}

map<int,int> mp;

int primeFactorization(int a){ //素因数分解を出力するプログラム
    int i,sq;
    if(a%2==0){ //偶数の場合
        if(!mp.count(2)){
            mp[2] = 1;
        }
        else{
            mp[2] +=1;
        }
        return primeFactorization(a/2); //2で割った値で再帰
    }
    sq = sqrt(a);
    for(i=3;i<=sq;i+=2){ //3以上√a以下の奇数の場合
        if(a%i==0){
            if(!mp.count(i)){
                mp[i] = 1;
            }
            else{
                mp[i] += 1;
            }
            return primeFactorization(a/i); //割れた値で再帰
        }
    }
    //偶数でも3以上√a以下の奇数の場合でも割り切れない場合
    if(a!=1){ //aが1でないなら、a自身は素数
        if(!mp.count(a))
        mp[a] = 1;
        else{
            mp[a] += 1;
        }
    }
    return 0;
}

ll kaizyou(ll n){
    if(n == 1){
        return 1;
    }
    else{
        return (n * kaizyou(n-1)) % 1000000007;
    }
}

int main(){
int A,B,M; cin >> A >> B >> M;
vi a(A);
vi b(B);
int p,q;
p = q = 100000000;
rep(i,A){
 cin >> a[i];
 chmin(p,a[i]);
}
rep(i,B) {
    cin >> b[i];
    chmin(q,b[i]);
}
int mi = p + q;
rep(i,M){
    int x,y,c;cin >> x >> y >> c;
    x--;
    y--;
    chmin(mi,a[x]+b[y]-c);
}
cout << mi << endl;
}

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

int main(){
    int A, B, M;
    cin >> A >> B >> M;
    int a[A], b[B];
    int x[M], y[M], c[M];
    int a_min=1000000, b_min=1000000;
    rep(i,A) {
        cin >> a[i];
        if(a[i] < a_min)    a_min = a[i];
    }
    rep(i,B) {
        cin >> b[i];
        if(b[i] < b_min)    b_min = b[i];
    }
    rep(i,M) cin >> x[i] >> y[i] >> c[i];

    int res = a_min + b_min;
    int price = 0;

    for(int i=0; i<M; i++){
        price = a[x[i]-1] + b[y[i]-1] - c[i];
        if(price < res){
            res = price;
        }
    }

    cout << res << endl;
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    for(int i = 0; i < N; i++){
        cin >> A.at(i);
    }
    sort(A.begin(), A.end());
    int ans = 0;
    for(int i = 0; i < K; i++){
        ans += A.at(N - 1 - i);
    }
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

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

  int n, k;
  cin >> n >> k;

  priority_queue<int, vector<int>, greater<int>> pq;

  for (int i = 0; i < n; ++i) {
    int x; cin >> x;

    if (pq.size() < k) pq.push(x);
    else if (pq.size() >= k && x > pq.top()) {
      pq.pop();
      pq.push(x);
    }
  }

  int res = 0;
  while (!pq.empty()) {
    res += pq.top(); pq.pop();
  }

  cout << res << "\n";
}


#include<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

pair<int, i_i> tab[100];

bool xsort(pair<int, i_i> qwe, pair<int, i_i> rty){
  return qwe.X > rty.X;
}
bool ysort(pair<int, i_i> qwe, pair<int, i_i> rty){
  return qwe.Y.X < rty.Y.X;
}

signed main(void){
  int i, j;
  for(int testcase = 0;testcase >= 0;testcase++){
    int(n);
    rep(i, n)tab[i] = mp(0, mp(i, 0));
    rep(i, n * (n - 1) / 2){
      int4(a, b, c, d);
      --a; --b;
      if(c == d){
        ++tab[a].X;
        ++tab[b].X;
      }else if(c > d){
        tab[a].X += 3;
      }else{
        tab[b].X += 3;
      }
    }
    sort(tab, tab + n, xsort);
    tab[0].Y.Y = 1;
    rep1(i, n){
      if(tab[i - 1].X == tab[i].X)tab[i].Y.Y = tab[i - 1].Y.Y;
      else tab[i].Y.Y = i + 1;
    }
    sort(tab, tab + n, ysort);
    rep(i, n)pri(tab[i].Y.Y);

/*/

//*/ break;
  }
  return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<string>

using namespace std;

int main(){

	while(1){
		int n;
		cin >> n;
		if(n==0) break;
		int pre[n];
		string str;
		for(int i=0;i<n;i++){
			cin >> pre[i];
		}

		cin >> str;

		for(int i=0;i<str.size();i++){
			for(int j=pre[i%n];j>0;j--){
				str[i]--;
				if(str[i]<=('A'-1)) str[i]='z';
				if(str[i]==96 ) str[i] = 'Z';
			}
		}
		cout << str << endl;
	}
}

#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
const ll INF=1e18;
// Binary Indexed Tree (Fenwick Tree)
// https://youtu.be/lyHk98daDJo?t=7960
template<typename T>
struct BIT {
  int n;
  vector<T> d;
  BIT(int n=0):n(n),d(n+1) {}
    //末尾の要素(i&-i)をたす→一つ上の階層なので、要素の更新の時に使える
  void add(int i, T x=1) {
    for (i++; i <= n; i += i&-i) {
      d[i] += x;
    }
  }
     //末尾の要素(i&-i)を引く→一つ上の一つ左の階層なので、要素の総和をとる時に使える
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i&-i) {
      x += d[i];
    }
    return x;
  }
    T sum(int l, int r) {
        return sum(r-1) - sum(l-1);
    }
};

int main() {
  ll n,q;
  cin>>n>>q;
  BIT <ll> B(n);
  vector<ll> a(n);
  rep(i,n){
    cin>>a[i];
    B.add(i,a[i]);
  }
  rep(i,q){
    ll t1,t2,t3;
    cin>>t1>>t2>>t3;
    if(t1==0){
      B.add(t2,t3);
    }
    else{

      ll ans=B.sum(t2,t3);
      cout<<ans<<endl;
    }

  }

}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  double d[10], v1, v2;
  vector<int> result;
  while (scanf("%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,\n",
               d, d+1, d+2, d+3, d+4, d+5, d+6, d+7, d+8, d+9, &v1, &v2) != EOF) {
    double hoge = 0;
    for (int i = 0; i < 10; i++)
      hoge += d[i];
    hoge /= v1 + v2;
    hoge *= v1;
    for (int i = 0; i < 10; i++) {
      hoge -= d[i];
      if (hoge <= 0) {
        result.push_back(i+1);
        break;
      }
    }
  }

  for (int i = 0; i < result.size(); i++)
    cout << result[i] << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  vector<int> a(n+1,0);
  for(int i=1;i<=n;i++)
    cin>>a.at(i);
  vector<int> d(n+1);
  int s=0;
  for(int i=0;i<n;i++){
    d.at(i)=abs(a.at(i+1)-a.at(i));
    s+=d.at(i);
  }
  d.at(n)=abs(a.at(n));
  s+=d.at(n);
  for(int i=1;i<=n;i++){
    if(min({a.at(i-1),a.at(i),a.at((i+1)%(n+1))})==a.at(i)||max({a.at(i-1),a.at(i),a.at((i+1)%(n+1))})==a.at(i))
      cout<<s-2*min(d.at(i-1),d.at(i))<<endl;
    else
      cout<<s<<endl;
  }
}

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
const int INF = 1e9;

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

    int sum = 0;
    rep(i, n + 1) {
        d[i] = abs(a[i + 1] - a[i]);
        sum += d[i];
    }

    rep(i, n)
        sd[i] = abs(a[i + 2] - a[i]);

    rep(i, n) {
        cout << sum - d[i] - d[i + 1] + sd[i] << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    string s;
    cin >> s;
    rep(i,s.size()-1){
        if(s[i]=='A'&&s[i+1]=='C'){
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
}

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
int main()
{
    string s;
    cin >> s;
    if(s.find("AC") != string::npos)
    {
        cout << "Yes\n";
    }
    else
    {
        cout << "No\n";
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
const int maxn=(1e6)+10;
int n,m,sum,s,a[maxn],b[maxn];
char S[maxn];
int N,id[maxn],d[maxn],tot;
bool cmpa(int x,int y) { return a[x]<a[y]; }
bool cmpb(int x,int y) { return b[x]>b[y]; }
int main() {
	//freopen("1.txt","r",stdin);
	read(n);
	for (int i=1;i<=n;i++) {
		scanf("%s",S+1);
		m=strlen(S+1);
		s=0;
		for (int j=1;j<=m;j++) {
			if (S[j]=='(') s++; else s--;
			a[i]=max(a[i],-s);
		}
		sum+=s;
		s=0;
		for (int j=m;j>=1;j--) {
			if (S[j]=='(') s++; else s--;
			b[i]=max(b[i],s);
		}
		//printf("%d %d\n",a[i],b[i]);
	}
	if (sum!=0) { printf("No\n"); return 0; }

	sum=0;
	for (int i=1;i<=n;i++) if (b[i]>=a[i]) d[++tot]=i;
	sort(d+1,d+tot+1,cmpa);
	for (int i=1;i<=tot;i++) id[++N]=d[i];
	tot=0;
	for (int i=1;i<=n;i++) if (b[i]<a[i]) d[++tot]=i;
	sort(d+1,d+tot+1,cmpb);
	for (int i=1;i<=tot;i++) id[++N]=d[i];
	for (int i=1;i<=N;i++) {
		if (sum-a[id[i]]<0) { printf("No\n"); return 0; }
		sum+=b[id[i]]-a[id[i]];
	}
	printf("Yes\n");
	return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/

#include <iostream>
#include <cstdlib>

const int MAX_SIZE = 1000000;
const int MAX_M = 100 + 1;

void insertionSort(int A[], int n, int g, int &count) {
  for (int i = g; i < n; i++) {
     int v = A[i];

     int j = i -g;

     while ((j >= 0) && (A[j] > v)) {
       A[j + g] = A[j];
       j -= g;
       count = count + 1;
     }

     A[j + g] = v;
  }
}

int main() {
  int n = 0;

  std::cin >> n;

  int A[MAX_SIZE] = {0};

  for (int i = 0; i < n; i++) {
    std::cin >> A[i];
  }

  int m = 0;
  int G[MAX_M] = {0};

  G[0] = 1;
  m++;

  while (G[m] < n) {
    int g = 3 * G[m - 1] + 1;

    if (g >= n) {
      break;
    } else {
      G[m] = g;
    }

    m++;
  }

  std::cout << m << std::endl;

  int count = 0;

  for (int i = m - 1; i >= 0; i--) {
    std::cout << G[i];

    if (i > 0) {
      std::cout << " ";
    } else {
      std::cout << std::endl;
    }

    insertionSort(A, n, G[i], count);
  }

  std::cout << count << std::endl;

  for (int i = 0; i < n; i++) {
    std::cout << A[i] << std::endl;
  }

  return 0;
}


#include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <stack>

using namespace std;

#define mod 1000000007

class UF
{
private:
	vector<int> root;

public:
	UF(int n)
	{
		root = vector<int>(n + 1, -1);
	}

	int getroot(int n)
	{
		if(root[n] == -1) return n;
		else return root[n] = getroot(root[n]);
	}

	void add(int x, int y)
	{
		x = getroot(x);
		y = getroot(y);
		if(x == y) return;
		if(x > y) swap(x, y);
		root[y] = x;
		return;
	}
};

int main()
{
	int n;
	cin >> n;
	priority_queue<pair<int, pair<int, int> > > qu;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			int w;
			cin >> w;
			if(w >= 0){
				qu.push(make_pair(-w, make_pair(i, j)));
			}
		}
	}
	UF uf(n);
	int ans = 0;
	while(!qu.empty()){
		int cost = -(qu.top()).first;
		int x = ((qu.top()).second).first;
		int y = ((qu.top()).second).second;
		qu.pop();
		if(uf.getroot(x) == uf.getroot(y)) continue;
		ans += cost;
		uf.add(x, y);
	}
	cout << ans << endl;
	return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int n; // number of vertices
int P[20200], A[20200], B[20200], C[20200];
pair<int, int> D[20200]; // Weights and coordinates

int root(int a) { // find the root of a
  if (P[a] == a) return a; // a is the root
  return  (P[a] = root(P[a])); // find root of a, make it a's parent
}

bool is_same_set(int a, int b) { // a and b are in the same group?
  return root(a) == root(b);
}

void unite(int a, int b) { // put a and b in same group
  P[root(a)] = root(b);
}

int main() {
  cin >> n; // the number of vertices
  for (int i=0; i<n; ++i) {
    P[i] = i; // Initialize vertices to disjoint sets
    for (int j=0; j<n; ++j) {
      A[(n * i) + j] = i;
      B[(n * i) + j] = j;
      cin >> C[(n * i) + j];
    }
  }
  for (int i=0; i < n*n; i++) {
    D[i].first = C[i];
    D[i].second = i;
  }
  sort(D, D+n*n);
  int sum = 0;

  for (int i=0; i<n*n; ++i) {
    if (D[i].first >= 0) { // Assuming the weight is not -1
      if (!is_same_set(A[D[i].second], B[D[i].second])) {
	unite(A[D[i].second], B[D[i].second]);
	sum += D[i].first;
      }
    }
  }
  cout << sum << endl;
}


// VU TRUONG AN
#include <bits/stdc++.h>
#define F first
#define S second
#define maxn 200005
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define pi acos(-1)
#define bit(p,x) ((x>>p) & 1)
#define Task "CF"
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<pair<int,int>,int > piii;

const int base = 200003;
const long long MM = 1ll * MOD * MOD;

int n;

bool Count(int x){
    string s;
    while(x > 0){
        int pos = x % 10;
        s += pos + '0';
        x /= 10;
    }
    n = s.length();
    s = " " + s;
    for(int i = 1;i <= n / 2;++i){
        if(s[i] != s[n - i + 1]) return 0;
    }
    return 1;
}

void Solve(){
    int a,b;
    cin >> a >> b;
    int ans = 0;
    for(int i = a;i <= b;++i){
        ans += Count(i);
    }
    cout << ans;
}

void NumTime(){cerr << "Toi" << "\n";cerr << "Di" << "\n";cerr << "Chep" << "\n";cerr << "Code" << "\n";}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen(Task".inp", "r", stdin);
    freopen(Task".out", "w", stdout);
#endif
    int test = 1;
//    cin >> test;
    while(test--){
        NumTime();
        Solve();
    }
}


#include <stdio.h>

int main(){
	char name1[110];
	char name2[110];

	scanf("%s%s", &name1, &name2);
	printf("%s%s", name2, name1);
	return 0;
}

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

int main(){
  int a[N],b[N],ansa,ansb;
  for(int i=0;i<N;i++)cin>>a[i];
  for(int i=0;i<N;i++)cin>>b[i];
  sort(a,a+N,greater<int>());
  sort(b,b+N,greater<int>());
  ansa=ansb=0;
  for(int i=0;i<3;i++)ansa+=a[i];
  for(int i=0;i<3;i++)ansb+=b[i];
  cout<<ansa<<' '<<ansb<<endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;

int main(void) {
    int N;cin>>N;
    vector<int> A(N + 2);
    // departure
    A.push_back(0);
    for (int i = 1; i <= N; i++) cin>>A[i];
    // goal
    A.push_back(0);

    int SUM[N + 1];
    int sum = 0;
    for (int i = 0; i <= N; i++) {
        SUM[i] = abs(A[i + 1] - A[i]);
        sum += SUM[i];
    }
    // 結果出力
    for (int i = 1; i <= N; i++) {
        cout << sum - SUM[i - 1] - SUM[i] + abs(A[i + 1] - A[i - 1]) << endl;
    }
    return 0;
}

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

typedef long long ll;

const int MAXN = 16;
const int MAXL = (1 << 16) + 10;
const ll NEG = -1e18;

vector<int> grafo[MAXL];
ll valor[MAXL],dp[MAXL],mat[MAXN][MAXN];
int vis[MAXL],N;

void brute(int pos,int bitmask,int submask){

	if(pos == N){
		if(__builtin_popcount(submask) == 0) return;
		grafo[bitmask].push_back(submask);
		return;
	}

	brute(pos+1,bitmask,submask);
	brute(pos+1, bitmask | (1 << pos), submask );
	brute(pos+1, bitmask | (1 << pos), submask | (1 << pos) );

}

void calcula(){

	for(int bitmask = 1;bitmask < (1 << N);bitmask++){
		for(int i = 0;i<N;i++){
			if(!(bitmask & (1 << i))) continue;
			for(int j = i+1;j<N;j++){
				if(!(bitmask & (1 << j))) continue;
				valor[bitmask] += mat[i][j];
			}
		}
	}

}

ll solve(int bitmask){

	if(bitmask == 0) return 0;
	if(vis[bitmask]) return dp[bitmask];

	vis[bitmask] = 1;

	ll best = NEG;

	for(int submask : grafo[bitmask]){
		best = max(best, solve(bitmask ^ submask) + valor[submask] );
	}

	return dp[bitmask] = best;

}

int main(){

	cin >> N;
	for(int i = 0;i<N;i++){
		for(int j = 0;j<N;j++){
			cin >> mat[i][j];
		}
	}

	brute(0,0,0);
	calcula();

	cout << solve((1 << N) - 1) << endl;

	return 0;

}


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

#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define rep2(i, s, n) for(long long i=(s);i<(long long)(n);i++)
#define repi(i, n) for(int i=0;i<(int)(n);i++)
#define rep2i(i, s, n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(), v.end()

#define deg2rad(deg) (((deg)/360)*2*M_PI)
#define rad2deg(rad) (((rad)/2/M_PI)*360)

using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;

const ll INF = (1LL<<60);
const int INFi = (1<<29);

/*素数判定*/
bool is_prime(ll n){
    if(n==1) return false;
    for(ll i=2;i*i<=n;i++){
        if(n%i==0) return false;
    }
    return true;
}

/*約数列挙*/
vll enum_divisors(ll n){
    vll l;
    for(ll i=1;i*i<=n;i++){
        if(n%i==0){
            l.push_back(i);
            if(n/i != i) l.push_back(n/i);
        }
    }
   sort(all(l));
   return l;
}

/*素因数分解*/
vector<P> prime_factorize(ll n){
    vector<P> l;
    for(ll i=2;i*i<=n;i++){
        if(n%i!=0) continue;
        ll e = 0;
        while(n%i==0){
            e++;
            n /= i;
        }
        l.push_back({i, e});
    }
    if(n!=1) l.push_back({n, 1});
    return l;
}

/*最小公倍数*/
ll lcm(ll a, ll b){
    return a*b/__gcd(a,b);
}

/*最大公約数*/
ll gcd(ll a, ll b){
    return __gcd(a,b);
}

/*組み合わせ(Combination)*/
const ll CMAX = 1010000;
const ll CMOD = 1e9+7;
ll fac[CMAX], finv[CMAX], inv[CMAX];
// テーブルを作る前処理
void combinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (ll i = 2; i < CMAX; i++) {
		fac[i] = fac[i - 1] * i % CMOD;
		inv[i] = CMOD - inv[CMOD%i] * (CMOD / i) % CMOD;
		finv[i] = finv[i - 1] * inv[i] % CMOD;
	}
}
// 二項係数計算
ll comb(ll n, ll k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % CMOD) % CMOD;
}

int main(){

    ll n, q; cin >> n >> q;
    string s; cin >> s;
    vll l(q), r(q); rep(i, q) cin >> l[i] >> r[i];

    vll a(n, 0);
    a[0] = 0;

    bool b = false;
    if(s[0]=='A') b = true;

    rep2(i, 1, n){
        a[i] = a[i-1];
        if(b && s[i]=='C'){
            a[i]++;
        }

        if(s[i]=='A'){
            b = true;
        }
        else{
            b = false;
        }
    }

    rep(i, q){
        cout << a[r[i]-1]-a[l[i]-1] << endl;
    }

    return 0;
}

#include <iostream>

using namespace std;

int table[501][501];
int C[501][501];

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

  int N, M, Q;
  cin >> N >> M >> Q;

  for (int i = 0; i < M; i++) {
    int l, r;
    cin >> l >> r;
    table[l][r]++;
  }
  for (int i = 1; i <= N; i++) {
    C[i][1] = table[i][1];
    for (int j = 2; j <= N; j++) {
      C[i][j] = C[i][j - 1] + table[i][j];
    }
  }
  for (int i = 0; i < Q; i++) {
    int p, q;
    cin >> p >> q;

    int ans = 0;
    for (int i = p; i <= q; i++) {
      ans += C[i][q] - C[i][p - 1];
    }
    cout << ans << endl;
  }

  return 0;
}


#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <iostream>
#include <cstring>
#include <algorithm>
#define R register
#define IN inline
#define W while
#define ls (now << 1)
#define rs ((now << 1) | 1)
#define MX 200500
#define gc getchar()
#define ll long long
template <class T>
IN void in(T &x)
{
	x = 0; R char c = gc;
	for (; !isdigit(c); c = gc);
	for (;  isdigit(c); c = gc)
	x = (x << 1) + (x << 3) + c - 48;
}
template <class T> IN T max(T a, T b) {return a > b ? a : b;}
template <class T> IN T min(T a, T b) {return a < b ? a : b;}
template <class T> IN T abs(T a) {return a > 0 ? a : -a;}
int n, q;
struct Node
{
	ll val[3], tag;
	Node(){std::memset(val, 63, sizeof(val));}
}tree[MX << 2];
IN void pushup(R int now)
{
	tree[now].val[0] = min(tree[ls].val[0], tree[rs].val[0]);
	tree[now].val[1] = min(tree[ls].val[1], tree[rs].val[1]);
	tree[now].val[2] = min(tree[ls].val[2], tree[rs].val[2]);
}
IN void pushdown(R int now)
{
	if (tree[now].tag)
	{
		tree[ls].val[0] += tree[now].tag;
		tree[ls].val[1] += tree[now].tag;
		tree[ls].val[2] += tree[now].tag;
		tree[ls].tag	+= tree[now].tag;
		tree[rs].val[0] += tree[now].tag;
		tree[rs].val[1] += tree[now].tag;
		tree[rs].val[2] += tree[now].tag;
		tree[rs].tag	+= tree[now].tag;
		tree[now].tag = 0;
	}
}
void modify(R int now, R int lef, R int rig, R int pos, R ll v)
{
	if (lef == rig)
	{
		tree[now].val[0] = v;
		tree[now].val[1] = v - pos;
		tree[now].val[2] = v + pos;
		return;
	}
	pushdown(now);
	int mid = lef + rig >> 1;
	if (pos <= mid) modify(ls, lef, mid, pos, v);
	else modify(rs, mid + 1, rig, pos, v);
	pushup(now);
}
IN void add(R ll v)
{
	tree[1].tag += v;
	tree[1].val[0] += v;
	tree[1].val[1] += v;
	tree[1].val[2] += v;
}
ll query(R int now, R int lef, R int rig, R int lb, R int rb, R int id)
{
	if (lef >= lb && rig <= rb) return tree[now].val[id];
	pushdown(now);
	int mid = lef + rig >> 1; ll ret = LONG_LONG_MAX;
	if (lb <= mid) ret = query(ls, lef, mid, lb, rb, id);
	if (rb > mid) ret = min(ret, query(rs, mid + 1, rig, lb, rb, id));
	return ret;
}
int main(void)
{
	int a, b, last, cur, del;
	ll res1, res2, res3;
	in(n), in(q), in(a), in(b);
	in(last);
	modify(1, 1, n, b, abs(last - a));
	modify(1, 1, n, a, abs(last - b));
	W (--q)
	{
		in(cur); del = abs(last - cur);
		res1 = query(1, 1, n, cur, cur, 0) + del;
		res2 = query(1, 1, n, 1, cur, 1) + cur;
		res3 = query(1, 1, n, cur + 1, n, 2) - cur;
		res1 = min(res1, min(res2, res3));
		add(del);
		modify(1, 1, n, last, res1);
		last = cur;
	}
	printf("%lld", tree[1].val[0]);
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)
#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)
#define EREP(i,a) for(int i=start[(a)];i;i=e[i].next)
template<class T>inline void chkmax(T &a,T b){ if(a<b)a=b;}
template<class T>inline void chkmin(T &a,T b){ if(a>b)a=b;}
#define fi first
#define se second
#define mkr(a,b) make_pair(a,b)
inline int read()
{
	int sum=0,p=1;char ch=getchar();
	while(!(('0'<=ch && ch<='9') || ch=='-'))ch=getchar();
	if(ch=='-')p=-1,ch=getchar();
	while('0'<=ch && ch<='9')sum=sum*10+ch-48,ch=getchar();
	return sum*p;
}

const int maxn=2e5+20;

int n,q,sta,stb,x[maxn];
ll s[maxn];

struct Fenwick_Tree {
	int rev;
	ll s[maxn];
	Fenwick_Tree(int _rev=0){ rev=_rev;}
	inline void Clear(){ memset(s,63,sizeof(s));}

	inline void Add(int x,ll d){ for(x=rev?n-x+1:x;x<=n;x+=x&(-x))chkmin(s[x],d);}

	inline ll Sum(int x){ ll res=1ll<<60; for(x=rev?n-x+1:x;x;x-=x&(-x))chkmin(res,s[x]); return res;}
}A(0),B(1);

inline void init()
{
	n=read();q=read(); sta=read(); stb=read();
	REP(i,1,q)x[i]=read();
}

ll dp[maxn];
ll ans=1ll<<60;

inline void doing()
{
	A.Clear(); B.Clear();
	x[0]=sta;
	REP(i,2,q)s[i]=s[i-1]+abs(x[i]-x[i-1]);
	dp[0]=abs(x[1]-stb); A.Add(sta,dp[0]-sta); B.Add(sta,dp[0]+sta);
	REP(i,1,q)
	{
		dp[i]=s[i]+min(x[i+1]+A.Sum(x[i+1]) , -x[i+1] + B.Sum(x[i+1]));
		A.Add(x[i],-s[i+1]-x[i]+dp[i]); B.Add(x[i],-s[i+1]+x[i]+dp[i]);
		chkmin(ans,dp[i]+s[q]-s[i+1]);
	}
}

int main()
{
	init();
	doing();
	swap(sta,stb); doing();
	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
#define rep(i,n) for (ll i = 0; i < (ll)(n); ++i)
#define rrep(i,j,n) for(ll i = (ll)(j); i < (ll)(n); i++)
#define mrep(i,n) for (ll i = (ll)(n-1); i > 0; i--)
#define be(v) (v).begin(), (v).end()
#define scout cout << fixed << setprecision(20)
ll INF = 1LL << 60;
ll mod = 1e9 + 7;

bool isplime(int p){
	if(p==1) return false;
	for(int i=2;i<p;i++) {
		if(p%i==0) return false;
	}
	return true;
}

int main() {
	int n; cin >> n;
	for(int i=31;i<=55555;i+=30) {
		if(isplime(i)) {
			if(i!=31) cout << " ";
			cout << i;
			n--;
		}
		if(n==0) break;
	}
	cout << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //これつけるとA[N]でもいいらしい
//for文のマクロ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define big 1000000007
#define all(a) sort((a).begin(),(a).end()) //ソートのマクロ
#define Re(a) reverse((a).begin(),(a).end())
#define YN(a) if(a){cout<<"Yes"<<endl;}else cout<<"No"<<endl;//条件によってYes、Noを出力する
const int MOD=1000000007;
const double pi = 3.141592653589793;
//素数判定 O(√N)
bool isPrime(int64_t x){
    if(x<=1){
        return false;
    }
    for(int i=2 ;i*i<=x ;i++){
        if(x%i==0){
            return false;
        }
    }
    return true;
}
int main(){
    int n;
    cin>>n;
    vector<int> prime;
    set<int> s;
    vector<int> ans;
    for(int i=2;i<=55555;i++){
        if(isPrime(i)){
            prime.push_back(i);
        }
    }
    int sum=0;
    int k=0;
    //5で割って1余る素数だけ入れてけばどの5個選んでも5の倍数だね
    //天才すぎる　かっけえ・・・・・(思いつかなった)
    while(ans.size()!=n){
        if(prime[k]%5==1){
            ans.push_back(prime[k]);
        }
        k++;
    }
    rep(i,n){
        cout<<ans[i]<<" ";
    }
}


#include <bits/stdc++.h>

using namespace std;

template<typename T> void drop(const T &x){cout<<x<<'\n';exit(0);}

void milktea() {
  int d, g;
  cin >> d >> g;
  vector<int> p(d), c(d);
  for(int i = 0; i < d; i++) {
    cin >> p[i] >> c[i];
  }

  int ans = 1e9;
  for(int bit = 0; bit < (1 << d); bit++) {
    int s = 0, num = 0, m = -1;
    for(int i = 0; i < d; i++) {
      if(bit & 1 << i) {
        s += 100 * (i+1) * p[i] + c[i];
        num += p[i];
      } else {
        m = i;
      }
    }
    cout << '\n';
    if(s < g) {
      int s1 = 100 * (m+1);
      int need = (g - s + s1 - 1) / s1;
      if(need >= p[m]) continue;
      num += need;
    }
    ans = min(ans, num);
  }
  cout << ans << '\n';
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int tt=1;
  //cin >> tt;
  while(tt--) milktea();

  return 0;
}

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

int main() {
    int64_t N;
    string S;
    cin >> N >> S;

    reverse(S.begin() + N, S.end());

    map<pair<string, string>, int64_t> mp[2];
    for (int64_t i = 0; i < (1LL << N); i++) {
        for (int64_t j = 0; j < 2; j++) {
            string str1, str2;
            for (int64_t k = 0; k < N; k++) {
                if (i & (1LL << k)) {
                    str1 += S[k + N * j];
                } else {
                    str2 += S[k + N * j];
                }
            }

            mp[j][make_pair(str1, str2)]++;
        }
    }

    int64_t ans = 0;
    for (const auto& p : mp[0]) {
        ans += mp[0][p.first] * mp[1][p.first];
    }
    cout << ans << endl;
}


#include<cmath>
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
	register char ch;
	while(!isdigit(ch=getchar()));
	register int x=ch^'0';
	while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
	return x;
}
const int N=600;
int n,d1,d2,col[2][N*N];
std::vector<std::pair<int,int>> v[2];
inline int id(const int &x,const int &y) {
	return x*N+y;
}
inline bool check(const int &x) {
	return 0<=x&&x<n*2;
}
void dfs(const int &x,const int &t) {
	const int i=x/N,j=x%N;
	for(auto &d:v[t]) {
		const int nx=i+d.first,ny=j+d.second;
		if(!check(nx)||!check(ny)) continue;
		const int y=id(i+d.first,j+d.second);
		if(!col[t][y]) {
			col[t][y]=col[t][x]==1?2:1;
			dfs(y,t);
		}
	}
}
int main() {
	n=getint(),d1=getint(),d2=getint();
	for(register int i=0;i<=d1;i++) {
		const int j=sqrt(d1-i*i);
		if(i*i+j*j!=d1) continue;
		v[0].emplace_back(std::make_pair(i,j));
		v[0].emplace_back(std::make_pair(-i,j));
		v[0].emplace_back(std::make_pair(i,-j));
		v[0].emplace_back(std::make_pair(-i,-j));
	}
	for(register int i=0;i<=d2;i++) {
		const int j=sqrt(d2-i*i);
		if(i*i+j*j!=d2) continue;
		v[1].emplace_back(std::make_pair(i,j));
		v[1].emplace_back(std::make_pair(-i,j));
		v[1].emplace_back(std::make_pair(i,-j));
		v[1].emplace_back(std::make_pair(-i,-j));
	}
	for(register int i=0;i<n*2;i++) {
		for(register int j=0;j<n*2;j++) {
			if(!col[0][id(i,j)]) {
				col[0][id(i,j)]=1;
				dfs(id(i,j),0);
			}
			if(!col[1][id(i,j)]) {
				col[1][id(i,j)]=1;
				dfs(id(i,j),1);
			}
		}
	}
	for(register int i=0,cnt=0;i<n*2;i++) {
		for(register int j=0;j<n*2;j++) {
			if(col[0][id(i,j)]==1&&col[1][id(i,j)]==1) {
				printf("%d %d\n",i,j);
				if(++cnt==n*n) return 0;
			}
		}
	}
}


#include<bits/stdc++.h>
#define REP(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
const ll MOD = 1000000007;

int main() {
  int n, h, w;
  cin >> n >> h >> w;
  cout << max(n - h + 1, 1) * max(n - w + 1, 1) << endl;
  return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for(int i= 0; i < (n); i++)
using ll= long long int;
using namespace std;
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;}
ll mod= 1e9 + 7;

int main(){
  string a,b,c;
  cin >> a >>b >> c;
  int an=a.size(),bn=b.size(),cn=c.size();
  if(a[an-1]==b[0] && b[bn-1]==c[0]){cout << "YES" << endl;}else{cout << "NO" <<endl;}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> duo;

#define TT template<typename T>
TT T    sq(T x){ return x*x; }
TT int  ubnd(vector<T>& v,T x){ return upper_bound(v.begin(), v.end(), x) - v.begin(); }
TT int  lbnd(vector<T>& v,T x){ return lower_bound(v.begin(), v.end(), x) - v.begin(); }
TT void sort(T& v){ sort(v.begin(), v.end()); }
TT void revs(T& v){ reverse(v.begin(), v.end()); }
TT void uniq(T& v){ sort(v); v.erase(unique(v.begin(), v.end()), v.end()); }
TT void show(T& v,char d=' ',char e='\n'){for(int i=0;i<v.size();++i)cout<<v[i]<<((i==v.size()-1)?e:d);}
TT void inpt(vector<T>& v,int n){ v.reserve(n);for(int i=0;i<n;++i){T x; cin>>x; v.push_back(x);} }
TT T In(){ T x; cin >> x; return x; }

static inline void fast_io(){ ios::sync_with_stdio(0); cin.tie(0); }
static inline int in(){ int x; scanf("%d", &x); return x; }
static inline string instr(){ string x; cin >> x; return x; }
static const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
static const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};

int main()
{
  while (int e = in()){
    int m = 1e8;
    for (int z = 0; z <= 100; ++z){
      for (int y = 0; y <= 1000; ++y){
        int x = e - (sq(z) * z + sq(y));
        if (x < 0) continue;
        m = min(m, z + y + x);
      }
    }
    printf("%d\n", m);
  }
  return (0);
}

#include <iostream>

using namespace std;

int main() {
	int e,small ;
	while(cin >> e && e>0){
		small = e;
		for(int z = 0;  z*z*z <= e; z++){
			for(int y = 0; y*y+z*z*z <= e; y++){
				int x = e-y*y-(z*z)*z;
				small=min(small, x+y+z);
			}
		}
	cout << small << endl ;
	}
return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define inf(x) (ll)(1e##x)
using namespace std;
template<typename tn> void read(tn &a){
	tn x=0,f=1; char c=' ';
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	a=x*f;
}
int fa[101000],n,k,ans,f[101000];
vector<int> e[101000];
void dfs(int x){
	int mx=0;
	for(int v:e[x]) dfs(v),mx=max(mx,f[v]+1);
	if(x==1||fa[x]==1) return;
	if(mx==k-1) ans++,f[x]=-1;
	else f[x]=mx;
}
int main(){
	read(n);read(k);
	for(int i=1;i<=n;i++) read(fa[i]);
	if(fa[1]!=1) fa[1]=1,ans=1;
	for(int i=2;i<=n;i++) e[fa[i]].push_back(i);
	dfs(1);
	cout<<ans<<'\n';
	return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define x first
#define y second
typedef pair<int,int> pii;
const int MAXN=105;

int n,m;
int sumr[MAXN][MAXN],sumc[MAXN][MAXN],f[MAXN][MAXN][MAXN];
char s[MAXN][MAXN];
pii ed;

int main(){
    //freopen("robot.in","r",stdin);
    //freopen("robot.out","w",stdout);

    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%s",s[i]+1);
        for(int j=1;j<=m;j++){
            sumr[i][j]=sumr[i-1][j];
            sumc[i][j]=sumc[i][j-1];
            if(s[i][j]=='o') sumr[i][j]++,sumc[i][j]++;
            if(s[i][j]=='E') ed.x=i,ed.y=j;
        }
    }
    int ans=0;
    memset(f,-1,sizeof f);
    f[0][0][0]=0;
    int U=ed.x-1,D=n-ed.x,L=ed.y-1,R=m-ed.y;
    for(int l=0;l<=L;l++)
        for(int r=0;r<=R;r++)
            for(int u=0;u<=U;u++)
                for(int d=0;d<=D;d++){
                    if(f[r][u][d]==-1) continue;
                    ans=max(ans,f[r][u][d]);
                    int left=max(r+1,ed.y-l),right=min(m-l,ed.y+r);
                    int up=max(d+1,ed.x-u),down=min(n-u,ed.x+d);
                    if(left>right||up>down) continue;
                    int temp;
                    if(u<U-d){
                        temp=sumc[ed.x-u-1][right]-sumc[ed.x-u-1][left-1];
                        f[r][u+1][d]=max(f[r][u+1][d],f[r][u][d]+temp);
                    }
                    if(d<D-u){
                        temp=sumc[ed.x+d+1][right]-sumc[ed.x+d+1][left-1];
                        f[r][u][d+1]=max(f[r][u][d+1],f[r][u][d]+temp);
                    }
                    if(r<R-l){
                        temp=sumr[down][ed.y+r+1]-sumr[up-1][ed.y+r+1];
                        f[r+1][u][d]=max(f[r+1][u][d],f[r][u][d]+temp);
                    }
                    if(l<L-r){
                        temp=sumr[down][ed.y-l-1]-sumr[up-1][ed.y-l-1];
                        f[r][u][d]=max(f[r][u][d],f[r][u][d]+temp);
                    }
                }
    printf("%d",ans);

    fclose(stdin);
    fclose(stdout);
}


#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
#include <stack>
#include <assert.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(ll i = 0; i < n; i++)

int main(){
    int x;
    cin >> x;
    cout << x * x * x << endl;
}

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main(int argc, char* argv[])
{
    int n = 0, n2 = 0;
    cin >> n;
    //n = atoi(argv[1]);
    if(n < 1 || n > 100) {
        return 1;
    }
    n2 = pow(n, 3.0);
    cout << n2 << endl;
}


#include <iostream>
#include <string>

using namespace std;

int main() {

    string str;
    int n;

    cin >> str;
    cin >> n;

    while (n-- > 0) {

        string q, p;
        int a, b, size;

        cin >> q;
        cin >> a >> b;
        size = b - a + 1;

        if (q == "print") {
            cout << str.substr(a, size) << endl;
        } else if (q == "reverse") {
            string sub = str.substr(a, size);
            for (int i = a, i_ = sub.length() - 1; i_ < sub.length(); ++i, --i_) {
                str[i] = sub[i_];
            }
        } else {
            cin >> p;
            str.replace(a, size, p);
        }
    }
}

#include<iostream>
using namespace std;

string rep(string s,int a,int b,string p);
string rev(string s,int a,int b);
void pri(string s,int a,int b);

int main(){
        string code,k,p;
        int n,a,b;
        string haki;
        getline(cin,code);
        cin >> n;
        getline(cin, haki);
        for(int i = 0; i < n; i++){
                cin >> k;
                if(k == "replace"){
                        cin >> a >> b >> p;
                        code = rep(code ,a,b,p);
                }else if(k == "reverse"){
                        cin >> a >> b;
                        code =rev(code , a,b);
                }else if(k == "print"){
                        cin >> a >> b;
                        pri(code, a,b);
                }
                getline(cin,haki);
        }
}

string rep(string s,int a, int b, string p){
        for(int i = a; i <=b; i++){
                s[i] = p[i-a];
        }
        return s;
}string rev(string s, int a, int b){
        string p = s;
        for(int i = a; i <= b; i++){
                s[i] = p[b-i+a];
        }
        return s;
}
void pri(string s, int a, int b){
        for(int i = a; i <= b;i++){
                cout <<s[i];
        }
        cout << endl;
}


//#define MYDEBUG
#include <bits/stdc++.h>

#ifdef MYDEBUG
#define dbp(x) cout<<#x<<": "<<x<<endl
#define dbp2(x,y) cout<<#x<<","<<#y<<": "<<x<<","<<y<<endl
#define dbp3(x,y,z) cout<<#x<<","<<#y<<","<<#z<<": "<<x<<","<<y<<","<<z<<endl
#define dbp4(w,x,y,z) cout<<#w<<","<<#x<<","<<#y<<","<<#z<<": "<<w<<","<<x<<","<<y<<","<<z<<endl
#define ifcin(x) std::ifstream cin(x)
#else
#define dbp(x)
#define dbp2(x,y)
#define dbp3(x,y,z)
#define dbp4(w,x,y,z)
#define ifcin(x)
#endif
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for(int i=from; i<to; ++i)
#define REP(i, from, to) for(int i=from; i<=to; ++i)
#define EPS = 1e-14;
using std::vector;
using std::cout;
using std::cin;
using std::endl;
using std::max;
using std::min;
using std::swap;
using std::string;
using std::fill;
using std::pair;
using std::sort;
using std::reverse;
using std::pair;
using std::greater;
using std::priority_queue;
using std::ostream;
typedef std::complex<int> P;

template<typename T>
ostream& operator<<(ostream& out, const vector<vector<T> >& v) {
	for (size_t i = 0; i < v.size(); ++i) {
		out << v[i] << endl;
	}
	return out;
}

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
	out << "[";
	size_t last = v.size() - 1;
	for (size_t i = 0; i < v.size(); ++i) {
		out << v[i];
		if (i != last) {
			out << ",";
		}
	}
	out << "]";
	return out;
}

const int MAX_N = 1 << 17;
const int LAZY_INIT = -1;
const int VAL_INIT = INT_MAX;
int N = 1;
int mini[MAX_N*2], lazy[MAX_N*2];

void init(int n_){
	while(N < n_){
		N *= 2;
	}
	for(int i = 0; i < N*2 - 1; ++i){
		lazy[i] = LAZY_INIT;
		mini[i] = VAL_INIT;
	}
}

void setLazy(int k, int v){
	lazy[k] = v;
	mini[k] = v;
}

void push(int k){
	if(lazy[k] == LAZY_INIT){
		return;
	}
	setLazy(k*2+1, lazy[k]);
	setLazy(k*2+2, lazy[k]);
	lazy[k] = LAZY_INIT;
}

void fix(int k){
	mini[k] = min(mini[k*2+1], mini[k*2+2]);
}

void fill(int qL, int qR, int val, int k=0, int nL=0, int nR=N){
	if(nR<=qL || qR<=nL){
		return;
	}
	if(qL<=nL && nR<=qR){
		setLazy(k, val);
		return;
	}
	push(k);
	int nM = (nL+nR)/2;
	fill(qL, qR, val, k*2+1, nL, nM);
	fill(qL, qR, val, k*2+2, nM, nR);
	fix(k);
}

int minimum(int qL, int qR, int k=0, int nL=0, int nR=N){
	if(qR<=nL || nR <=qL){
		return VAL_INIT;
	}
	if(qL<=nL && nR<=qR){
		return mini[k];
	}
	push(k);
	int nM = (nL+nR)/2;
	int vl = minimum(qL, qR, k*2+1, nL, nM);
	int vr = minimum(qL, qR, k*2+2, nM, nR);
	return min(vl, vr);
}

void print(){
	cout << "mini: ";
	rep(i, 0, N-1+N){
		cout << mini[i] << ",";
	}
	cout << endl;
	cout << "lazy: ";
	rep(i, 0, N-1+N){
		cout << lazy[i] << ",";
	}
	cout << endl;
}

int n, q;
void solve() {
	ifcin("/storage/emulated/0/AppProjects/AtCoder/jni/in.txt");
	cin >> n >> q;
	init(n);
	rep(query_index, 0, q){
		int com;
		cin >> com;
		if(com==0){
			int s,t,x;
			cin >> s >> t >> x;
			//print();
			fill(s, t+1, x);
			//dbp4(N, s,t,x);
			//print();
			//cout << endl;
		}else{
			int i;
			cin >> i;
			cout << minimum(i, i+1) << endl;
		}
	}
}

int main() {
	solve();
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
typedef pair<ll,ll> pll;
void read(int& x){ scanf("%d",&x); }
void read(ll& x){ scanf("%lld",&x); }
template<typename T,typename... Args>
void read(T& a,Args&... b){ read(a); read(b...); }
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define eb emplace_back
#define x first
#define y second

int n, m;
int x, y;

int a[110][110];

char buf[110];
void in(){
	read(n, m);
	for(int i=1; i<=n; ++i){
		scanf("%s", buf+1);
		for(int j=1; j<=m; ++j){
			a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1];
			if(buf[j] == 'o'){
				++a[i][j];
			} else if(buf[j] == 'E'){
				x=i; y=j;
			}
		}
	}
}

int dp[102][102][102][102];

int rect(int L1, int R1, int D1, int U1, int L2, int R2, int D2, int U2){
	int L=max(L1, L2);
	int R=min(R1, R2);
	int D=max(D1, D2);
	int U=min(U1, U2);
	if(L>R || D>U) return 0;
	if(0) printf("%d %d %d %d\n", L, R, D, U);
	return a[R][U]-a[L-1][U]-a[R][D-1]+a[L-1][D-1];
}

int main()
{
	in();
	for(int w=1; w<=n; ++w){
		for(int h=1; h<=m; ++h){
			for(int l=1; l+w-1<=n; ++l){
				int r=l+w-1;
				if(x<l || r<x) continue;
				for(int d=1; d+h-1<=m; ++d){
					int u=d+h-1;
					if(y<d || u<y) continue;
					int me=dp[l][r][d][u];
					auto f = [&](int A, int B, int C, int D){
						int I=l+A;
						int J=r+B;
						int K=d+C;
						int L=u+D;
						int nv = rect(1+(r-x), n-(x-l), 1+(u-y), m-(y-d),
							(A||B)?(A?I:J):(I),
							(A||B)?(A?I:J):(J),
							(C||D)?(C?K:L):(K),
							(C||D)?(C?K:L):(L)
						);
						int& T=dp[I][J][K][L];
						if(T < me+nv){
							if(0) printf("upd from %d-%d, %d-%d to df %d %d %d %d : %d+%d = %d\n",
								l,r,d,u, A,B,C,D, me, nv, me+nv);
						}
						T=max(T, me+nv);
					};
					if(1 < l) f(-1, 0, 0, 0);
					if(r < n) f(0, 1, 0, 0);
					if(1 < d) f(0, 0, -1, 0);
					if(u < m) f(0, 0, 0, 1);
				}
			}
		}
	}
	printf("%d\n", dp[1][n][1][m]);
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
using namespace std;
using ll=long long;
using pint=pair<int,int>;
#define rep(i,a,b) for(ll i=a;i<ll(b);i++)
#define repr(i,a,b) for(ll i=a;i>=ll(b);i--)
#define el '\n'
#define ALL(x) x.begin(),x.end()
#define ALLR(x) x.rbegin(),x.rend()
#define INF 1e9
#define DEBUG(x) cout<<"debug: "<<x<<endl
#define $in(v,n) ;rep(II,0,n)cin>>v[II]
inline void IN(void){return;}
template<typename F,typename... R>inline void IN(F& f, R&... r){cin>>f;IN(r...);}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}

int main(){
    int n, r;
    IN(n, r);
    if(n >= 10) cout << r << el;
    else {
        r += 100*(10-n);
        cout << r << el;
    }
    return 0;
}

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
#define SIZE 200005
#define INF 1000000005LL
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef pair <int,int> P;
vector<int> x = {1,-1,0,0};
vector<int> y = {0,0,-1,1};
int main(){
    set<ll> data;
    ll n;
    cin >> n;
    rep(i,0,n){
        ll a;
        cin >> a;
        if(data.count(a)){
            data.erase(a);
        }else{
            data.insert(a);
        }
    }
    cout<<data.size()<<endl;

  return 0;
}


#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <algorithm>

using namespace std;

int main(){
    int h,w;
    scanf("%d %d",&h, &w);
    int aaiDeffer[h][w];
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            scanf("%d", &aaiDeffer[i][j]);
        }
    }
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            int x;
            scanf("%d", &x);
            aaiDeffer[i][j] = abs(aaiDeffer[i][j] - x);  //aaiDeffer[i][j]に2整数の差を入れる
        }
    }

    vector<vector<vector<char>>> vvviCanValue(h, vector<vector<char>>(w, vector<char>(12801, 0)));  //(i,j)が0~12800の値を取れるか
    vvviCanValue[0][0][aaiDeffer[0][0]] = 1;
    for(int i = 1; i < h; i++){
        for(int j = 0; j < 12801; j++){
            if(vvviCanValue[i - 1][0][j] == 1){
                int x = aaiDeffer[i][0];
                vvviCanValue[i][0][abs(j - x)] = 1;
                vvviCanValue[i][0][j + x] = 1;
            }
        }
    }

    for(int i = 1; i < w; i++){
        for(int j = 0; j < 12801; j++){
            if(vvviCanValue[0][i - 1][j] == 1){
                int x = aaiDeffer[0][i];
                vvviCanValue[0][i][abs(j - x)] = 1;
                vvviCanValue[0][i][j + x] = 1;
            }
        }
    }

    for(int i = 1; i < h; i++){
        for(int j = 1; j < w; j++){
            for(int k = 0; k < 12801; k++){
                if(vvviCanValue[i - 1][j][k] == 1 || vvviCanValue[i][j - 1][k] == 1){
                    int x = aaiDeffer[i][j];
                    vvviCanValue[i][j][abs(k - x)] = 1;
                    vvviCanValue[i][j][k + x] = 1;
                }
            }
        }
    }
    for(int i = 0; i < 12801; i++){
        if(vvviCanValue[h-1][w-1][i] == 1){
            printf("%d",i);
            break;
        }
    }
    return 0;
}

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
  string data;
  int n;
  cin>>data>>n;
  while (n--){
    string order,p; int a,b;
    cin>>order>>a>>b;
    if (order=="replace"){
      cin>>p;
      data.replace(a,b-a+1,p);
    }
    else if(order=="reverse") reverse(begin(data)+a,begin(data)+b+1);
    else if  (order=="print") cout<<data.substr(a,b-a+1)<<endl;
  }
    return 0;
}


#include <iostream>
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <cassert>
#include <climits>
#include <string>
#include <bitset>
#include <cfloat>
#include <unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<ll>> vvll;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (ll i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define mes(a) cout << (a) << endl
#define dmes(a, b) cout << (a) << " " << (b) << endl
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
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;
}
struct io
{
    io()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
    }
};
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-19;

int main() {
    ll n, y;
    cin >> n >> y;
    y /= 1000;
    ll yc = 0;

    for (ll i = 0; i < n+1; i++) {
        for(ll j = 0; j < n+1; j++) {
            if (i+j > n) continue;
            yc = 10*i+5*j+(n-(i+j));
            if (yc == y) {
                printf("%lld %lld %lld", i, j, n-(i+j));
                re0;
            }
        }
    }

    mes("-1 -1 -1");

}

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

// syntax sugar: `for (int i = 0; i < N; ++i)`
#define rep(i, N) for (int i = 0; i < (int)(N); ++i)
#define rep2(i, startValue, N) for (int i = (int)(startValue); i < (int)(N); ++i)
// syntax sugar: `for (int i = 0; i < N; ++i)`
#define REP(type, name, beginValue, endCondValue) \
  for (type name = beginValue; name < endCondValue; ++name)
// syntax sugar: 多次元vector
#define VECTOR_DIM3(T, name, d1, d2, d3, initValue) \
    std::vector<std::vector<std::vector<T>>> name(d1, std::vector<std::vector<T>>(d2, std::vector<int>(d3, initValue)));
#define VECTOR_DIM2(T, name, d1, d2, initValue) \
    std::vector<std::vector<T>> name(d1, std::vector<T>(d2, initValue));
#define VECTOR_DIM1(T, name, d1, initValue) \
    std::vector<T> name(d1, initValue);
#define ll long long
#define ld long double

// ABC085C - Otoshidama
int main(){
  int n, y; // 1 <= n <= 2000
  cin >> n >> y;

  for (int a = 0; a <= n; ++a) {
    const int total = a * 10000;
    if (y < total) {
      break;
    }
    for (int b = 0; b <= n-a; ++b) {
      const int total2 = total + b * 5000;
      if (y < total2) {
        break;
      }
      const int c = (n-a-b);
      if (total2 + c * 1000 == y) {
        cout << a << " " << b << " " << c << endl;
        return 0;
      }
    }
  }
  cout << "-1 -1 -1" << endl;
  return 0;
}


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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf=1e9;
const int64_t inf64=1e18;
const double eps=1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
    	os << v << ",";
    }
    os << "]";
    return os;
}

template<class graph_type,class T> std::vector<T> spfa(const graph_type &graph,const int start){
    std::vector<T> dist(graph.size(),std::numeric_limits<T>::max());
    std::deque<int> dq;
    std::vector<bool> in_dq(graph.size(),false);
    dist[start]=0;
    dq.push_back(start);
    in_dq[start]=true;

    int64_t denom=1,numer=0;
    auto small_label_first=[&](){
        const int u=dq.front(),v=dq.back();
        if(dist[u]>dist[v]){
            dq.pop_back();
            dq.push_front(v);
        }
    };
    auto large_label_last=[&](){
        while(dist[dq.front()]*denom>numer){
            dq.push_back(dq.front());
            dq.pop_front();
        }
    };
    while(!dq.empty()){
        const int u=dq.front();
        dq.pop_front();
        in_dq[u]=false;
        --denom;
        numer-=dist[u];
        for(const auto &edge:graph[u]){
            const int v=edge.first;
            if(dist[v]>dist[u]+edge.second){
                dist[v]=dist[u]+edge.second;
                if(!in_dq[v]){
                    dq.push_back(v);
                    in_dq[v]=true;
                    ++denom;
                    numer+=dist[v];
                    small_label_first();
                    large_label_last();
                }
            }
        }
    }
    return dist;
}

void solve(){
    int v,e,r;
    cin >> v >> e >> r;
    vector<vector<pair<int,int>>> graph(v);
    rep(i,0,e){
        int s,t,d;
        cin >> s >> t >> d;
        graph[s].push_back(make_pair(t,d));
    }
    vector<int> dist=spfa<vector<vector<pair<int,int>>>,int>(graph,r);
    rep(i,0,v){
        if(dist[i]==std::numeric_limits<int>::max()) cout << "INF" << endl;
        else cout << dist[i] << endl;
    }
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(10);
    solve();
    return 0;
}

#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int> P;
const int INF=1e9+10;
const int N=1e5+10;

struct edge
{
    int to,cost;
    edge(){}
    edge(int t,int c):to(t),cost(c){}
};

vector<edge> G[N];
int V,E,S;
int d[N];

void init(int n)
{
    for(int i=0;i<=n;i++) G[i].clear();
}

void Dijkstra(int s)
{
    fill(d,d+V,INF);
    d[s]=0;
    priority_queue<P,vector<P>,greater<P> > q;
    q.push(P(0,s));

    while(!q.empty())
    {
        P tmp=q.top();q.pop();
        int v=tmp.second;
        if(d[v]<tmp.first) continue;
        for(int i=0;i<G[v].size();i++)
        {
            edge &e=G[v][i];
            if(d[e.to]>d[v]+e.cost&&d[v]!=INF)
            {
                d[e.to]=d[v]+e.cost;
                q.push(P(d[e.to],e.to));
            }
        }
    }
}

int main()
{
    while(scanf("%d %d %d",&V,&E,&S)!=EOF)
    {
        init(V);
        for(int i=0;i<E;i++)
        {
            int s,t,c;
            scanf("%d %d %d",&s,&t,&c);
            G[s].push_back(edge(t,c));
        }
        Dijkstra(S);
        for(int i=0;i<V;i++)
        {
            if(d[i]==INF)
            {
                puts("INF");
                continue;
            }
            printf("%d\n",d[i]);
        }
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define fi first
#define se second
const ll mod = 1e9+7;
const int N = 2e5+6;
int bit[55][55];
void add(int x, int y){
	for(int i=x;i>0;i-=(i&-i)){
		for(int j=y;j>0;j-=(j&-j)){
			bit[i][j]++;
		}
	}
}
ll get(int x, int y){
	ll ret =0;
	for(int i=x;i<55;i+=(i&-i)){
		for(int j=y;j<55;j+=(j&-j)){
			ret+=bit[i][j];
		}
	}
	return ret;
}
ll get2(ll x){
	ll ret = 1;
	while(x%2==0){
		ret++;
		x/=2;
	}
	return ret;
}
ll get5(ll x){
	ll ret = 1;
	while(x%5==0){
		ret++;
		x/=5;
	}
	return ret;
}
void solve(){
	int n;
	cin>>n;
	ll ans = 0;
	for(int i=1;i<=n;i++){
		ld inp;
		cin>>inp;
		ll trt = llround(inp*1e9);
		ll x = get2(trt);
		ll y = get5(trt);
		ll xp = max(1ll,20-x);
		ll yp = max(1ll,20-y);
		ans+=get(xp,yp);
		add(x,y);
	}
	cout<<ans<<endl;
}
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t=1;
//	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

#include <bits/stdc++.h>

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

using namespace std;

int A[100001];

int partition(int p, int r){
  int i;
  i = p-1;

  REP(j, p, r){
    if(A[j] <= A[r]){
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i+1], A[r]);

  return i+1;
}

signed main(){
  int n;
  cin >> n;
  rep(i, n){
    cin >> A[i];
  }

  int ret = partition(0, n-1);

  rep(i, n){
    if(i > 0) cout << " ";
    if(ret == i){
      cout << "[" << A[i] << "]";
    }else{
      cout << A[i];
    }
  }
  puts("");
}


#include<bits/stdc++.h>
//g++ -std=c++11

using namespace std;
typedef long long ll;

#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 //10^9+7

int main(){
	ll N;
	cin >> N;
	vector<ll> A(N);
	rep(i, N) cin >> A[i];

	map<ll, ll> M;
	rep(i, N) {
		map<ll, ll>::iterator it = M.lower_bound(A[i]);
		if(M.size() == 0 || it == M.begin()) {
			M[A[i]]++;
		} else {
			it--;
			(*it).second--;
			if((*it).second == 0) {
				M.erase(it);
			}
			M[A[i]]++;
		}
	}

	ll ans = 0;
	for(pair<ll, ll> item: M) {
		ans += item.second;
	}
	cout << ans  << endl;

}

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

int main(){
  int n;
  cin >> n;
  vector<int> a(n);
  for(int i=0;i<n;++i) cin >> a[i];

  multiset<int> ms;
  for(int i=0;i<n;++i){
    int s=a[i];
    auto itr=ms.lower_bound(s);
    if(itr!=ms.begin())ms.erase(--itr);
    ms.insert(s);
  }

  cout << ms.size() << endl;
  return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <climits>
#include <iomanip>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
int main(){
  ll n;
  cin >> n;
  vector<ll> a(n);
  map<ll,ll> mp;
  for(int i = 0;i < n;i++)cin >> a[i],mp[a[i]]++;
  sort(a.rbegin(),a.rend());
  ll res = 0;
  for(int i = 0;i < n;i++){
    if(mp[a[i]] == 0)continue;
    mp[a[i]]--;
    ll now = 2;
    while(1){
      if(now > a[i])break;
      now *= 2;
    }
    ll pan = now - a[i];
    if(mp[pan] >= 1)res++,mp[pan]--;
  }
  cout << res << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i=(int)(j);i<(int)(n);i++)
#define REP(i,j,n) for(int i=(int)(j);i<=(int)(n);i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(),(a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int,int>
#define priq priority_queue<int>
#define disup(A,key) distance(A.begin(),upper_bound(ALL(A),(int)(key)))
#define dislow(A,key) distance(A.begin(),lower_bound(ALL(A),(int)(key)))
#define tii tuple<int,int,int>
#define Priq priority_queue<int,vi,greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll<<60)
signed main(){
  int N; cin>>N;
  vi A(N);
  rep(i,0,N) cin>>A[i];
  int ans=0;
  map<int,int,greater<int>> M;
  rep(i,0,N) M[A[i]]++;
  for(auto p:M){
    int X=p.first;
    REP(i,1,60){
      int P=(1ll<<60-i);
      if(M.count(P-X)){
        if(P-X==X){
          ans+=M[X]/2;
          M[X]%=2;
        }
        else{
          int Q=min(M[P-X],M[X]);
          ans+=Q;
          M[P-X]-=Q;
          M[X]-=Q;
        }
      }
    }
  }
  cout<<ans<<endl;
  }


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

const int N = 100000;
int a[N+5], dp[N+5], cnt;

int main(){
    int n; scanf("%d", &n);
    for (int i = 1; i <= n; ++i){
        scanf("%d", a+i);
    }
    reverse(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i){
        int pos = upper_bound(dp + 1, dp + cnt + 1, a[i]) - dp;
        dp[pos] = a[i]; cnt = max(cnt, pos);
    }
    printf("%d\n", cnt);
    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for(long long int i = 0; i < n; i++)
#define _rep(i, m, n) for(long long int i = m; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll mod = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll A, ll B){
    if(B == 0)return A;
    return gcd(B, A % B);
}
ll lcm(ll A, ll B){
    return A * B / gcd(A, B);
}
using Graph = vector<vector<int>>;
Graph G;

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

    int n; cin >> n;

    vector<int> b;
    rep(i, n){
        int a; cin >> a;
        if(b.empty()){
            b.push_back(a);
            continue;
        }else{
            auto itr = lower_bound(b.begin(), b.end(), a);
            int idx = distance(b.begin(), itr);
            if(idx == 0) b.insert(b.begin(), a);
            else b[idx - 1] = a;
        }
    }
    //for(auto it : b) cout << it << endl;
    cout << b.size() << endl;
}


#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <cmath>
#include <map>
#include <iomanip>
#include <unordered_map>
#include <stack>

using namespace std;
typedef long long  ll;

const ll mod = 1000000007;

int main()
{
	string str;
	cin >> str;
	if (str.size() < 26)
	{
		map<char, ll> mp;
		for (ll i = 0; i < str.size(); i++)
		{
			mp[str[i]]++;
		}
		for (char i = 'a'; i < 'z' + 1; i++)
		{
			if (mp[i] == 0)
			{
				cout << str + i << endl;
				return 0;
			}
		}
	}
	ll index = str.size() - 1;
	for (ll i = str.size() - 1; i > 0 ; i--)
	{
		if (str[i - 1] > str[i])
		{
			index = i - 1;
		}
		else break;
	}
	if (index == 0)
	{
		cout << -1 << endl;
		return 0;
	}
	string q = str.substr(index, 1);
	for (ll i = index; i < str.size(); i++)
	{
		if (str.substr(index - 1, 1) < str.substr(i, 1))
		{
			q = min(q, str.substr(i, 1));
		}
	}
	string ans;
	if (index != 1)
	{
		ans += str.substr(0, index - 1);
	}
	cout << ans + q << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,a,b) for(int i = a ; i <= b ; ++i)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
#define fst first
#define snd second
using namespace __gnu_pbds;
using ordered_set =
 tree<int, null_type,less<int>,
 rb_tree_tag,tree_order_statistics_node_update>;
//find_by_order(k):returns iterator to kth element starting from 0
//order_of_key(k):returns count of elements strictly smaller than k
typedef long long ll;typedef pair<int, int> pii;
typedef vector<int> vi;typedef long double ld;
template<class T>
using min_heap = priority_queue<T,vector<T>,greater<T>>;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<typename T>
void remdup(vector<T>& v) {
	sort(all(v)); v.erase(unique(all(v)), v.end());
}
template<typename T> void re(vector<T> &v) {trav(i,v) cin >> i;}
void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); }
#ifdef np
#include "C:\Users\navodit\Desktop\mycodes\pr.h"
#else
#define trace(...)
#endif

#define MOD 1000000007

void solve(){
	map<string,int> week;
	week["MON"] = 1;
		week["TUE"] = 2; 	week["WED"] = 3; 	week["THU"] = 4; 	week["FRI"] = 5; 	week["SAT"] = 6;
			week["SUN"] = 0;
	string s;
	cin >> s;
	cout << 7 - week[s];
}
signed main() {
	unsyncIO();
	int tt =1 ;
	// cin >> tt;
	rep(i,0,tt) solve();

#ifdef np
	cout <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}


#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
#include <string>
#include <map>
#include <stack>
//#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define urept(soeji, start, n) for (int soeji = start; soeji < n; soeji++)
#define drept(soeji, start, n) for (int soeji = start; soeji > n; soeji--)
int main(void)
{
    int N;
    cin >> N;
    lli x;
    cin >> x;
    lli a[N];
    for (int i = 0; i < N; i++)
    {
        cin >> a[i];
    }
    sort(a, a + N);
    lli sum[N + 1];
    fill(sum, sum + N + 1, 0);
    int cnt = 0;
    for (int i = 1; i <= N; i++)
    {
        sum[i] = sum[i - 1] + a[i - 1];
    }
    for (int i = 0; i <= N; i++)
    {
        if (sum[i] > x)
        {
            cout << i - 1 << endl;
            return 0;
        }
        else if (sum[i] == x)
        {
            cout << i << endl;
            return 0;
        }
        else if (i == N)
        {
            cout << N - 1 << endl;
            return 0;
        }
    }
    return 0;
}

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

int main()
{
    int n,x,sum=0;
    cin>>n>>x;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    int ans=0;
    for(int i=0;i<n;i++)
    {
        if(sum+a[i]<=x&&i<n-1)
            {
                ++ans;
                sum+=a[i];
            }
        else if(sum+a[i]<=x && i==n-1)
        {
            if(sum+a[i]==x)
                ++ans;
        }
        else
           break;

    }
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

using int64 = long long;

int N, A[100000];

bool check()
{
  int even = 0, odd = 0;
  for(int i = 0; i < N; i++) {
    if(A[i] & 1) ++odd;
    else ++even;
  }
  if(even & 1) return (true);
  if(odd > 1) return (false);
  for(int i = 0; i < N; i++) {
    if(A[i] == 1) return (false);
    if(A[i] & 1) --A[i];
  }
  int gcd = 0;
  for(int i = 0; i < N; i++) {
    gcd = __gcd(gcd, A[i]);
  }
  for(int i = 0; i < N; i++) {
    A[i] /= gcd;
  }
  return !check();
}

int main()
{
  cin >> N;
  for(int i = 0; i < N; i++) cin >> A[i];
  if(check()) cout << "First" << endl;
  else cout << "Second" << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 100000000;
#define rep1(i, n) for (ll i=0; i<(n); i++)
#define rep2(i, k, n) for (ll i=k; i<(n); i++)
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() {
    ll n;
    cin >> n;
    vector<ll> a(n, 0);
    map<ll, ll> m;
    rep1(i, n) cin >> a[i];
    rep1(i, n){
        if (m[a[i]]!=0){
            cout << "NO" << endl;
            return 0;
        }
        m[a[i]]++;
    }
    cout << "YES" << endl;
    //printf("%.12f", ans);
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;

/***********debug tools***********/
template<class T> inline ostream& operator<<(ostream& os,vector<T> arr) {os << "["; for(int i = 0; i < (int)arr.size(); i++)os << arr[i] << (i == (int)arr.size() - 1 ? "]" : ",");os << endl;return os;}
template<typename A, typename B> ostream& operator<<(ostream& os, const pair<A,B>& p){os << "{" << p.first << "," << p.second << "}";return os; }
#define prvec(v) cerr<<#v<<": [";for(int __i = 0;__i < (int)(v).size(); __i++)cerr<<((v)[__i])<<(__i+1==(int)(v).size()?"]\n":",");
#define print(x) cerr<<#x<<": "<<(x)<<endl
/*********************************/

const int D = 30;
const int MAX_N = 100000;

int n, l;
vector<int> x;
vector<vector<int>> tor, tol;

void ftor() {
    for(int i = 0; i < n; i++) {

        int L = i + 1, R = n; //ooooxxx
        while(R - L > 1) {
            int mid = (L + R) / 2;
            (x[mid] - x[i] <= l ? L : R) = mid;
        }
        tor[0][i] = L;
    }
    tor[0][n - 1] = n - 1;

    for(int i = 0; i < D - 1; i++) {
        for(int j = 0; j < n; j++) {
            tor[i + 1][j] = tor[i][tor[i][j]];
        }
    }

}

void ftol() {
    for(int i = 1; i < n; i++) {
        int L = -1, R = i - 1;//xxxxxoooo
        while(R - L > 1) {
            int mid = (L + R) / 2;
            (x[i] - x[mid] <= l ? R : L) = mid;
        }
        tol[0][i] = R;
    }
    tol[0][0] = 0;
    for(int i = 0; i < D - 1; i++) {
        for(int j = 0; j < n; j++) {
            tol[i + 1][j] = tol[i][tol[i][j]];
        }
    }

}

int main()
{
    cin >> n;
    x.resize(n), tor.resize(D, vector<int>(n));
    tol = tor;
    for(int i = 0; i < n; i++)cin >> x[i];
    cin >> l;
    ftor();
    ftol();

    int Q; cin >> Q;
    while(Q--) {
        int a, b;
        cin >> a >> b;
        a--,b--;
        if(a > b) {

            int l = 0, r = 2 * n;//xxxxxxoooo
            while(r - l > 1) {
                int mid = (l + r) / 2;
                int pos = a;
                for(int i = 0; i < D; i++) {
                    if(mid & (1<<i))pos = tol[i][pos];
                }
                (pos <= b ? r : l) = mid;
            }
            cout << r << endl;

        } else {
            int l = 0, r = 2 * n;//xxxxxxoooo
            while(r - l > 1) {
                int mid = (l + r) / 2;
                int pos = a;
                for(int i = 0; i < D; i++) {
                    if(mid & (1<<i))pos = tor[i][pos];
                }
                (pos >= b ? r : l) = mid;
            }
            cout << r << endl;
        }

    }

}

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cctype>
#include <iostream>
#include <cstring>
#include <algorithm>
#define R register
#define IN inline
#define W while
#define ls (now << 1)
#define rs ((now << 1) | 1)
#define MX 200500
#define gc getchar()
#define ll long long
template <class T>
IN void in(T &x)
{
	x = 0; R char c = gc;
	for (; !isdigit(c); c = gc);
	for (;  isdigit(c); c = gc)
	x = (x << 1) + (x << 3) + c - 48;
}
template <class T> IN T max(T a, T b) {return a > b ? a : b;}
template <class T> IN T min(T a, T b) {return a < b ? a : b;}
template <class T> IN T abs(T a) {return a > 0 ? a : -a;}
int n, q;
struct Node
{
	ll val[3], tag;
	Node(){std::memset(val, 63, sizeof(val));}
}tree[MX << 2];
IN void pushup(R int now)
{
	tree[now].val[0] = min(tree[ls].val[0], tree[rs].val[0]);
	tree[now].val[1] = min(tree[ls].val[1], tree[rs].val[1]);
	tree[now].val[2] = min(tree[ls].val[2], tree[rs].val[2]);
}
IN void pushdown(R int now)
{
	if (tree[now].tag)
	{
		tree[ls].val[0] += tree[now].tag;
		tree[ls].val[1] += tree[now].tag;
		tree[ls].val[2] += tree[now].tag;
		tree[ls].tag	+= tree[now].tag;
		tree[rs].val[0] += tree[now].tag;
		tree[rs].val[1] += tree[now].tag;
		tree[rs].val[2] += tree[now].tag;
		tree[rs].tag	+= tree[now].tag;
		tree[now].tag = 0;
	}
}
void modify(R int now, R int lef, R int rig, R int pos, R ll v)
{
	if (lef == rig)
	{
		tree[now].val[0] = v;
		tree[now].val[1] = v - pos;
		tree[now].val[2] = v + pos;
		return;
	}
	pushdown(now);
	int mid = lef + rig >> 1;
	if (pos <= mid) modify(ls, lef, mid, pos, v);
	else modify(rs, mid + 1, rig, pos, v);
	pushup(now);
}
IN void add(R ll v)
{
	tree[1].tag += v;
	tree[1].val[0] += v;
	tree[1].val[1] += v;
	tree[1].val[2] += v;
}
ll query(R int now, R int lef, R int rig, R int lb, R int rb, R int id)
{
	if (lef >= lb && rig <= rb) return tree[now].val[id];
	pushdown(now);
	int mid = lef + rig >> 1; ll ret = LONG_LONG_MAX;
	if (lb <= mid) ret = query(ls, lef, mid, lb, rb, id);
	if (rb > mid) ret = min(ret, query(rs, mid + 1, rig, lb, rb, id));
	return ret;
}
int main(void)
{
	int a, b, last, cur, del;
	ll res1, res2, res3;
	in(n), in(q), in(a), in(b);
	in(last);
	modify(1, 1, n, b, abs(last - a));
	modify(1, 1, n, a, abs(last - b));
	W (--q)
	{
		in(cur); del = abs(last - cur);
		res1 = query(1, 1, n, cur, cur, 0) + del;
		res2 = query(1, 1, n, 1, cur, 1) + cur;
		res3 = query(1, 1, n, cur + 1, n, 2) - cur;
		res1 = min(res1, min(res2, res3));
		add(del);
		modify(1, 1, n, last, res1);
		last = cur;
	}
	printf("%lld", tree[1].val[0]);
}