#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0, length = n; i < length; i++)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ep emplace
#define epb emplace_back
#define scll static_cast<long long>
#define sz(x) static_cast<int>((x).size())
#define pfll(x) printf("%lld\n", x)
#define ci(x) cin >> x
#define ci2(x, y) cin >> x >> y
#define ci3(x, y, z) cin >> x >> y >> z
#define ci4(w, x, y, z) cin >> w >> x >> y >> z
#define co(x) cout << x << endl
#define co2(x, y) cout << x << " " << y << endl
#define co3(x, y, z) cout << x << " " << y << " " << z << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> PQG;
typedef priority_queue<P> PQP;
typedef priority_queue<P, vector<P>, greater<P>> PQPG;

const int MAX_N = 100, MOD = 1e9 + 7, INF = 1e9;
int n, m, a[4950], b[4950], c[100];
bool ans[4950], used[4950];
vector<P> to[100];

void dfs(int x) {
    for (auto &i: to[x]) {
        if (used[i.se]) continue;
        if (i.fi == a[i.se]) ans[i.se] = 1;
        used[i.se] = true;
        dfs(i.fi);
    }
}

int main() {
    ci2(n, m);
    rep(i, m) ci2(a[i], b[i]);
    rep(i, n) ci(c[i]);
    rep(i, m) {
        a[i]--; b[i]--;
        if (c[a[i]] < c[b[i]]) ans[i] = 1;
        if (c[a[i]] == c[b[i]]) {
            to[a[i]].epb(b[i], i);
            to[b[i]].epb(a[i], i);
        }
    }
    rep(i, n) dfs(i);
    rep(i, m) {
        if (ans[i]) co("<-");
        else co("->");
    }
    return 0;
}

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define INF LONG_MAX/3
//#define DIV 1000000007
//#define DIV 998244353

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

vector<ll> to[100005];

//先手取り、後てとり、turn?
tuple<ll, ll, bool> dfs(ll pos) {
	//ここに来て撮った
	ll mypoint = 0;
	ll tekipoint = 1;
	bool myturn = true;

	//P0 - P1, P0, P1
	vector<tuple<ll, ll, ll> >V;
	ll sump0 = 0;
	ll sump1 = 0;
	for(ll next: to[pos]) {
		ll p0, p1;
		bool turn;
		tie(p0, p1, turn) = dfs(next);

		if(!turn) {
			//順番は変わらず的よりたくさん取れる
			if(p0 > p1) {
				mypoint += p0;
				tekipoint += p1;
			} else {
				//turnは変更せず、敵が徳、なるべく行きたくない
				sump0 += p0;
				sump1 += p1;
			}
		} else {
			V.push_back({p0 - p1, p0, p1});
		}
	}
	sort(V.rbegin(), V.rend());

	rep(i, V.size()) {
		ll _, p0, p1;
		tie(_, p0, p1) = V[i];
		if(myturn) {
			mypoint += p0;
			tekipoint += p1;
		} else {
			mypoint += p1;
			tekipoint += p0;
		}
		myturn = !myturn;
	}
	if(myturn) {
		mypoint += sump0;
		tekipoint += sump1;
		return {mypoint, tekipoint, true};
	} else {
		mypoint += sump1;
		tekipoint += sump0;
		return {mypoint, tekipoint, false};
	}
}

int main(){
	ll N;
	cin >> N;
	rep(i, N-1) {
		ll n;
		cin >> n;
		n--;
		to[n].push_back(i+1);
	}
	ll a, b, c;
	tie(a, b, c) = dfs(0);
	cout << b << endl;
}


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

template<typename T>
struct segment_tree{
    function<T(T,T)> f = [](T a, T b){return a+b;};
    T inf=0;
    vector<T> value;
    int N;
    segment_tree(int n){
        N=1;
        while(N<n)N*=2;
        value.assign(2*N-1,inf);
    }
    segment_tree(int n, function<T(T,T)> func, T t1){
        N=1;
        while(N<n)N*=2;
        value.assign(2*N-1,t1);
        f=func;
        inf=t1;
    }
    void update(int i,T x){
        i+=N-1;
        value[i]=x;
        while(i>0){
            i=(i-1)/2;
            value[i]=f(value[i*2+1],value[i*2+2]);
        }
    }
    T query(T a,T b){
        return query(a,b,0,0,N);
    }
    T query(T a,T b, int k, int l, int r){
        if(r<=a || b<=l)return inf;
        if(a<=l && r<=b){
            return value[k];
        }else{
            ll c1=query(a,b,2*k+1,l,(l+r)/2);
            ll c2=query(a,b,2*k+2,(l+r)/2,r);
            return f(c1,c2);
        }
    }
    void print(){
        for(int i=0;i<ceil(log2(N))+1;i++){
            for(int j=0;j<(1<<i);j++){
                cout<<value[j+(1<<i)-1]<<((j==(1<<i)-1)?';':',');
            }
            cout<<endl;
        }
    }
};

int main() {
    ll h,w,m;
    cin>>h>>w>>m;
    V tatem(w,h);
    V yokom(h,w);
    VV mm(h,V());
    rep(i,m){
        ll x,y;
        cin>>y>>x;
        x--;y--;
        tatem[x]=min(tatem[x],y);
        yokom[y]=min(yokom[y],x);
        mm[y].push_back(x);
    }
    ll ans=0;
    rep(i,yokom[0]){
        ans+=tatem[i];
    }
    segment_tree<ll> sg(w);
    rep(i,tatem[0]){
        if(i==0){
            rep(j,w){
                if(j<yokom[0]){
                    sg.update(j,0);
                }else{
                    sg.update(j,1);
                }
            }
        }else{
            ans+=sg.query(0,yokom[i]);
            for(ll x:mm[i]){
                sg.update(x,1);
            }
        }
    }
    cout<<ans<<endl;
}

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int MAX = 200000;

template<class T> class segtree{
	public:
	T *tree;
	T u;
	int height,treeSize,dataSize;
	segtree(long long int length,T unit){
		long long int pre = 1;
		height=1;
		u=unit;
		while(pre<length){
			pre*=2;
			height++;
		}

		treeSize = pre*2-1;
		dataSize = pre;
		tree = new T[treeSize];

		for(int i=0;i<dataSize;i++)set(i,unit);
		construct();

	}
	void replace(long long int index, T value){
		tree[treeSize-dataSize+index]=value;
		index=treeSize-dataSize+index+1;
		while(index>0){
			index/=2;
			tree[index-1]=f(tree[index*2-1],tree[index*2]);
		}
	}
	T result(long long int l,long long int r){return result(l,r,0,1<<(height-1),0);}
	T result(long long int l,long long int r,int s,int len,long long int ind){
		if(l==r)return get(l);
		else if(l==s && r==s+len-1)return tree[ind];
		len/=2;

		T res=u;
		long long int L=s,R=s+len-1;
		if(!(r<L || R<l))res=f(result(max(L,l),min(R,r),L,len,ind*2+1),res);
		L+=len;R+=len;
		if(!(r<L || R<l))res=f(result(max(L,l),min(R,r),L,len,ind*2+2),res);
		return res;
	}

	void construct(){for(int i=treeSize-2;i>0;i-=2)tree[i/2]=f(tree[i],tree[i+1]);}
	void set(long long int index, T value){tree[treeSize-dataSize+index]=value;}
	T get(long long int index){return tree[treeSize-dataSize+index];}

	T f(T a,T b){
		return a+b;
	}
};

int main(){
	bool flag=true;
	long long int H,W,M,X,Y,ans=0;
	vector<long long int> row[MAX];
	cin>>H>>W>>M;

	segtree<long long int> seg(W,0);

	for(int i=0;i<W;i++)seg.set(i,0);
	seg.construct();

	for(int i=0;i<M;i++){
		cin>>X>>Y;X--;Y--;
		row[X].push_back(Y);
	}
	for(int i=0;i<H;i++){
		row[i].push_back(W);
		sort(row[i].begin(),row[i].end());

		if(row[i][0]==0)flag=false;
		ans+=row[i][0];
		if(!flag)ans-=seg.result(0,row[i][0]-1);
		if(i==0){
			for(int j=row[i][0];j<W;j++)seg.set(j,1);
			seg.construct();
		}else{
			for(int j=1;j<row[i].size();j++){
				if(row[i][j-1]+1==row[i][j])continue;
				ans+=(row[i][j]-1)-(row[i][j-1]+1)+1-seg.result(row[i][j-1]+1,row[i][j]-1);
			}
			for(int j=0;j<row[i].size()-1;j++)seg.replace(row[i][j],1);
		}
	}
	cout<<ans;
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
int shu1,shu2,shu3,shu4;
int shu;
int main()
{
	cin>>shu1>>shu2>>shu3>>shu4;
	shu=min(shu1,min(shu2,min(shu3,shu4))) ;
	cout<<shu;
}

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    int h, w, a, b;
    cin >> h >> w >> a >> b;
    int t = h * w;
    int m = 1 << (h * w);
    int dp[m][a + 1];
    memset(dp, 0, sizeof dp);
    dp[0][0] = 1;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            for (int k = 0; k < (1 << (i * w + j)); k++) {
                for (int l = 0; l < a; l++) {
                    if (i > 0) {
                        int temp = (1 << (i * w - w + j)) | (1 << (i * w + j));
                        if ((temp & k) == 0)
                            dp[temp | k][l + 1] += dp[k][l];
                    }
                    if (j > 0) {
                        int temp = (1 << (i * w - 1 + j)) | (1 << (i * w + j));
                        if ((temp & k) == 0)
                            dp[temp | k][l + 1] += dp[k][l];
                    }
                }
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < m; i++) {
        ans += dp[i][a];
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define eni(x) template < class c > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
template < class c > struct rge { c b, e; };
template < class c > rge<c> range(c i, c j) { return rge<c>{i, j}; }
template < class c > auto dud(c* x) -> decltype(cerr << *x, 0);
template < class c > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; return * this; }
eni(==) return * this << range(begin(i), end(i)); }
template < class c, class b > debug & operator <<(pair < b, c > d) {
  return * this << "(" << d.first << ", " << d.second << ")";
}
template < class c > debug & operator <<(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  return * this << "]";
}
#else
template < class c > debug & operator <<(const c&) { return * this; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define mod 1000000007
#define mx 1000050
mt19937 myrand(time(0));

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(),cout.tie();
    int n;
    cin>>n;
    set<int> s;
    for(int i=0;i<=200001;i++) s.insert(i);
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        s.erase(x);
        cout<<*s.begin()<<endl;
    }
    return 0;
}

//go to line 53 for some useful code.
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.hpp"
#else
#define dbg(...) 47
#define dbgm(...) 47
#endif
// refer https://codeforces.com/blog/entry/66279
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
//refer-> https://codeforces.com/blog/entry/62393
struct custom_hash {
   static uint64_t splitmix64(uint64_t x) {
       // http://xorshift.di.unimi.it/splitmix64.c
       x += 0x9e3779b97f4a7c15;
       x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
       x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
       return x ^ (x >> 31);
   }
   size_t operator()(uint64_t x) const {
       static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
       return splitmix64(x + FIXED_RANDOM);
   }
};
typedef long long ll;
typedef unsigned long int ul;
typedef unsigned long long int ull;
typedef unsigned int ui;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;
typedef vector<ll> vll;
typedef vector<int> vii;
typedef unordered_map<int,int, custom_hash> umap;
typedef unordered_set<int, custom_hash>uset;
#define f(i, x, n) for (int i = x; i < n; i++)
#define rf(i, n, x) for(int i=n;i>=x;--i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define mem(a, b) memset(a, b, sizeof(a))
#define fast_io() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define mod ((ll)(1e9+7))
#define oo INT64_MAX
#define MX ((int)(2e5+5))
//*	write code from here
int fen[MX];
void add(int idx){
	for(;idx<MX;idx += (idx&(-idx))) fen[idx]++;
}
int sum(int idx){
	int val = 0;
	for(;idx>0;idx -= (idx &(-idx))) val +=fen[idx];
	return val;
}
void solve(){
	int n;
	cin>>n;
	vii a(n), b(n);
	for(auto &aa:a)cin>>aa;
	for(auto &bb:b)cin>>bb;
	for(int i=0;i<n;++i){
		a[i] +=i;
		b[i] +=i;
	}
	map<int,queue<int>> map;
	f(i, 0, n) map[b[i]].push(i+1);
	f(i, 0, n){
		int t = a[i];
		if(map.find(t) == map.end() || map[t].empty()){
			cout<<-1<<endl;
			return;
		}
		a[i] = map[t].front();
		map[t].pop();
	}
	ll ans = 0;
	f(i, 0, n){
		ans += sum(MX-3)-sum(a[i]);
		add(a[i]);
	}
	cout<<ans<<endl;
}
int main()
{
	fast_io();
	cerr << "...............Console is yours! :)................." << endl;
	//freopen("input.in","r",stdin);
	solve();
	cerr<<"......^_^....."<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int p = 31;
const int m = 1e9 + 7;
typedef long long ll;
typedef int in;
#define ff first
#define ss second
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define pb push_back
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

const int N = 1e2 + 10, M = N * N;

int seen[N];
vector<pii> g[N];
vector<int>type(M, -1);

void dfs(int u, int par, vector<pii>& edges) {
    seen[u] = 1;
    for (auto& [v, e] : g[u]) {
        if (v == par or seen[v] == 2) {
            continue;
        }
        if (seen[v]) {
            if (edges[e].first == u) {
                type[e] = 2;
            }
            else {
                type[e] = 1;
            }
        }
        else {
            if (edges[e].first == u) {
                type[e] = 2;
            }
            else {
                type[e] = 1;
            }
            dfs(v, u, edges);
        }
    }
    seen[u] = 2;
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<pii> edges(m);
    for (auto& [a, b] : edges) {
        cin >> a >> b;
        --a; --b;
    }
    vector<int> c(n);
    for (auto& i : c) {
        cin >> i;
    }
    for (int i = 0; i < m; ++i) {
        int a = edges[i].first, b = edges[i].second;
        if (c[a] == c[b]) {
            g[a].pb({b, i});
            g[b].pb({a, i});
            continue;
        }
        if (c[a] < c[b])type[i] = 1;
        else
            type[i] = 2;
    }
    for (int i = 0; i < n; ++i) {
        if (!seen[i]) {
            dfs(i, -1, edges);
        }
    }
    for (int i = 0; i < m; ++i) {
        if (type[i] == 1) {
            cout << "<-\n";
        } else {
            cout << "->\n";
        }
    }
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0;i<(int)(n);i++)
int main()
{
    int n,x;
    cin >> n >> x;
    vector<int> a(n), b(n);
    int i = 0;
    rep(i,n){
        cin >> a[i];
        if(a[i] != x)
        {
            b[i] = a[i];
            cout << b[i] << " ";
        }
    }

    return 0;

}


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

const int64_t MOD = 1000000007;
typedef int64_t ll;
typedef uint64_t ull;

#define FOR(i, start, end) for(uint64_t i=start; i<end; i++)
#define REP(i, n) FOR(i, 0, n)

// 最大公約数gcd
// 最小公倍数lcm=m*n/gcd
uint64_t gcd(uint64_t m, uint64_t n) {
  uint64_t temp;
  while (m % n != 0){
    temp = n;
    n = m % n;
    m = temp;
  }
  return n;
}

uint64_t lcm(uint64_t m, uint64_t n) {
  uint64_t g = gcd(m,n);
  if(m%g==0) return (m/g)*n;
  if(n%g==0) return (n/g)*m;
  return (m*n)/gcd(m,n);
}

// vector<vector<uint64_t> > v(n+1, vector<uint64_t>(n+1, 0))
// v[n][k]に組み合わせ数が入る。
void comb(vector<vector <uint64_t> > &v){
  for(uint64_t i = 0;i <v.size(); i++){
    v[i][0]=1;
    v[i][i]=1;
  }
  for(uint64_t k = 1;k <v.size();k++){
    for(uint64_t j = 1;j<k;j++){
      v[k][j]=(v[k-1][j-1]+v[k-1][j]);
    }
  }
}

// 掛け算オーバーフロー判定
bool is_product_overflow(uint64_t a, uint64_t b) {

	uint64_t prod = a * b;

	return (prod / b != a);
}

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

// フェルマーの小定理
// mod. m での a の逆元 a^{-1} を計算する
// (a/b)%m = a*movinv(b,m)%m
int64_t modinv(int64_t a, int64_t m) {
    int64_t b = m, u = 1, v = 0;
    while (b) {
        int64_t t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

/*
  //エラトステネスのふるい
  int maxNum = 100000;
  char isNotPrime[maxNum+1] = {};
  isNotPrime[0]=isNotPrime[1]=1;
  for(int i=2;i<maxNum;i++){
    if(isNotPrime[i]==1) continue;
    int num = i+i;
    while(num<=maxNum){
      isNotPrime[num]=1;
      num += i;
    }
  }
*/
// 円周率
// M_PI

// #include <iomanip> // setprecisionを使用するのに必要
// cout << std::fixed << std::setprecision(15) << y << endl;

// 昇順
// priority_queue<int, vector<int>, greater<int> > queue;

// 降順ソート vector<int> v;
// sort(v.begin(),v.end(),greater<int>());

signed main() {
  ll n = 0;

  ll m = 1000;
  for(int i=0;i<4;i++){
    cin >>  n;
    m = min(m,n);
  }
  cout << m << endl;

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
// --------------------------------------------------------
template<class T> bool chmax(T& a, const T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T b) { if (b < a) { a = b; return 1; } return 0; }
#define FOR(i,l,r) for (ll i = (l); i < (r); ++i)
#define RFOR(i,l,r) for (ll i = (r)-1; (l) <= i; --i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUMLL(c) accumulate(ALL(c), 0LL)
#define COUNT(c,v) count(ALL(c),(v))
#define SZ(c) ((ll)(c).size())
#define BIT(b,i) (((b)>>(i)) & 1)
#define PCNT(b) __builtin_popcountll(b)
#define CIN(c) cin >> (c)
#define COUT(c) cout << (c) << '\n'
#define debug(x) cerr << "l." << __LINE__ << " : " << #x << " = " << (x) << '\n'
ll llceil(ll a, ll b) { return (a + b - 1) / b; }
ll bitlen(ll b) { if (b <= 0) { return 0; } return (64LL - __builtin_clzll(b)); }
string toupper(const string& S) { string T(S); REP(i,SZ(T)) T[i] = toupper(T[i]); return T; }
string tolower(const string& S) { string T(S); REP(i,SZ(T)) T[i] = tolower(T[i]); return T; }
using P = pair<ll,ll>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VVS = vector<VS>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VVVLL = vector<VVLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VVVB = vector<VVB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VVVD = vector<VVD>;
using VLD = vector<ld>;
using VVLD = vector<VLD>;
using VVVLD = vector<VVLD>;
static const double EPS = 1e-10;
static const double PI  = acos(-1.0);
static const ll MOD = 1000000007;
// static const ll MOD = 998244353;
static const ll INF = (1LL << 62) - 1;  // 4611686018427387904 - 1
// --------------------------------------------------------
// #include <atcoder/all>
// using namespace atcoder;

// Editorial AC

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

    ll N; cin >> N;
    VLL A(N); REP(i,N) cin >> A[i];
    VLL B(N); REP(i,N) cin >> B[i];

    VLL E,O;  // Even,Odd
    REP(i,N) {
        if (i % 2 == 0) {
            E.push_back(B[i] - A[i]);
        } else {
            O.push_back(B[i] - A[i]);
        }
    }
    RSORT(E); RSORT(O);

    ll score = SUMLL(A);
    ll ans = score;
    REP(i,N/2) {
        score += E[i] + O[i];
        chmax(ans, score);
    }
    COUT(ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define vll vector<ll>
#define vvll vector<vll>
#define mll map<ll,ll>
#define f(i,m,n) for(i=m;i<n;i++)
#define ee endl
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define take(v,n) for(i=0;i<n;i++) cin>>v[i];
#define print(v,n) for(i=0;i<n;i++) cout<<v[i]<<" ";
#define No cout<<"NO"<<endl;return
#define Yes cout<<"YES"<<endl;return
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sot(v) sort(v.begin(),v.end());
#define PI 3.14159265   ;
ll mod =1e9+7;
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (int i = 5; i*i <= n; i = i + 6)
        if (n%i == 0 || n % (i + 2) == 0)
            return false;
    return true;
}
ll powe(ll x, ll y) {
    ll res = 1;
    while (y > 0) {
        if (y & 1) res =( res * x)%mod;
        y = y >> 1;
        x = (x * x)%mod;
    }
    return res;
}
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};
unsigned long long power(unsigned long long x, int y, int p)
{
    unsigned long long res = 1; // Initialize result

    x = x % p; // Update x if it is more than or
    while (y > 0)
    {
        if (y & 1) res = (res * x) % p;
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
unsigned long long modInverse(unsigned long long n,int p)
{
    return power(n, p - 2, p);
}
unsigned long long nCrModPFermat(unsigned long long n,int r, int p)
{
    if (n < r)
        return 0;
    // Base case
    if (r == 0)
        return 1;
    unsigned long long fac[n + 1];
    fac[0] = 1;
    for (int i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % p;

    return (fac[n] * modInverse(fac[r], p) % p
            * modInverse(fac[n - r], p) % p)
           % p;
}

void solve()
{
    ll i, x,m, y, j, k, h, n, w, k1, k2,k3,q;
    cin>>n>>x;
    x*=100;
    ll sum=0;
    for(i=0;i<n;i++)
    {
        cin>>k1>>k2;
        ll temp=k1*k2;
        sum+=temp;
        //cout<<sum<<ee;
        if(sum>x)
        {
            cout<<i+1<<ee;
            return;
        }

    }
    cout<<-1;
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    fast;
    ll i, j, l, k, t, n;
    t=1;
    for (i = 1; i <= t; i++)
    {
        //cout<<"Case #"<<i<<": ";
        solve();
    }
}
//

#include <iostream>
using namespace std;

int main(){
	int n, t=0, x; double s;
	cin >> n >> s;
	s = (int)s*100;
	int a[n+1]={0}, b[n+1]={0};

	for (int i=1; i<=n; i++)
		cin >> a[i] >> b[i];

	for (int i=1; i<=n; i++){
		x = a[i]*b[i];
		t += x;
		if (t > s){
			cout << i << endl; return 0;
		}
	}

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


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

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << "Line " << __LINE__ << ": " <<  #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;

template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};

const ll INF = (1ll<<62);
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;

typedef vector<vector<ll>> Graph;
bool bipartite_coloring(int now, int col, Graph &G, vector<int> &mark){
    //vector<int> mark(N, -1) を渡す！
    if(mark[now] != -1) return (mark[now] == col);
    mark[now] = col;
    bool ok = true;
    for(auto next: G[now]){
        ok &= bipartite_coloring(next, 1-col, G, mark);
    }
    return ok;
}

int main(){

    ll N, M;
    cin >> N >> M;
    V<ll> A(M), B(M);
    REP(i, M){
        cin >> A[i] >> B[i];
        A[i]--, B[i]--;
    }

    ll res = 0;
    REP(mask, 1ll<<N){
        Graph G(N);
        ll cnt = 1;
        V<int> mark(N, -1);
        REP(i, M){
            if((mask >> A[i] & 1) and (mask >> B[i] & 1)){
                goto HELL;
            }

            if((!(mask >> A[i] & 1)) and (!(mask >> B[i] & 1))){
                G[A[i]].push_back(B[i]);
                G[B[i]].push_back(A[i]);
            }
        }

        REP(i, N){
            if(!(mask >> i & 1)){
                if(mark[i] == -1){
                    if(bipartite_coloring(i, 0, G, mark)){
                        cnt *= 2;
                    }else{
                        cnt *= 0;
                    }
                }
            }
        }
        res += cnt;
        HELL:;
    }

    cout << res << endl;

    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 1e5;

ll f(ll x, ll n) {
    ll sum = 0;
    for (int i = 1; i <= n; i++) {
        if (i + 2 > x) break;
        ll j = x - i;
        ll k = max<ll>(0, j - n);
        if (k > n) k = n;
        sum += n * k;
        ll l = (j - 1) - (k + 1) + 1;
        if (l >= 1) sum += l * (l + 1) / 2;
        if (n < j - 1) {
            l = j - 1 - n;
            sum -= l * (l + 1) / 2;
        }
        // debug(i);
        // debug(sum);
    }
    return sum;
}

map<ll, vvll> g(ll n) {
    map<ll, vvll> mp;
    REP(i,n+1)REP(j,n+1)REP(k,n+1) {
        mp[i + j + k].push_back(vll{i, j, k});
    }
    return mp;
}

int main() {
    ll n, k; cin >> n >> k;
    // debug(f(n * 3 - 1, n));
    // debug(f(14, n) - f(13, n));
    // debug(f(12, n));
    // auto res = g(n);
    // debug((int)res[14].size());
    // debug(f(n * 3, n));
    ll ok = 0, ng = 3 * n + 1;
    while (ng - ok > 1) {
        ll mid = (ok + ng) / 2;
        if (f(mid, n) < k) ok = mid;
        else ng = mid;
    }
    k -= f(ok, n);
    int first = -1;
    ll sum_1 = ng;
    // debug(sum_1);
    // debug(k);
    for (int i = 1; i <= n; i++) {
        // debug(i);
        ll tmp = sum_1 - i;
        ll l = max<ll>(1, tmp - n);
        ll r = min<ll>(n, tmp - 1);
        ll len = r - l + 1;
        if (len <= 0) continue;
        // debug(len);
        if (k <= len) {
            first = i;
            break;
        }
        else k -= len;
    }
    // debug(k);
    // debug(first);
    ll sum_2 = sum_1 - first;
    // debug(sum_2);
    int second = -1;
    for (int i = 1; i <= n; i++) {
        if (sum_2 - i <= 0 || sum_2 - i > n) continue;
        k--;
        if (k == 0) {
            second = i;
            break;
        }
    }
    cout << first << " " << second << " " << sum_2 - second << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
template<class T,class U>
inline bool chmax(T &a,U b){
    if(a<b){
        a=b;
        return true;
    }
    return false;
}
template<class T,class U>
inline bool chmin(T &a,U b){
    if(a>b){
        a=b;
        return true;
    }
    return false;
}
constexpr int INF=1000000000;
constexpr ll llINF=3000000000000000000;
constexpr int mod=1000000007;
constexpr double eps=1e-8;
const double pi=acos(-1);
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int Random(int mi,int ma){
    random_device rnd;
    mt19937 mt(rnd());//32bit
    //[mi,ma]
    uniform_int_distribution<int>engine(mi,ma);
    return engine(mt);
}
/*
vector<vector<ll>>C,sC;
void init_comb(int n,int m){
    C.resize(n+1,vector<ll>(m+1,0));
    sC.resize(n+1,vector<ll>(m+1,0));
    C[0][0]=1;
    for(int i=1;i<=n;i++){
        C[i][0]=1;
        for(int j=1;j<=m;j++){
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
        }
    }
    rep(i,n+1){
        rep(j,m){
            sC[i][j+1]=(sC[i][j]+C[i][j])%mod;
        }
    }
}*/
ll gcd(ll a,ll b){
    while(a%b){
        a%=b;
        swap(a,b);
    }
    return b;
}
ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
bool isprime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
vector<int>primes;
void init_prime(int n){
	primes.push_back(2);
    for(int i=3;i<=n;i+=2){
        bool f=true;
        for(int j:primes){
            if(j*j>i)break;
            if(i%j==0){
                f=false;
                break;
            }
        }
        if(f)primes.push_back(i);
    }
}
ll modpow(ll a,ll b){
    ll res=1;
    while(b){
        if(b&1){
            res*=a;
            res%=mod;
        }
        a*=a;
        a%=mod;
        b>>=1;
    }
    return res;
}
vector<ll>inv,fact,factinv;
void init_fact(int n){
    inv.resize(n+1);
    fact.resize(n+1);
    factinv.resize(n+1);
    inv[0]=0;
    inv[1]=1;
    fact[0]=1;
    factinv[0]=1;
    for(ll i=1;i<=n;i++){
        if(i>=2)inv[i]=mod-((mod/i)*inv[mod%i]%mod);
        fact[i]=(fact[i-1]*i)%mod;
        factinv[i]=factinv[i-1]*inv[i]%mod;
    }
}
ll _inv(ll a,ll m=mod){
    //gcd(a,m) must be 1
    ll b=m,u=1,v=0;
    while(b){
        ll t=a/b;
        a-=t*b;swap(a,b);
        u-=t*v;swap(u,v);
    }
    u%=m;
    if(u<0)u+=m;
    return u;
}
ll comb(int a,int b){
    if(a<b)return 0;
    if(a<0)return 0;
    return fact[a]*factinv[a-b]%mod*factinv[b]%mod;
}
ll multicomb(int a,int b){
    return comb(a+b-1,b);
}
int n,m;
vector<int>g[1010][26];
int dp[1010][1010];
#define P pair<int,int>
#define PP pair<int,P>
void solve(){
    cin>>n>>m;
    priority_queue<PP,vector<PP>,greater<PP>>q;
    rep(i,n)rep(j,n)dp[i][j]=INF;
    rep(i,n){
        dp[i][i]=0;
        q.push({0,{i,i}});
    }
    rep(i,m){
        int a,b;
        char c;
        cin>>a>>b>>c;
        a--,b--;
        g[a][c-'a'].push_back(b);
        g[b][c-'a'].push_back(a);
        if(a!=b){
            dp[a][b]=dp[b][a]=1;
            q.push({1,{a,b}});
            q.push({1,{b,a}});
        }
    }
    while(!q.empty()){
        PP p=q.top();
        q.pop();
        int l=p.second.first,r=p.second.second;
        if(dp[l][r]!=p.first)continue;
        rep(k,26){
            for(int a:g[l][k]){
                for(int b:g[r][k]){
                    if(chmin(dp[a][b],p.first+2)){
                        q.push({dp[a][b],{a,b}});
                    }
                }
            }
        }
    }
    cout<<(dp[0][n-1]==INF?-1:dp[0][n-1])<<endl;
}
int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    //int t;cin>>t;while(t--)
    solve();
    return 0;
}

/***

                                Bismillahir Rahmanir Rahim
                                Rabbi Zidni Ilma
                                Author : Saidur Rahman
                                Department of CSE, City University, Bangladesh.

***/

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

#define         endl                "\n"
#define         EPS                 1e-6
#define         F                   first
#define         sf                  scanf
#define         pf                  printf
#define         S                   second
#define         done                return 0
#define         MP                  make_pair
#define         pb                  push_back
#define         start               int main()
#define         PI                  acos(-1.0)
#define         NO                  cout<<"NO\n"
#define         No                  cout<<"No\n"
#define         no                  cout<<"no\n"
#define         YES                 cout<<"YES\n"
#define         Yes                 cout<<"Yes\n"
#define         yes                 cout<<"yes\n"
#define         sz(x)               int(x.size())
#define         all(a)              a.begin(),a.end()
#define         srt(x)              sort(x.begin(),x.end())
#define         clock_start         clock_t tStart = clock()
#define         MEM(a, b)           memset(a, (b), sizeof(a))
#define         watch(x)            cout << (#x) << " is " << (x) << endl
#define         Fast                ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define         show_time           cout<<"\n\nTime Taken: "<<fixed<<setprecision(3)<<(double)(clock() - tStart) /  CLOCKS_PER_SEC<<"s"

            /****** Template of some bit operations *****/

long long     ToggleBit (long long n, long long X)    { return n ^ (1LL << X); }//k-1 tomo bit 1 hole 0 korbe r 0 hole 1
long long     SetBit (long long n, long long X)       { return n | (1LL << X); }//k-1 tomo bit 0 hole 1 korbe, 1 hole unchange
long long     ClearBit (long long n, long long X)     { return n & ~(1LL << X); }//k-1 tomo bit 1 hole 0 korbe, 0 hole unchange
bool          CheckBit (long long n, long long X)     { return (bool)(n & (1LL << X)); }//k-1 tomo bit 1 naki 0 bolbe

            ///////////////////////////////////////////////////////////////////////////
        ///////////////////////// DO NOT TOUCH BEFORE THIS LINE //////////////////////////
            //////////////////////////////////////////////////////////////////////////

typedef     long long   ll;
const       int MAX  = 1e5 + 5;
const       int MOD  = 1e9 + 7;
const       int INF  = 1e9;
const       ll LINF  = 1e18;

start
{
    Fast;

#ifndef ONLINE_JUDGE
    clock_start;
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    string s1, s2;
    cin >> s1 >> s2;
    int cnt1=0, cnt2=0;
    for (int i = 0; i < sz(s1);i++)
    {
        cnt1 += (s1[i] - '0');
    }
    for (int i = 0; i < sz(s2);i++)
    {
        cnt2 += (s2[i] - '0');
    }
    cout << max(cnt1, cnt2) << endl;

#ifndef ONLINE_JUDGE
    show_time;
#endif

    done;
}


                                //ABHISHEK AGRAWAL,BIT mesra//
                        //Newbie......You have to be odd to be no. ONE :)//
  //Authhor: Abhishekagrawal
  //Date: 20/02/2021
  //Time: 17:30:13
  #include<bits/stdc++.h>
  using namespace std;
  #define ll long long int
  #define ld long double
  #define fl float
  #define vll vector<ll>
  #define pii pair<int,int>
  #define vpll vector<pair<ll,ll>>
  #define pb push_back
  #define f first
  #define s second
  #define mll map<ll,ll>
  #define mp make_pair
  #define sp(n) fixed<<setprecision(n)
  #define mod (ll)1000000007
  #define sortv(v) sort(v.begin(),v.end())
  #define INF (ll)(1e15)
  #define loop(i,n) for(int i=0;i<n;i++)
  #define loop1(i,n) for(int i=1;i<=n;i++)
  #define rloop(n,i) for(int i=n-1;i>=0;i--)
  #define sorta(a) sort(a,a+n,greater<ll>())
  #define countone(n) __builtin_popcount(n)
  #define numoftrailzero(n) __builtin_ctz(n)
  #define maxpowoftwo(n) __builtin_clz(n)
  #define leastindexwithone(n)  __builtin_ffs(n)
  #define what_is(x) cerr << #x << " is " << x << endl;
  #define pfv(v) cout<<v.size()<<"\n";loop(i,v.size()) cout<<v[i]<<" ";cout<<"\n";
  #define pv(v) loop(i,v.size()) cout<<v[i]<<" ";
  #define all(v) v.begin(),v.end()
  #define mset(dp,val) memset(dp,val,sizeof(dp))
  ll test,n;ll sum,ans;
  // #ifdef TESTING
  // #define DEBUG fprintf(stderr, "====TESTING====\n")
  // #define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
  // #define debug(...) fprintf(stderr, __VA_ARGS__)
  // #else
  // #define DEBUG
  // #define VALUE(x)
  // #define debug(...)
  // #endif
  void the_happiest_place_on_earth()
  {
      ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #ifdef ENABLE_FILE_IO
      freopen("out.txt", "r", stdin);
      freopen("out1.txt", "w", stdout);
    #endif
  }
  // bool comp(const pair<ll,ll> &a, const pair<ll,ll> &b){
  //   if(a.s+2*a.f==b.s+2*b.f) return a.f>b.f;
  //   return (a.s+2*a.f>b.s+2*b.f);
  // }

  const int N=200040;ll a[N],b[N],c[N],par[N],Size[N],present[N],color[N];
  ll power(ll a,ll b,ll M){a%=M;ll res=1;while(b>0){if(b%2 ==1){res=(res*a)%M;}a=(a*a)%M;b/=2;} return res;}
  void testcase(){
    ll m;
    cin>>n>>m;ll k;cin>>k;
    ll mo=998244353;
    if(n==1){
      cout<<power(k,m,mo);
      return;
    }
    if(m==1){
      cout<<power(k,n,998244353);
      return;
    }
    ans=0;
    loop1(i,k){
      ll n1=power(i,n,mo);
      ll n2=power(i-1,n,mo);
      n1=((n1-n2)%mo+mo)%mo;
      n2=power(k-i+1,m,mo);
      n1=(n1*n2)%mo;
      (ans+=n1)%=mo;
    }
    cout<<ans<<"\n";
    return;
  }

  int main(){
    the_happiest_place_on_earth();
    // Today's thought: Push yourself,because no one else is going to do it for you.
    // Things are not always #000000 and #FFFFFF
    // START OF PROGRAM LOGIC
    // g++ -o a BB.cpp -DENABLE_FILE_IO;./a
    // g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG -o a file_name.cpp;./a
    test=1;int t=0;
    // cin>>test;
    while(test--){
      //cout<<"Case #"<<++t<<": ";
      testcase();
    }
    cerr << "Time : " << 1000 *((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    // //END OF PROGRAM LOGIC
    return 0;
  }
  /* stuff you should look for
      * int overflow, array bounds
      * special cases (n=1?)
      * do smth instead of nothing and stay organized
      * WRITE STUFF DOWN
      * DON'T GET STUCK ON ONE APPROACH
  */

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

ll M = 998244353;

ll mpow(ll x, ll n){ //x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
    ll ans = 1;
    while(n != 0){
        if(n&1) ans = ans*x % M;
        x = x*x % M;
        n = n >> 1;
    }
    return ans;
}

// vector<ll> fac(300001); //n!(mod M)
// vector<ll> ifac(300001); //k!^{M-2} (mod M)
// ll comb(ll a, ll b){ //aCbをmod計算
//     if(a == 0 && b == 0)return 1;
//     if(a < b || a < 0)return 0;
//     ll tmp = ifac[a-b]* ifac[b] % M;
//     return tmp * fac[a] % M;
// }

int main(){
    // fac[0] = 1;
    // ifac[0] = 1;
    // for(ll i = 0; i<300000; i++){
    //     fac[i+1] = fac[i]*(i+1) % M; // n!(mod M)
    //     ifac[i+1] = ifac[i]*mpow(i+1, M-2) % M; // k!^{M-2} (mod M) ←累乗にmpowを採用
    // }

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

    if (n == 1 & m == 1) {
        cout << k << endl;
        return 0;
    }

    if (n == 1 | m == 1) {
        ll ans = 0;
        for(int d = 1; d <= k; d++) {
            ans += (mpow(d, n) - mpow(d - 1, n)) * (mpow(k-d + 1, m) - mpow(k - d, m));
            ans += M;
            ans %= M;
        }
        cout << ans << endl;
        return 0;
    }
    // if (m == 1) {
    //     ll ans = 0;
    //     for(int a0 = 1; a0 <= k; a0++) {
    //         ans += (mpow(d, n) - mpow(d - 1, n)) * (mpow(k-d + 1, m) - mpow(k - d, m));
    //         ans += M;
    //         ans %= M;
    //     }
    //     cout << ans << endl;
    //     return 0;
    // }

    ll ans = 0;
    ll tmp = 0;
    for(int d = 1; d <= k; d++) {
        ans += (mpow(d, n) - mpow(d - 1, n)) * mpow(k-d + 1, m);
        ans += M;
        ans %= M;
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
#define print(x) if(x)cout<<"YES\n";else cout<<"NO\n";
#define pb push_back
#define F first
#define S second
//#define mod 1000000007
#define mod 998244353
using namespace __gnu_pbds;
using namespace std;
long long int binaryexpo(long long x, long long n)
{
	if (n == 0)
		return 1;
	else if (n & 1)
		return (x * binaryexpo(x * x, (n - 1) / 2));
	else
		return binaryexpo(x * x, (n / 2));
}
ll score(string x) {
	ll freq[10] = {};
	for (auto c : x) {
		freq[c - '0']++;
	}
	ll sc = 0;
	for (ll i = 1; i <= 9; i++) {
		ll temp = i * binaryexpo(10, freq[i]);
		sc += temp;
	}
	return sc;
}
void solve() {
	cout << fixed << setprecision(20);
	ll k;
	cin >> k;
	vector<ll> v(9, k);
	for (int i = 0; i < 9; i++) {
		v[i] = k;
	}
	string s, t;
	cin >> s >> t;
	for (auto x : s) {
		v[x - '1']--;
	}
	for (auto x : t) {
		v[x - '1']--;
	}
	ll total = (9 * k - 8) * (9 * k - 9), cnt = 0;
	long double ans = 0.00;
	for (int i = 1; i <= 9; i++) {
		for (int j = 1; j <= 9; j++) {
			if (i == j && v[i - 1] > 1) {
				string a = s.substr(0, 4);
				a.pb((i + '0'));
				string b = t.substr(0, 4);
				b.pb((i + '0'));
				ll x = score(a);
				ll y = score(b);
				if (x > y) {
					ll aa = v[i - 1] * (v[i - 1] - 1) ;
					ans += (long double)aa / total ;
					cnt += aa;
				}
			}
			else if (i != j && v[i - 1] > 0 && v[j - 1] > 0) {
				string a = s.substr(0, 4);
				a.pb((i + '0'));
				string b = t.substr(0, 4);
				b.pb((j + '0'));
				ll x = score(a);
				ll y = score(b);
				if (x > y) {
					ll aa = v[i - 1] * v[j - 1];
					ans += (long double)aa / total;
					cnt += aa;
				}
			}
		}
	}
	// cout << cnt << " " << total << "\n";
	// ans = (long double)cnt / total;
	cout << ans << "\n";

}
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	fastio;
	int T = 1;
	// cin >> T;
	while (T--) {
		solve();
	}
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=200005,INF=1<<30;

ll rui[20];

int main(){

    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);

    rui[0]=1;
    for(int i=1;i<10;i++) rui[i]=rui[i-1]*10;

    ll K;cin>>K;
    string S,T;cin>>S>>T;
    vector<ll> rem(10,K);

    double ans=0;

    for(int i=0;i<4;i++){
        rem[int(S[i]-'0')]--;
        rem[int(T[i]-'0')]--;
    }

    for(int a=1;a<=9;a++){
        for(int b=1;b<=9;b++){
            if(a==b){
                if(rem[a]<=1) continue;
                ll cnt=rem[a]*(rem[a]-1);

                ll X=0,Y=0;
                for(int i=1;i<=9;i++){
                    int ca=0;
                    for(int j=0;j<4;j++){
                        if(int(S[j]-'0')==i) ca++;
                    }
                    if(i==a) ca++;
                    X+=rui[ca]*i;

                    ca=0;

                    for(int j=0;j<4;j++){
                        if(int(T[j]-'0')==i) ca++;
                    }
                    if(i==b) ca++;
                    Y+=rui[ca]*i;
                }

                if(X>Y) ans+=cnt;
            }else{
                if(rem[a]<=0||rem[b]<=0) continue;
                ll cnt=rem[a]*(rem[b]);

                ll X=0,Y=0;
                for(int i=1;i<=9;i++){
                    int ca=0;
                    for(int j=0;j<4;j++){
                        if(int(S[j]-'0')==i) ca++;
                    }
                    if(i==a) ca++;
                    X+=rui[ca]*i;

                    ca=0;

                    for(int j=0;j<4;j++){
                        if(int(T[j]-'0')==i) ca++;
                    }
                    if(i==b) ca++;
                    Y+=rui[ca]*i;
                }

                if(X>Y) ans+=cnt;
            }
        }
    }

    ll pat=(9*K-8)*(9*K-9);

    ans/=pat;

    cout<<fixed<<setprecision(25)<<ans<<endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#define int long long
using namespace std;
vector<pair<int,int>>x, y;
signed main()
{
	int N;
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> N;
	int i;
	for (i = 0; i < N; i++)
	{
		int a, b;
		cin >> a >> b;
		x.push_back({ a,i });
		y.push_back({ b,i });
	}
	sort(x.begin(), x.end());
	sort(y.begin(), y.end());
	vector<int>r;
	r.push_back(x[N - 1].first - x[0].first);
	r.push_back(x[N - 2].first - x[0].first);
	r.push_back(x[N - 1].first - x[1].first);
	if(!((((x[0].second==y[0].second)&&(x[N-1].second==y[N-1].second))||((((x[0].second == y[N-1].second) && (x[N - 1].second == y[0].second)))))))
		r.push_back(y[N - 1].first - y[0].first);
	r.push_back(y[N - 2].first - y[0].first);
	r.push_back(y[N - 1].first - y[1].first);
	sort(r.begin(), r.end());
	cout << r[r.size()-2];
}

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0; i<(n); i++)
#define sz(x) int(x.size())

using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef tuple<int,int,int> tp;

int main(){
    int n; cin >> n;
    set<int> house;

    vector<P> h(n);
    vector<int> X(4);
    X[0] = X[1] = -1e9;
    X[2] = X[3] =  1e9;
    vector<int> idx(4);

    vector<int> Y(4);
    Y[0] = Y[1] = -1e9;
    Y[2] = Y[3] =  1e9;
    vector<int> idy(4);

    rep(i,n){
        int x,y; cin >> x >> y;

        if(x > X[0]){ swap(X[0],X[1]); swap(idx[0],idx[1]); X[0] = x; idx[0] = i; }
        else if(x > X[1]){ X[1] = x; idx[1] = i; }

        if(x < X[2]){ swap(X[2],X[3]); swap(idx[2],idx[3]); X[2] = x; idx[2] = i; }
        else if(x < X[3]){ X[3] = x; idx[3] = i; }

        if(y > Y[0]){ swap(Y[0],Y[1]); swap(idy[0],idy[1]); Y[0] = y; idy[0] = i; }
        else if(y > Y[1]){ Y[1] = y; idy[1] = i; }

        if(y < Y[2]){ swap(Y[2],Y[3]); swap(idy[2],idy[3]); Y[2] = y; idy[2] = i; }
        else if(y < Y[3]){ Y[3] = y; idy[3] = i; }

        h[i] = make_pair(x,y);
    }

    rep(i,4) house.insert(idx[i]);
    rep(i,4) house.insert(idy[i]);

    vector<int> dist;

    for(auto u : house){
        for(auto v : house){
            if(u == v) continue;
            dist.push_back(max(abs(h[u].first-h[v].first), abs(h[u].second-h[v].second)));
        }
    }

    sort(dist.begin(),dist.end());
    cout << dist[sz(dist)-3] << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
int a,b,c,d;
bool check(int a,int b,int c,int d)
{
    if(a + b == c + d || a - b == c - d || abs(a-c) + abs(b-d) <= 3) return true;
    return false;
}
signed main()
{
    cin >> a >> b >> c >> d;
    if(a == c && b == d) {puts("0"); return 0;}
    else if(a + b == c + d || a - b == c - d || abs(a-c) + abs(b-d) <= 3) {puts("1"); return 0;}
    else
    {
        for(int i = a - 3; i <= a + 3; ++ i)
        {
            for(int j = b - 3; j <= b + 3; ++ j)
            {
                if(abs(i - a) + abs(j - b) <= 3)
                {
                    if(check(i,j,c,d)) {puts("2"); return 0;}
                }
            }
        }

    }

    double x1 = (d + c + a - b) / 2.0 , x2 = (a + b + c -d) / 2.0;
//    cout << x1 << ' ' << x2 << endl;
    if(x1 == (int)x1 || x2 == (int)x2) puts("2");
    else puts("3");
}

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

int main(){
long long r1,r2,c1,c2, a, b;
cin >> r1 >> c1;
cin >> r2 >> c2;
int x;
a=abs(r2-r1); b=abs(c2-c1);
if(a==0&&b==0){x=0;cout<< x <<endl;return 0;}
if((a+b)<4||a-b==0){x=1;cout<< x <<endl;return 0;}
if((a+b)%2==0||abs(a-b)<4){x=2;cout<< x <<endl;return 0;}
x=3; cout<< x <<endl;
return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int64_t INF = INT64_C(1) << 60;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  int64_t X;
  cin >> X;
  vector<int64_t> A(N);
  for (auto &&e : A) {
    cin >> e;
  }
  int64_t res = INF;
  for (int k = 1; k < N + 1; k++) {
    vector<vector<int64_t>> dp(k + 1, vector<int64_t>(k, -INF));
    dp[0][0] = 0;
    for (int i = 0; i < N; i++) {
      for (int j = k; j > 0; j--) {
        for (int r = 0; r < k; r++) {
          if (dp[j - 1][r] < 0) continue;
          dp[j][(r + A[i]) % k] = max(dp[j][(r + A[i]) % k], dp[j - 1][r] + A[i]);
        }
      }
    }
    if (dp[k][X % k] > 0) res = min(res, (X - dp[k][X % k]) / k);
  }
  cout << res << '\n';
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define rep2(i, j, n) for(int i=j;i<(int)(n);i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using P = pair<int, int>;

map<int, ll> table;
int main(){
    int N;
    cin >> N;
    ll C;
    cin >> C;
    rep(i, N){
        int a, b, e;
        cin >> a >> b >> e;
        table[a-1] += e;
        table[b] -= e;
    }
    vector<pair<int, ll>> table2;
    for(pair<int, ll> p : table){
        table2.pb(p);
    }
    sort(all(table2));
    ll rui[table2.size()+1];
    ll sum = 0;
    rui[0] = 0;
    rep(i, table2.size()){
        rui[i+1] = rui[i] + table2[i].second;
        if(i>0 && rui[i]>C){
            sum += (table2[i].first - table2[i-1].first)*C;
        } else if(i>0) {
            sum += (table2[i].first - table2[i-1].first)*rui[i];
        }
    }

    cout << sum << endl;
}


#include <bits/stdc++.h>
inline int read( ) {
	int x = 0;
	char c = getchar( );
	while ( !isdigit( c ) ) c = getchar( );
	while ( isdigit( c ) ) {
		x = x * 10 + ( c ^ 48 );
		c = getchar( );
	}
	return x;
}
const int N = 107, mo = 1e9 + 7;
inline int qp( int b, int p ) {
	int ret = 1;
	while ( p ) {
		if ( p & 1 ) ret = 1ll * ret * b % mo;
		b = 1ll * b * b % mo;
		p >>= 1;
	}
	return ret;
}
int n, m, k, a[ N ], deg[ N ];
struct Matrix {

	int matrix[101][101];

	void clear() { memset(matrix, 0, sizeof(matrix)); }

	void I() { for (int i = 0; i <= 100; ++ i) matrix[i][i] = 1; }

	Matrix operator *(const Matrix &a) const {
		Matrix res;
		res.clear();
		for (int i = 1; i <= n; ++ i) {
			for (int j = 1; j <= n; ++ j) {
				for (int k = 1; k <= n; ++ k) {
					(res.matrix[i][j] += 1ll * matrix[i][k] % mo * a.matrix[k][j] % mo) %= mo;
				}
			}
		}
		return res;
	}

}mat, ret;
int main( ) {
	n = read( ), m = read( ), k = read( );
	for ( int i = 1; i <= n; ++ i ) a[ i ] = read( );
	int INV = qp( 2 * m, mo - 2 );
	for ( int i = 1, x, y; i <= m; ++ i ) {
		x = read( ), y = read( );
		++ deg[ x ];
		++ deg[ y ];
		mat.matrix[ x ][ y ] = mat.matrix[ y ][ x ] = INV;
	}
	for ( int i = 1; i <= n; ++ i ) mat.matrix[ i ][ i ] = 1ll * ( 2 * m - deg[ i ] + mo ) % mo * INV % mo;
	for ( int i = 1; i <= n; ++ i ) ret.matrix[ 1 ][ i ] = a[ i ];
	Matrix ans;
	ans.clear();
	ans.I();
	while (k) {
		if (k & 1) ans = ans * mat;
		k >>= 1;
		mat = mat * mat;
	}
	ret = ret * ans;
	for ( int i = 1; i <= n; ++ i ) printf( "%d\n", ret.matrix[ 1 ][ i ] );
	return 0;
}

#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0, length = n; i < length; i++)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ep emplace
#define epb emplace_back
#define scll static_cast<long long>
#define sz(x) static_cast<int>((x).size())
#define pfll(x) printf("%lld\n", x)
#define ci(x) cin >> x
#define ci2(x, y) cin >> x >> y
#define ci3(x, y, z) cin >> x >> y >> z
#define ci4(w, x, y, z) cin >> w >> x >> y >> z
#define co(x) cout << x << endl
#define co2(x, y) cout << x << " " << y << endl
#define co3(x, y, z) cout << x << " " << y << " " << z << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> PQG;
typedef priority_queue<P> PQP;
typedef priority_queue<P, vector<P>, greater<P>> PQPG;

const int MAX_N = 2e5, MOD = 1e9 + 7, INF = 1e9;
int n, m, k;
vector<int> to[100];
typedef vector<int> vec;
typedef vector<vec> mat;

int po(int x, int y) {
    int ans = 1;
    while (y > 0) {
        if (y & 1) ans = static_cast<long>(ans) * x % MOD;
        x = static_cast<long>(x) * x % MOD;
        y >>= 1;
    }
    return ans;
}

mat mul(mat &A, mat &B) {
    mat C(A.size(), vec(B[0].size()));
    for (int i = 0; i < A.size(); i++) {
        for (int k = 0; k < B.size(); k++) {
            for (int j = 0; j < B[0].size(); j++) {
                C[i][j] = (C[i][j] + scll(A[i][k]) * B[k][j] % MOD) % MOD;
            }
        }
    }
    return C;
}

mat pow(mat A, long n) {
    mat B(A.size(), vec(A.size()));
    for (int i = 0; i < A.size(); i++) {
        B[i][i] = 1;
    }
    while (n > 0) {
        if (n & 1) B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;
}

int main() {
    ci3(n, m, k);
    mat a(n, vec(1));
    rep(i, n) ci(a[i][0]);
    rep(i, m) {
        int x, y;
        ci2(x, y);
        to[x - 1].epb(y - 1);
        to[y - 1].epb(x - 1);
    }
    mat PRO(n, vec(n));
    rep(i, n) {
        rep(j, n) PRO[i][j] = 0;
        for (auto &j: to[i]) PRO[i][j] = 1;
        PRO[i][i] = (2 * m - to[i].size()) % MOD;
    }
    mat POPRO = pow(PRO, k);
    mat ans = mul(POPRO, a);
    int pp = po(po(2 * m, MOD - 2), k);
    rep(i, n) co(scll(ans[i][0]) * pp % MOD);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>  using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll           long long
#define rep(i,j,n)   for(int i=j;i<n;i++)
#define scn(a)       scanf("%d",&a)
#define scns(a,b)    scanf("%d %d",&a,&b)
#define print(a)     printf("%d\n",a)
#define vec          vector<int>
#define pb           emplace_back
#define pairs        pair<int,int>
#define f            first
#define s            second
#define all(v)       v.begin(),v.end()
#define mem(a,b)     memset(a,b,sizeof(a))
#define inf          1e18
const int mod = 1e9+7;
const int N = 5e5+5;

int n;

int main()
{
    int test; test = 1;

    while(test--)
    {
        string s1; cin>>s1;

        s1 = "0" + s1;

        int n = (int)s1.size()-1, st = -1;

        vector<pair<int,int>> v1;

        int mp[n+5][26]; memset(mp, 0, sizeof mp);

        for(int i = 1; i <= n; ++i)
        ++mp[i][s1[i]-'a'];

        for(int i = 1; i <= n; ++i)
        for(int j = 0; j < 26; ++j)
        mp[i][j] += mp[i-1][j];

        for(int i = 1; i <= n-2;)
        {
            if(s1[i] == s1[i+1] && s1[i] != s1[i+2])
            v1.pb(i, s1[i]-'a'), i += 2;
            else
            ++i;
        }

        if((int)v1.size() == 0)
        {
            print(0);

            continue;
        }

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

        ll ans = 0; reverse(all(v1));

        int ind = -1, prev = -1;

        // cout<<n<<endl;

        for(auto [st, ch]:v1)
        {
            // cout<<st<<" ch "<<ch<<endl;

            if(prev == -1)
            {
                // cout<<ch<<endl;

                int val = mp[n][ch] - mp[st+1][ch];

                // cout<<val<<endl;

                ans += n - (st + 1) - val;
            }
            else
            {
                if(prev == ch)
                {
                    // cout<<prev<<" "<<ind<<endl;

                    int val = mp[ind-1][ch] - mp[st+1][ch];

                    // cout<<"VAL "<<val<<endl;

                    ans += n - (st + 1) - val - (n - ind + 1);
                }
                else
                {
                    int val = mp[ind-1][ch] - mp[st+1][ch];

                    ans += n - (st + 1) - val;
                }
            }

            prev = ch, ind = st;

            // cout<<ans<<endl;
        }

        printf("%lld\n",ans);
    }

    return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define REP(i,j) for(int i=0;i<j;i++)
#define REPA(i,j) for(int i=1;i<=j;i++)
#define FORN(i,j,k) for(int i=j;i<k;i++)
#define vi vector<int>
#define vvi vector<vi >
#define pii pair<int,int>
#define vpii vector<pii >
#define all(a) a.begin(),a.end()
using namespace std;
const int INF=1<<30;
inline void read(int &x){
//	short neg=1;
	x=0;
	char c=getchar();
	/*while(c<'0'||c>'9'){
		if(c=='-')neg=-1;
		c=getchar();
	}*/
	while(c>='0'&&c<='9'){
		x=(x<<3)+(x<<1)+(c^48),c=getchar();
	}
//	x*=neg;
}
string s;
int n;
int sum[256]={0};
int main(void){
	cin>>s;
	n=s.size();
	reverse(all(s));
	ll ans=0;
	sum[(int)s[0]]++;
	for(int i=1;i+1<n;i++){
		if(s[i]==s[i+1]){
			ans+=i-sum[(int)s[i]];
			for(char c='a';c<='z';c++)sum[(int)c]=0;
			sum[(int)s[i]]=i+2;
			i++;
		}
		else{
			sum[(int)s[i]]++;
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<vector>
#define IL inline
#define re register
#define LL long long
#define ULL unsigned long long
#ifdef TH
#define debug printf("Now is %d\n",__LINE__);
#else
#define debug
#endif
using namespace std;

template<class T>inline void read(T&x)
{
	char ch=getchar();
	int fu;
	while(!isdigit(ch)&&ch!='-') ch=getchar();
	if(ch=='-') fu=-1,ch=getchar();
	x=ch-'0';ch=getchar();
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	x*=fu;
}
inline int read()
{
	int x=0,fu=1;
	char ch=getchar();
	while(!isdigit(ch)&&ch!='-') ch=getchar();
	if(ch=='-') fu=-1,ch=getchar();
	x=ch-'0';ch=getchar();
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return x*fu;
}
int G[55];
template<class T>inline void write(T x)
{
	int g=0;
	if(x<0) x=-x,putchar('-');
	do{G[++g]=x%10;x/=10;}while(x);
	for(int i=g;i>=1;--i)putchar('0'+G[i]);putchar('\n');
}

int main()
{
	char ch=getchar();
	if(getchar()==ch&&getchar()==ch) cout<<"Won";
	else cout<<"Lost";
	return 0;
}

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

const int mod=1000000007;
const int N=1e5 ;
const int INF=1001001001;

void solve(){

string s ;
cin>>s;

int n=s.length() ;
//cout<<n<<"\n" ;

ll ans=0 ;
map<char,int> mp ;
mp[s.back()]=1 ;

for(int i=n-2;i>=0;i--){

	mp[s[i]]++ ;

	if(s[i]==s[i+1]){
		ans+=n-i-mp[s[i]] ;
		mp=map<char,int>() ;
		mp[s[i]]=n-i ;

	}

//	cout<<i<<" "<<ans<<"\n" ;
}

cout<<ans ;

}

int main(){

	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin) ;
		freopen("output.txt","w",stdout) ;
    #endif

		cin.tie(0);
		cout.tie(0);
		ios_base::sync_with_stdio(false);

		int y ;

		y=1 ;
		//cin>>y ;

		while(y--){

			solve() ;

		}

		return 0 ;
}


#include <bits/stdc++.h>

using namespace std;

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

  int n;
  cin >> n;

  vector<int64_t> a(n), b(n);
  for (auto &ai : a) {
    cin >> ai;
  }
  for (auto &bi : b) {
    cin >> bi;
  }

  int64_t mxa = a[0];
  int64_t c = a[0] * b[0];
  cout << c << '\n';
  for (int i = 1; i < n; ++i) {
    mxa = max(mxa, a[i]);
    c = max(c, mxa * b[i]);
    cout << c << '\n';
  }

  return 0;
}

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

#define boost                         \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define ll long long int
// #define int ll
#define vi vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define pb push_back
#define B begin()
#define E end()
#define sz(A) (ll)(A).size()
#define F first
#define S second
#define loop(z, v) for (auto &z : v)
#define sp << ' ' <<
#define sps << ' '
#define nl '\n'
// #define nl endl
#define fat 1000000007
// #define fat 998244353
#define PI_2 1.57079632679489661923

int main() {
    ll tt = 1, n, q, m, l, r, c, i, j, k, w;
    boost;

    // cin >> tt;
    ll ans = 1;

    for(int _ = 1; _ <= tt; _++){
        cin >> l >> r;

        cout << (l + r) / 2 sp (l - r) / 2 << nl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    cin >> str;
    int n = str.length() - 1;
    char ch = ' ';
    long long ans = 0;
    for (int i = 0; i <=n; i++)
    {
        if (i !=n &&str[i]!=ch&& str[i] == str[i + 1] && str[i] != str[i + 2])
        {
            ans += n - i -1;
            ch = str[i];
            i++;
        }
        else if (ch == str[i])
        {
            ans--;
        }

    }
    cout << ans << "\n";
    return 0;
}

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <complex>
#include <vector>
#include <limits>
#include <iomanip>
#include <cassert>
#include <numeric>
#include <chrono>
#include <random>

using namespace std;

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

#define debug(x) cerr << #x << " = " << (x) << endl;
#define debug_pii(x) cerr << "(" << x.first << "," << x.second << ")";
#define rep(i, n) for(int i = 0;i < n;i++)
#define pb push_back
#define F first
#define S second

// template<typename _Ty1, typename _Ty2>
// ostream& operator<<(ostream& _os, const pair<_Ty1, _Ty2>& _p) {
//     _os << '(' <<_p.first << ',' << _p.second << ')';
//     return _os;
// }
//
// template<typename _Ty1, typename _Ty2>
// istream& operator>>(istream& _is, pair<_Ty1, _Ty2>& _p) {
//     _is >> _p.first >> _p.second;
//     return _is;
// }

void solve() {
  string s;
  cin>>s;
  int n = s.size();
  vector<int> F(26);
  int i = n-3, j = n-2, k = n-1;
  ll ans = 0;
  while (i >= 0) {
    if(s[i] == s[j] && s[j] != s[k]) {
      ans += n-k-1-F[s[i]-'a']+1;
      F.assign(26,0);
      F[s[i]-'a']= n-1-j;
    } else {
      F[s[k]-'a']++;
    }
    i--;j--;k--;
  }
  cout<<ans<<endl;
}

// 0123....i=j!=k......n-1

//         aababbbbb
//         aaaabbbbb
//         aaaaabbbb

// d+1, d+2, d+3, ...,  d'    ,... , M
// 0 [1]
// 1 [1]
// 2 [2,4,8,6,2]
// 3 [3,9,7,1,3]
// 4 [4,6,4]
// 5 [1]
// 6 [1]
// 7 [7,9,3,1,7]
// 8 [8,4,2,6,8]
// 9 [9,1,9]

int main() {

 // freopen("input.in","r",stdin);
 // freopen("output.out","w",stdout);
 // cout << fixed << setprecision(15);2000000000

 ios_base::sync_with_stdio(false);
 cin.tie(nullptr);
 // prime_factors();
 int t = 1;
 // cin >> t;
 while (t--) {
   solve();
 }

 return 0;
}


#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#ifdef __GXX_EXPERIMENTAL_CXX0X__
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;
#define mod 1000000007
#define mod2 998244353
#define pb push_back
#define ll long long
#define fi first
#define lb lower_bound
#define test ll int t;cin>>t;while(t--)
#define se second
#define pll pair<long long,long long>
#define icc ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll gcd(ll a, ll b){
    if(!a)return b;
    return gcd(b%a,a);
}
long long power(long long a,long long b){
    long long ans=1;
    while(b>0){
        if(b&1){ans=(ans*a);}
                a=(a*a);
                b>>=1;
        }
    return ans;
}
vector<ll> spf,prime;
void primes(ll n){
    spf.assign(n+1,0);
    spf[1] = 1;
    for(ll i=2; i<=n; i++){
        if (!spf[i]){
         spf[i]=i;prime.pb(i);
         for (ll j=i*i; j<=n; j+=i)
         if (!spf[j])
         spf[j] = i;
        }
    }
}
ll fact[1000001];
void factorial(ll n){
	fact[0]=1;
	for(ll i=1;i<=n;i++){
		fact[i]=fact[i-1]*i;
		if(fact[i]>=mod)
			fact[i]%=mod;
	}
}
priority_queue<ll,vector<ll>,greater<ll> > qqqqq;

void solve(){
   ll a,b,c;
   cin>>a>>b>>c;
   if(c&1){
       if(a>b)cout<<">";
       else if(a==b)cout<<"=";
       else cout<<"<";
   }
   else{
       if(abs(a)>abs(b))cout<<">";
       else if(abs(a)==abs(b))cout<<"=";
       else cout<<"<";
   }
}
int main(){
    icc;
    //test
    solve();
	return 0;
}
// ll op;
// 	cin>>op;
// 	for(ll fff=1;fff<=op;fff++){

// 		cout<<"Case #"<<fff<<": "<<ans<<endl;
// 	}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	int a, b, c;
	cin >> a >> b >> c;

	if(c % 2 == 0) {
		a = abs(a);
		b = abs(b);
	}

	if(a == b) cout << "=";
	else cout << (a > b ? ">" : "<");
}


#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
    vector<int> vec(3);
    for (int i = 0; i < 3; i++) {
        cin >> vec[i];
    }
    sort(vec.begin(), vec.end(), greater<int>());

    cout << vec[0]+vec[1] << endl;
}

///////////////////////Loading Payloads.......////////////////////////////
#include<bits/stdc++.h>
#define f(i,a,b) for(int i = a; i < b; i++)
#define fd(i,a,b) for(int i = a; i > b;i--)
#define fld(i,a,b) for(ll i = a ; i > b;i--)
#define ll long long
#define fl(i,a,b) for(ll i = a; i < b; i++)
#define VI vector<int>
#define VP vector<pair<int,int>>
#define VPL vector<pair<ll,ll>>
#define VS vector<string>
#define VL vector<long long>
#define VC vector<char>
#define VF vector<float>
#define VD vector<double>
#define VB vector<bool>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define fa(i,skill) for(auto i : skill)
#define fr(i,skill) for(VI :: reverse_iterator i = skill.rbegin(); i != skill.rend(); i++)
#define PI pair<int,int>
#define mi map<int,int>
#define ml map<ll,ll>
#define mci map<char,int>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define MAX 1000000007
#define INF 10000000000000007
#define debug(x) cout<<"This side    ----> "<<#x<<" -> "<<x<<endl
#define printvec(x) fa(a,x)cout << a << " ";cout << "\n";
using namespace std;

///////////////////////Engine Check.......////////////////////////////
void edit(VI &skill,int n)
{
    f(i,0,n)
    {
        int x;
        cin >> x;
        skill.PB(x);
    }
}

bool cmp(pair<ll,ll>p1,pair<ll,ll>p2)
{
    if(p1.S > p2.S)
        return true;
    if(p1.S < p2.S)
        return false;
    if(p1.F > p2.F)
        return true;
    else
        return false;
}
void editl(VL &skill,ll n)
{
    fl(i,0,n)
    {
        ll x;
        cin >> x;
        skill.PB(x);
    }
}
void editpl(VPL &skill,ll n)
{
    fl(i,0,n)
    {
        ll x,y;
        cin >> x >> y;
        skill.PB({x,y});
    }
}
ll nextPowerOf2(ll n)
{
    n--;
    n |= n >> 1;
    n |= n >> 2;
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
    n++;
    return n;
}

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

const ll N = 0;

VL graph[N];

VB vis(N,0);
set<ll> st;

void dfs(ll node)
{
    if(vis[node]) return;

    vis[node] = true;
    st.insert(node);

    // debug(c);
    fa(a,graph[node])
        {
            dfs(a);
        }

}

bool isGoogles = 0;
bool testcase = 0;
void achilles()
{
    VL arr;editl(arr,3);
    sort(rall(arr));

    cout << arr[0] + arr[1] << "\n";
}

///////////////////////3....2....1...Ignition////////////////////////////

int main()
{

    ios :: sync_with_stdio(0);
    cin.tie(0);
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    // seive(N);
    ll t=1,w=1;
    if(testcase)
        cin >>t;
    // t = 2e4;
    while(t--)
        {
    achilles();
    w++;
}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = int64_t;
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)

void solve(long long X, long long Y, long long A, long long B) {

    ll exp_a = 0;
    while (true) {
        if (X > Y / A) break;
        if (X * A >= X + B) break;
        if (X * A >= Y) break;

        X *= A;
        exp_a++;
    }
    ll exp_b = (Y - X - 1) / B;
    cout << exp_a + exp_b << endl;
}

int main() {
    long long X;
    scanf("%lld", &X);
    long long Y;
    scanf("%lld", &Y);
    long long A;
    scanf("%lld", &A);
    long long B;
    scanf("%lld", &B);
    solve(X, Y, A, B);
    return 0;
}

#include <bits/stdc++.h>
#define int long long int
#define ll long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define rep(i, init, n) for (int i = init; i < (int)n; i++)
#define rev(i, n, init) for (int i = (int)n; i >= init; i--)
#define MOD1 1e9 + 7
#define MOD2 998244353
#define f first
#define s second
#define endl '\n'
#define pii pair<int, int>
#define tii tuple<int, int>
#define all(v) v.begin(), v.end()
#define mt make_tuple
#define precise(i) cout << fixed << setprecision(i)
#define codejam cout << "Case #" << ii + 1 << ": ";
#define impossible cout << "IMPOSSIBLE" << endl;
#define error(s) throw runtime_error(s)
#define prev prev1
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int myRand(int R)
{
     int val = rng() % R;
     assert(val >= 0);
     return val;
}

const long double PI = atan(1.0) * 4;
const int32_t INF32 = 2e9 + 7;
const int64_t INF64 = 3e18;
const int32_t LOG = 21;
const int32_t MOD = MOD1;

using namespace std;

//-------------------DEBUGGING-------------------------
void my_debugger(string s, int LINE_NUM) { cerr << endl; }
template <typename start, typename... end>
void my_debugger(string s, int LINE_NUM, start x, end... y)
{
     if (s.back() != ',')
     {
          s += ',';
          cerr << "LINE(" << LINE_NUM << "): ";
     }
     int i = s.find(',');
     cerr << s.substr(0, i) << " = " << x;
     s = s.substr(i + 1);
     if (!s.empty())
          cerr << ", ";
     my_debugger(s, LINE_NUM, y...);
}

#ifdef AJIT
#define debug(...) my_debugger(#__VA_ARGS__, __LINE__, __VA_ARGS__);
#else
#define debug(...) ;
#endif

//-----------------------------------------------------

int32_t main()
{
     getFaster;
     //files_init();
     int tests = 1;
     //cin >> tests;
     rep(ii, 0, tests)
     {
          int n,k;
          cin>>n>>k;
          string s;
          cin>>s;
          vector<int> pow2(k+1,1);
          rep(i,1,k+1)pow2[i]=(pow2[i-1]*2)%n;
          vector<vector<int>> dp(n+1,vector<int>(k+1,0));
          map<char,int> m;
          m['R']=0,m['P']=1,m['S']=2;
          vector<char> convert={'R','P','S'};
          rep(round,0,k+1)
          {
               rep(i,0,n)
               {
                      if(round==0)
                      {
                           dp[i][round]=m[s[i]];
                           continue;
                      }
                      int val1=dp[i][round-1];
                      int val2=dp[(i+pow2[round-1])%n][round-1];
                      if(val1>val2)swap(val1,val2);
                      if(val1==val2 || val1+1==val2)
                      dp[i][round]=val2;
               }
          }
          cout<<convert[dp[0][k]]<<endl;
     }
     return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define vec vector
#define rep(i,n) for (int i=0; i<(n); i++)
#define print(a) cout << a << endl
#define fix(n) fixed << setprecision(n)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define Yes "Yes"
#define YES "YES"
#define No "No"
#define NO "NO"

int main() {
  int N;
  ll X;
  cin >> N >> X;
  X *= 100;
  rep(i,N) {
    ll V,P;
    cin >> V >> P;
    X -= V*P;
    if (X<0) {
      print(i+1);
      return 0;
    }
  }
  print(-1);
  return 0;
}

/*author - Ashutosh Chitranshi*/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << "\n"
#define watch2(x,y) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<"\n"
#define watch3(x,y,z) cout <<(#x)<<" is "<<(x)<<" and "<<(#y)<<" is "<<(y)<<" and "<<(#z)<<" is "<<(z)<<"\n"
#define pow2(x) ((x)*(x))
#define ll long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define pf push_front
#define mod 1000000007
//#define ordered_set tree<pair<ll,ll>,null_type,less<pair<ll,ll>>,rb_tree_tag,tree_order_statistics_node_update>
#define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
#define mp make_pair
#define ff first
#define ss second
#define null NULL
#define all(c) (c).begin(),(c).end()
#define nl "\n"
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector< vi > vvi;
typedef vector< vl > vvl;
typedef pair< int,int > ii;
typedef pair< ll,ll> pll;
typedef map< ll,ll> mll;
typedef map< int,int> mii;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll a,b,c,d;
    cin >> a >> b>>c>>d;
    cout << min({a,b,c,d}) << nl;
    return 0;
}


/*AUTHOR - LAKSHYA JAIN*/
#include <bits/stdc++.h>
using namespace std;
#define ll                long long
#define pb                push_back
#define vi                vector<int>
#define vl                vector<long long>
#define vll               vector<pair<long long,long long>>
#define vii               vector<pair<int,int>>
#define vvi               vector<vector<int>>
#define maxq(x)           priority_queue<x>
#define minq(x)           priority_queue<x,vector<x>,greater<x>>
#define graph             map<int,vi>
#define vvl               vector<vector<long long>>
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define f                 first
#define s                 second
#define pii               pair<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define rrep(i,a,b)       for(int i = a;i>=b;i--)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll
# define quick ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
vvl dp;
string solve(int a,int b,ll k)
{
    if(a == 0)
    {
        string temp(b,'b');
        return temp;
    }
    if(b == 0)
    {
        string temp(a,'a');
        return temp;
    }
    if(dp[a - 1][b]>=k)
    return 'a' + solve(a - 1,b,k);
    return 'b' + solve(a,b - 1,k - dp[a - 1][b]);
}
int main()
{
  quick
  int T = 1;
  //cin>>T;
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif
  while(T--)
  {
    int a,b;
    ll k;
    cin>>a>>b>>k;
    dp = vvl(a + 1,vl(b + 1,0));
    dp[0][0] = 1;
    rep(i,0,a + 1)
    {
        rep(j,0,b + 1)
        {
            if(i>0)
            dp[i][j] += dp[i - 1][j];
            if(j>0)
            dp[i][j] += dp[i][j - 1];
        }
    }
    cout<<solve(a,b,k);
  }
  return 0;

}

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>
#include <chrono>
#include <random>
#include <functional>

using namespace std;

#define int long long

const int N = 70;
const int INF = 1e18 + 239;

int C[N][N];

void init() {
    for (int i = 0; i < N; i++) {
        C[i][0] = 1;
        C[i][i] = 1;
        for (int j = 1; j < i; j++) {
            C[i][j] = min(INF, C[i - 1][j] + C[i - 1][j - 1]);
        }
    }
}

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

    init();

    int a, b, k;
    cin >> a >> b >> k;
    k--;
    while (a > 0 || b > 0) {
        if (a == 0) {
            cout << 'b';
            b--;
            continue;
        }
        if (b == 0) {
            cout << 'a';
            a--;
            continue;
        }
        if (C[a + b - 1][a - 1] <= k) {
            k -= C[a + b - 1][a - 1];
            cout << 'b';
            b--;
        } else {
            cout << 'a';
            a--;
        }
    }
    cout << '\n';
}


/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
 #define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);}
#else
 #define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1<<20;
#define UnionFindSIZE 1234567
map<int,int>AA[UnionFindSIZE];
int map_id[UnionFindSIZE];
struct Union_Find{
    int d[UnionFindSIZE],num[UnionFindSIZE];
    void init(int n){
        REP(i,n)d[i]=i,num[i]=1,map_id[i]=i;
    }
    int find(int x){
        int y = x, z = x;
        while(y != d[y]) y = d[y];
        while(x != y){
            x = d[x];
            d[z] = y;
            z = x;
        }
        return y;
    }
    bool is_root(int x){return d[x]==x;}
    bool uu(int x,int y){
        x=find(x);
        y=find(y);
        if(x==y)return 0;
        if(num[x]>num[y])swap(x,y);
        if(SZ(AA[map_id[x]])>SZ(AA[map_id[y]])){
            swap(map_id[x],map_id[y]);
        }
        for(auto v:AA[map_id[x]]){
            AA[map_id[y]][v.F]+=v.S;
        }
        num[y]+=num[x];
        d[x]=y;
        return 1;
    }
}U;
int N,Q;
int C[SIZE];
void solve() {
    REP(i,Q){
        int ty;
        R(ty);
        if(ty==1){
            int a,b;
            R(a,b);
            a--;b--;
            U.uu(a,b);
        }else{
            int x,y;
            R(x,y);
            x--;
            W(AA[map_id[U.find(x)]][y]);
        }
    }
}
void input() {
    R(N,Q);
    U.init(N);
    REP(i,N){
        R(C[i]);
        AA[i][C[i]]=1;
    }
}
int main(){
    input();
    solve();
    return 0;
}

#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#define N 200008
using namespace std;
int a[N],f[N];
map<int,int>mp[N];
int find(int);
int main()
{
	map<int,int>::iterator k;
	int i,n,q,typ,x,y;
	scanf("%d %d",&n,&q);
	for(i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		f[i]=i;mp[i][a[i]]=1;
	}
	while(q--)
	{
		scanf("%d %d %d",&typ,&x,&y);
		if(typ==1)
		{
			x=find(x);y=find(y);
			if(x==y)continue;
			if(mp[x].size()<mp[y].size())swap(x,y);
			for(k=mp[y].begin();k!=mp[y].end();++k)
				mp[x][k->first]+=k->second;
			f[y]=x;
		}
		else
		{x=find(x);printf("%d\n",mp[x][y]);}
	}
	return 0;
}
int find(int u)
{
	int i=u,j;
	while(i!=f[i])i=f[i];
	while(u!=f[u])
	{
		j=u;
		u=f[u];
		f[j]=i;
	}
	return i;
}


#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

int main() {
  string s;
  cin >> s;
  int n = SZ(s);
  vector<vector<int>> cnt(n + 1, vector<int>(26));
  for (int i = 0; i < n; ++i) {
    cnt[i + 1] = cnt[i];
    int k = s[i] - 'a';
    cnt[i + 1][k]++;
  }
  int64 ret = 0;
  int pre = -1, pos = n;
  for (int i = n - 2; i >= 0; --i) {
    if (s[i] == s[i + 1] && s[i] != s[i + 2]) {
      int j = i + 2;
      int k = s[i] - 'a';
      ret += (pos - j) - (cnt[pos][k] - cnt[j][k]);
      if (k != pre) {
        ret += n - pos;
      }
      pre = k;
      pos = i;
    }
  }
  cout << ret << '\n';

  return 0;
}

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

const int N = 1e5 + 10;

long long n;
unordered_set<long long> st;

int main()
{
    long long ans = 0;
    cin >> n;
    for (int i = 2; ; ++i)
    {
        if ((long long)round(pow(i, 2)) > n)
            break;
        for (int j = 2; ; ++j)
        {
            if ((long long)round(pow(i, j)) > n)
                break;
            st.insert((long long)round(pow(i, j)));
        }
    }

    cout << n - st.size() << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define ff first
#define ss second
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define d(a,b,c) cout<<a<<" "<<b<<" "<<c<<'\n'
#define IO ios_base::sync_with_stdio(0);cin.tie(0);

int main()
{
    IO;
	string s; cin >> s;
	char lstc = ' ';
	int lst = 0;
	ll ans = 0;
	int cnt[26];
	memset(cnt, 0, sizeof(cnt));

	for(int i = s.size()-1, cntin = 0; i >= 0; i--)
	{
		cnt[s[i]-'a']++;
		cntin++;
		if(i < s.size()-1 and s[i] == s[i+1])
		{
			for(int j = 0; j < 26; j++)
			{
				if(int(s[i]-'a') != j)
				{
					ans += cnt[j];
					cnt[j] = 0;
				}
				else cnt[j] = cntin;
			}
		}
	}
	cout << ans << endl;
	return 0;
}

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

const int INF = 1001001001;

int N;
vector<int> A;
vector<pair<int,int>> X;
vector<int> Side;
vector<int> Stacks[2];
string ans;

int main(){
  cin >> N; N*=2;
  vector<int> A(N); rep(i,N) cin >> A[i];
  rep(i,N) X.push_back({A[i],i});
  sort(X.begin(),X.end());
  Side.resize(N);
  rep(i,N) Side[X[i].second] = i / (N/2);
  ans = string(N,'.');
  rep(i,N){
    if(Stacks[1-Side[i]].empty()){
      Stacks[Side[i]].push_back(i);
    }
    else{
      int opposite = Stacks[1-Side[i]].back();
      Stacks[1-Side[i]].pop_back();
      ans[opposite] = '(';
      ans[i] = ')';
    }
  }
  cout << ans << "\n";
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = n-1; i >= 0; i--)
#define  all(x) (x).begin(),(x).end()     // 昇順ソート
#define  rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define  FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define sz(x) ((int)(x).size())
typedef long long ll;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VP = vector<P>;
template<typename T> void view(T e){std::cout << e << std::endl;}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; }

const int inf = 1 << 30;
const ll INF = 1LL << 60;

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

int main(){
    int h, w, n, m;
    cin >> h >> w >> n >> m;
    VVI mp(h, VI(w));
    VP L(n);
    rep(i,n){
        int a, b;
        cin >> a >> b;
        a--; b--;
        L[i].first = a, L[i].second = b;
    }
    rep(i, m){
        int c, d;
        cin >> c >> d;
        c--; d--;
        mp[c][d] = -1;
    }
    vector<vector<vector<bool>>> visited(h, vector<vector<bool>>(w, vector<bool>(4)));
    auto check = [&](int x, int y){
        if (x < 0 || x >= w || y < 0 || y >= h) return false;
        else return true;
    };
    rep(li, n){
        int row = L[li].first, col = L[li].second;
        rep(i, 4){
            if (visited[row][col][i]) continue;
            visited[row][col][i] = true;

            int sy = row + dy[i], sx = col + dx[i];
            while(check(sx, sy)){
                if (mp[sy][sx] == -1 || visited[sy][sx][i]) break;
                visited[sy][sx][i] = true;
                sy += dy[i], sx += dx[i];
            }
        }
    }
    int ans = 0;
    rep(row, h) rep(col, w){
        bool ok = false;
        rep(i, 4) if (visited[row][col][i]) ok = true;
        if (ok) ans++;
    }
    view(ans);
    return 0;
}

//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#define int long long
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())

#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

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

template<class t> using vc=vector<t>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 1000000007;//998244353
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
template<class T> using mat = vc<vc<T>>;
mat<ll> make_e(int n){
	mat<ll>ret(n, vc<ll>(n));
	rep(i, n) ret[i][i] = 1;
	return ret;
}
template<class T>
mat<T> operator+(mat<T> a, mat<T> b){
	assert(a.size() == b.size());
	if(a.size()) assert(a[0].size() == b[0].size());

	rep(i, a.size()) rep(j, a[0].size()){
		a[i][j] += b[i][j];
		if(a[i][j] >= mod) a[i][j] -= mod;
	}

	return a;
}
template<class T>
mat<T> operator-(mat<T> a, mat<T> b){
	assert(a.size() == b.size());
	if(a.size()) assert(a[0].size() == b[0].size());

	rep(i, a.size()) rep(j, a[0].size()){
		a[i][j] += mod - b[i][j];
		if(a[i][j] >= mod) a[i][j] -= mod;
	}

	return a;
}
template<class T>
mat<T> operator*(mat<T> a, mat<T> b){
	if(a.empty() || b.empty()) return mat<T>();
	int n = a.size();
	int m = b[0].size();
	int len = a[0].size();
	assert(len == b.size());
	mat<T>ret(n);
	rep(i, n) ret[i] = vc<T>(m);

	rep(i, n) rep(j, len) rep(k, m){
		ll add = (ll)(a[i][j]) * b[j][k];
		add %= mod;
		ret[i][k] += add;
		if(ret[i][k] >= mod) ret[i][k] -= mod;
	}
	return ret;
}

void solve(){
	int n, m, k;
	cin >> n >> m >> k;

	mat<ll>M = make_e(n);
	vc<ll>num(n);
	rep(i, n) cin>>num[i];
	ll rv = modpow(2*m, mod-2);
	rep(i, m){
		int a, b; cin >> a >> b;
		a--; b--;
		M[a][a] -= rv;
		M[b][b] -= rv;
		M[a][b] += rv;
		M[b][a] += rv;
	}
	rep(i, n) rep(j, n) M[i][j] = (M[i][j]%mod+mod)%mod;

	mat<ll>ret(n, vc<ll>(n));
	rep(i, n) ret[i][i] = 1;
	while(k){
		if(k&1) ret = ret*M;
		k >>= 1;
		M = M*M;
	}
	mat<ll>ans(n, vc<ll>(1));
	rep(i, n) ans[i][0] = num[i];
	auto result = ret*ans;
	rep(i, n) o(result[i][0]);
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}


#include <cstdio>
#include <cstdlib>
using namespace std;
#define ll long long
const ll MAXN = 400011;

struct fhqTreap {
	ll tot, rt;
	ll val[MAXN], sum[MAXN], sz[MAXN], ls[MAXN], rs[MAXN], rd[MAXN];

	void pushup(ll u) {sz[u] = sz[ls[u]] + sz[rs[u]] + 1; sum[u] = sum[ls[u]] + sum[rs[u]] + val[u];}

	void split(ll u, ll k, ll &x, ll &y) {
		if(!u) {x = y = 0; return;}
		if(val[u] <= k) x = u, split(rs[u], k, rs[u], y);
		else y = u, split(ls[u], k, x, ls[u]);
		pushup(u);
	}

	ll merge(ll u, ll v) {
		if(!u || !v) return u | v;
		if(rd[u] < rd[v]) {rs[u] = merge(rs[u], v); pushup(u); return u;}
		else {ls[v] = merge(u, ls[v]); pushup(v); return v;}
	}

	ll add(ll k) {
		rd[++tot] = rand();
		sz[tot] = 1;
		val[tot] = sum[tot] = k;
		return tot;
	}

	void insert(ll k) {
		ll x, y;
		split(rt, k, x, y);
		rt = merge(merge(x, add(k)), y);
	}

	ll cb(ll k) {
		ll x, y;
		split(rt, k, x, y);
		ll ans = sz[x] * k + sum[y];
		rt = merge(x, y);
		return ans;
	}

	void delet(ll k) {
		ll x, y, z;
		split(rt, k, x, z);
		split(x, k - 1, x, y);
		y = merge(ls[y], rs[y]);
		rt = merge(merge(x, y), z);
	}
} t[2];

ll q;
ll len[2];
ll w[2][MAXN];

ll read() {
	ll X = 0, F = 1; char C = getchar();
	while(C < '0' || C > '9') {if(C == '-')F=-1; C = getchar();}
	while(C >= '0' && C <= '9') {X = X*10+C-'0'; C = getchar();}
	return X * F;
}

int main() {
	ll ans = 0;
	len[1] = read(); len[0] = read(); q = read();
	for(ll i = 0; i < 2; ++i)
		for(ll j = 1; j <= len[i]; ++j)
			t[i].rt = t[i].merge(t[i].rt, t[i].add(0));
	for(ll i = 1, opt, x, y; i <= q; ++i) {
		opt = (read() & 1); x = read(); y = read();
		t[opt].delet(w[opt][x]);
		t[opt].insert(y);
		ans -= t[opt^1].cb(w[opt][x]);
		w[opt][x] = y;
		ans += t[opt^1].cb(w[opt][x]);
		printf("%lld\n", ans);
	}
	return 0;
}

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

struct BIT {
    vector<int64_t> t;
    BIT (int n) : t(n, 0) {}
    void update(int i, int64_t x) {
        for (; i < int(t.size()); i |= i + 1) {
            t[i] += x;
        }
    }
    int64_t query(int i) {
        int64_t res = 0;
        for (; i > 0; i &= i - 1) {
            res += t[i - 1];
        }
        return res;
    }
};

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n[2], q;
    cin >> n[0] >> n[1] >> q;
    vector<array<int, 3>> queries(q);
    vector<int> nums(1, 0);
    nums.reserve(q + 1);
    for (auto& it : queries) {
        cin >> it[0] >> it[1] >> it[2];
        it[0]--;
        it[1]--;
        nums.push_back(it[2]);
    }
    sort(nums.begin(), nums.end());
    nums.erase(unique(nums.begin(), nums.end()), nums.end());

    int sz = nums.size();
    int64_t ans = 0;
    vector<int> v[2];
    v[0].resize(n[0]);
    v[1].resize(n[1]);
    vector<BIT> cnt(2, BIT(sz)), sum(2, BIT(sz));
    cnt[0].update(0, n[0]);
    cnt[1].update(0, n[1]);
    for (auto qq : queries) {
        int a = qq[0], b = qq[0] ^ 1;
        int before = v[a][qq[1]];
        int id = lower_bound(nums.begin(), nums.end(), before) - nums.begin();
        ans -= int64_t(before) * cnt[b].query(id);
        ans += sum[b].query(id);
        int after = qq[2];
        int nid = lower_bound(nums.begin(), nums.end(), after) - nums.begin();
        ans -= sum[b].query(nid);
        ans += int64_t(after) * cnt[b].query(nid);
        cnt[a].update(id, -1);
        sum[a].update(id, -before);
        cnt[a].update(nid, 1);
        sum[a].update(nid, after);
        v[a][qq[1]] = after;
        cout << ans << '\n';
    }
    return 0;
}