#include<bits/stdc++.h>
//#include <atcoder/all>

#define INF 1e9
#define rep(i,n)for(long long i=0;(i)<(long long)(n);i++)
#define REP(i,a,b)for(long long i=(long long)(a);(i)<=(long long)(b);i++)
#define ALL(a)  (a).begin(),(a).end()
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define pb push_back

using namespace std;
//using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
//using mint = modint1000000007;

const ll ZER = 0;
const ll MOD = 1e9 + 7;

map<ll, ll> memo;
ll x, y;
// y -> x
ll rec(ll n){
    if(memo.find(n) != memo.end())return memo[n];
    if(n <= x)return x - n;
    ll res = n - x;
    chmin(res, rec(n / 2) + (n % 2) + 1);
    if(n % 2 != 0)chmin(res, rec(n / 2 + 1) + (2 - (n % 2)) + 1);
    return memo[n] = res;
}

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

#include "bits/stdc++.h"
#define rep(i,b) for(ll i=0;i<b;i++)
#define ll long long
using namespace std;
/*--Input//////////////////////////////////////////////////*/
inline void IN(void){return;}
template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
    cin >> first;
    IN(rest...);
    return;
}
#define SS(T, ...)      T __VA_ARGS__; IN(__VA_ARGS__);
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
/*--Output/////////////////////////////////////////////////*/
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
inline int p(void){cout << endl; return 0;}
template<class Head> int p(Head&& head){cout << head;p();return 0;} template<class Head,class... Tail> int p(Head&& head,Tail&&... tail){cout<<head<<" ";p(forward<Tail>(tail)...);return 0;}
struct ProconInit {
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;

    ProconInit() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(IOS_PREC);
        if(AUTOFLUSH)
            cout << unitbuf;
    }
} PROCON_INIT;

#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr<<#x<<" "<<x<<'\n';
#define all(x) (x).begin(),(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 (b<a) { a=b; return 1; } return 0; }
bool cmp(pair<ll,ll> a, pair<ll,ll> b) { return a.second < b.second; }

bool P[200001];

int main() {
ll now =0;
SS(ll,n);
rep(i,n){
    SS(ll,po);
    P[po]=true;
    for(ll j=now;;j++){
        if(P[j]==false){
            now=j;
            p(now);
            break;
        }
    }
}
}


#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int MAX = 10010;
class shuturyoku_unko {
public:
	char C[MAX * 6];
	constexpr shuturyoku_unko() : C() {
		rep(i, MAX) {
			int X = i;
			rep(j, 5) {
				C[i * 6 + 4 - j] = '0' + X % 10;
				X /= 10;
			}
			C[i * 6 + 5] = ' ';
		}
	}
};
constexpr shuturyoku_unko f;

char T[800], *ci = T, c, *di;
int N, A[110], B[110];
bool furagu[10001];
inline void keisan(int s) {
	di = T;
	rep(i, N + 1) {
		int tmp = A[i] + (B[i] > s);
		int tmp2;
		if (tmp >= 1000) {
			if (tmp >= 10000) tmp2 = 6;
			else tmp2 = 5;
		}
		else {
			if (tmp >= 100) tmp2 = 4;
			else if (tmp >= 10) tmp2 = 3;
			else tmp2 = 2;
		}
		memcpy(di, f.C + tmp * 6 + 6 - tmp2, tmp2);
		di += tmp2;
	}
}

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

	fread(T, 800, 1, stdin);
	while ((c = *ci++) >= '0') N = N * 10 + c - '0';
	ci += N + 1;
	rep(i, N + 1) {
		while ((c = *ci++) >= '0') A[i] = A[i] * 10 + c - '0';
	}

	int saishou = 1e9;
	rep(i, N) if (saishou > abs(A[i] - A[i + 1])) saishou = abs(A[i] - A[i + 1]);

	rep(i, N + 1) {
		B[i] = A[i] % saishou;
		A[i] = A[i] / saishou;
		furagu[B[i]] = 1;
	}

	printf("%d\n", saishou);
	keisan(0);
	rep(s, saishou) {
		if (furagu[s]) keisan(s);
		fwrite(T, 1, di - T, stdout);
	}

	Would you please return 0;
}

// author : ytczy666
// 21.6.29 10:09
#include <bits/stdc++.h>

const int N = 110;

#define inf 0x3f3f3f3f
int n, a[N], b[N * N][N];

char str[N];

int main() {
	scanf("%d", &n);
	scanf("%s", str + 1);
	for (int i = 0; i <= n; i ++) scanf("%d", &a[i]);
	int k = inf;
	for (int i = 1; i <= n; i ++) k = std::min(k, abs(a[i] - a[i - 1]));
	printf("%d\n", k);
	for (int j = 0; j <= n; j ++) {
		int d = a[j] / k, m = a[j] % k;
		for (int i = 1; i <= k; i ++) {
			if (i <= m) b[i][j] = d + 1;
			else b[i][j] = d;
		}
	}
	for (int i = 1; i <= k; i ++, puts(""))
		for (int j = 0; j <= n; j ++)
			printf("%d ", b[i][j]);
	return 0;
}

// 考虑最优答案最多是k=min{d_i}，再想是否一定可以构造出这样一组方案。
// 我们直接就考虑平均分配，为什么这么想，因为我们要让各部分都满足整体的关系。
// 而因为k=min{d_i},所以一定不会出现相邻两个数相等的情况，否则就把前几个数+1


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

int main() {
  int n, k;
  cin >> n >> k;

  cout << 100*n*(n+1)*k/2+n*k*(k+1)/2 << endl;
}

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define MAXN 100005
#define lowbit(x) (x&-x)
#define reg register
typedef long long LL;
typedef unsigned long long uLL;
typedef unsigned int uint;
typedef pair<int,int> pii;
const LL INF=0x7f7f7f7f7f7f;
const int mo=998244353;
const double PI=acos(-1.0);
template<typename _T>
_T Fabs(_T x){return x<0?-x:x;}
template<typename _T>
void read(_T &x){
	_T f=1;x=0;char s=getchar();
	while(s>'9'||s<'0'){if(s=='-')f=-1;s=getchar();}
	while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}
	x*=f;
}
int n,d,h;double maxx;
signed main(){
	read(n);read(d);read(h);
	for(int i=1;i<=n;i++){
		int x,y;read(x);read(y);
		double tmp=1.0*y-1.0*(x)/(d-x)*(h-y);
		maxx=max(maxx,tmp);
	}
	printf("%.3f\n",maxx);
	return 0;
}


// E - Unique Color
#include <bits/stdc++.h>
using namespace std;
#define vec vector
using vi = vec<int>;
#define rep(i,e) for(int i=0;i<(e);++i)

int n;
vi C, ans;
vec<vi> G;
map<int,int> cc;

void dfs(int v){
	if(ans[v] > 0) return;
	cc[C[v]]++;
	ans[v] = cc[C[v]];
	for(auto&u:G[v]) dfs(u);
	cc[C[v]]--;
}

int main(){
	cin>>n;
	C.resize(n);for(auto&c:C) cin>>c;

	G.resize(n);
	rep(i, n-1){
		int a, b; cin>>a>>b; --a, --b;
		G[a].push_back(b);
		G[b].push_back(a);
	}

	ans.resize(n, -1);
	dfs(0);
	rep(i, n) if(ans[i] == 1) cout<< i+1 <<endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>
// #include "atcoder/all"

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T>
bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}

template <int B = 20>
struct Mex{
    vector<vector<int>> v;
    vector<int> cnt;
    Mex() : v(B), cnt(1 << B, 0){
        for(int i = 0; i < B; ++i){
            int siz = 1 << (B - i - 1);
            v[i].resize(siz);
            for(int j = 0; j < siz; ++j){
                v[i][j] = j << i;
            }
        }
    }
    void inc(int x){
        if(cnt[x]++ == 0){
            v[0][x] = (1 << (B - 1));
            for(int i = 1; i < B; ++i){
                v[i][x >> i] = min(v[i - 1][(x >> i) * 2], v[i - 1][(x >> i) * 2 + 1]);
            }
        }
    }
    void dec(int x){
        if(--cnt[x] == 0){
            v[0][x] = x;
            for(int i = 1; i < B; ++i){
                v[i][x >> i] = min(v[i - 1][(x >> i) * 2], v[i - 1][(x >> i) * 2 + 1]);
            }
        }
    }
    int get(){
        return v[B - 1][0];
    }
};

void solve(long long n, long long m, std::vector<long long> a){

    int ans = n;
    Mex<22> d;
    for(int i = 0; i < n; ++i){
        d.inc(a[i]);
        if(i >= m){
            d.dec(a[i - m]);
        }
        if(i >= m - 1){
            ans = min(ans, d.get());
        }
    }
    cout << ans << endl;
}

signed main(){
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, M, std::move(A));
    return 0;
}


//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define pb push_back
#define F first
#define S second
//#pragma GCC optimize("Ofast")

const int maxn = 1e5 + 5;

ll x, ans = 1e18 + 5;
map<ll, int> ca;

void calc(ll y)
{
	queue<pair<ll, int> > q;
	q.push({y, 0});
	while(q.size())
	{
		auto b = q.front();
		q.pop();
		int t = b.S;
		if(ca.count(b.F)&& ca[b.F] < t)
			continue;
		ca[b.F] = t;
		y = b.F;
		ans = min(ans, abs(y - x) + t);
		if(y % 2 == 0)
			q.push({y / 2, t + 1});
		else
		{
			q.push({y / 2, t + 2});
			q.push({y / 2 + 1, t + 2});
		}
	}
	/*if(ca.count(y))
		return;
	ca.insert(y);
	ans = min(ans, abs(y - x) + t);
	if(y == 0)
		return;
	if(y % 2 == 0)
		calc(y / 2, t + 1);
	else
	{
		calc(y / 2, t + 2);
		calc(y / 2 + 1, t + 2);
	}*/
}

int main()
{
	//ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	ll y;
	cin >> x >> y;
	if(x >= y)
	{
		cout << x - y << '\n';
		return 0;
	}
	else
	{
		calc(y);
		cout << ans << '\n';
	}
}

#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scanf("%c",&a)
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 1e3 + 10;
int n, m;
int a[N], b[N];
int f[N][N];

int main() {
    n = qr(), m = qr();
    repi(i, 1, n)a[i] = qr();
    repi(i, 1, m)b[i] = qr();
    memset(f, 0x3f, sizeof(f));
    f[0][0] = 0;
    int ans = INF;
    repi(i, 0, m)f[0][i] = i;
    repi(i, 0, n)f[i][0] = i;
    repi(i, 1, n)
        repi(j, 1, m) {
            if (a[i] != b[j]) {
                f[i][j] = min({f[i][j], f[i - 1][j - 1] + 1, f[i][j - 1] + 1, f[i - 1][j] + 1});
            } else {
                f[i][j] = min({f[i][j], f[i - 1][j - 1], f[i][j - 1] + 1, f[i - 1][j] + 1});
            }
        }
    repi(i, 1, n)repi(j, 1, m)ans = min(ans, f[i][j] + n - i + m - j);
    pi(ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ~ ";
	dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {";  \
	for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
struct UnionFind{
	vector<int> par,rank,hasL,hasR,marked;
	UnionFind(int N){
		par.assign(N,0);
		rank.assign(N,0);
		hasL.resize(N);hasR.resize(N);marked.resize(N);
		rep(i,N) par[i]=i;
	}
	int find(int x){
		if(par[x]==x) return x;
		return par[x]=find(par[x]);
	}
	bool same(int x,int y){
		return find(x)==find(y);
	}
	void unite(int x,int y){
		x=find(x),y=find(y);
		if(x==y) return;
		if(rank[x]<rank[y]) swap(x,y);

		//x becomes root
		par[y]=x;
		if(rank[x]==rank[y]) rank[x]++;
		hasL[x] += hasL[y];
		hasR[x] += hasR[y];
		marked[x] += marked[y];
	}
};
int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);

	int H,W; cin >> H >> W;
	V<string> s(H); rep(i,H) cin >> s[i];

	UnionFind UF(H+W);
	rep(i,H) UF.hasL[i] = 1;
	rep(i,W) UF.hasR[i+H] = 1;
	UF.marked[0] = true;
	rep(i,H) rep(j,W) if(s[i][j] == '#'){
		UF.unite(i,j+H);
	}
	for(int i:{0,H-1}) for(int j:{0,W-1}) UF.unite(i,j+H);
	int ans = 1e9;
	{
		int waf = 0;
		rep(v,H+W) if(UF.find(v) == v && UF.hasL[v] && !UF.marked[v]) waf++;
		chmin(ans,waf);
	}
	{
		int waf = 0;
		rep(v,H+W) if(UF.find(v) == v && UF.hasR[v] && !UF.marked[v]) waf++;
		chmin(ans,waf);
	}
	cout << ans << endl;
}

//starusc
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
inline int read(){
	int x=0,f=1,c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
	return f==1?x:-x;
}
const int M=1004,N=1e6+4;
struct edge{
	int v,nxt;
}e[N<<2];
int first[N],cnt;
inline void addedge(int u,int v){
	e[++cnt].v=v;e[cnt].nxt=first[u];first[u]=cnt;
}
struct node{
	int x,y,id;
}a[N];
inline bool comp_x(const node &a,const node &b){
	return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
inline bool comp_y(const node &a,const node &b){
	return a.y<b.y||(a.y==b.y&&a.x<b.x);
}
inline bool comp_id(const node &a,const node &b){
	return a.id<b.id;
}
int n,m,tot,cx,cy,rx,ry,vx[M],vy[M],vis[N];
char s[M];
void dfs(int x){
	vis[x]=1;
	if(!vx[a[x].x])cx++;
	if(!vy[a[x].y])cy++;
	vx[a[x].x]=vy[a[x].y]=1;
	for(int i=first[x];i;i=e[i].nxt)
		if(!vis[e[i].v])
			dfs(e[i].v);
}
int main(){
	n=read();m=read();
	for(int i=1;i<=n;i++){
		scanf("%s",s+1);
		for(int j=1;j<=m;j++){
			if(s[j]=='#'){
				a[++tot].x=i;a[tot].y=j;a[tot].id=tot;
			}
		}
	}
	sort(a+1,a+tot+1,comp_x);
	for(int i=1;i<=tot;i++){
		if(a[i].x==a[i-1].x){
			addedge(a[i].id,a[i-1].id);
			addedge(a[i-1].id,a[i].id);
		}
	}
	sort(a+1,a+tot+1,comp_y);
	for(int i=1;i<=tot;i++){
		if(a[i].y==a[i-1].y){
			addedge(a[i].id,a[i-1].id);
			addedge(a[i-1].id,a[i].id);
		}
	}
	rx=n-2;ry=m-2;
	vx[1]=vx[n]=vy[1]=vy[m]=1;
	sort(a+1,a+tot+1,comp_id);
	for(int i=1;i<=tot;i++)if(!vis[i]&&(a[i].x==1||a[i].y==1||a[i].x==n||a[i].y==m)){
		cx=cy=0;
		dfs(i);
		rx-=cx;ry-=cy;
	}
	for(int i=1;i<=tot;i++)if(!vis[i]){
		cx=cy=0;
		dfs(i);
		rx-=cx-1;ry-=cy-1;
	}
	cout<<min(rx,ry)<<"\n";
	return (0-0);
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL<<60;
const double PI = acos(-1.0);
/*const double PI = atan2(0.0,-1.0)*/
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rep1(i,aa,n) for(ll i = aa; i <= (ll)(n); i++)
#define ALL(a)  (a).begin(),(a).end()
#define ar_init(aa,val) rep(i,aa.size()) aa[i]=val;
#define v2d(aa,val1,val2,ini) vector<vector<ll>> aa(val1,vector<ll>(val2,ini));
#define pqe priority_queue<ll>
#define pqeg priority_queue<ll,vector<ll>,greater<ll>>
#define lcin(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define eout(...) COU(__VA_ARGS__);
#define sout(...) SCOU(__VA_ARGS__);
#define scin(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define lb(aa,val) lower_bound(ALL(aa),val)
#define pb push_back
#define mkp make_pair
#define si size()

void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
    cin>>head;
    CINT(move(tail)...);
}

void COU(){}
template <class Head,class... Tail>
void COU(Head&& head,Tail&&... tail){
    cout<<head<<endl;
    COU(move(tail)...);
}

void SCOU(){}
template <class Head,class... Tail>
void SCOU(Head&& head,Tail&&... tail){
    cout<<head<<" ";
    SCOU(move(tail)...);
}

template <class T = ll>
T IN(){T x;cin>>x;return (x);}

using pii = pair<ll,ll>;
using v2 = vector<vector<ll>>;
using v1 = vector<ll>;
using v2p = vector<vector<pii>>;
using v1p = vector<pii>;
//ll mo=1000000007;
ll mo=998244353;

ll mod_pow(ll N,ll n){
if(n==0) return 1;
ll pp,hh;
pp=n/2;
hh=n%2;
int tt=mod_pow(N,n/2);
if(hh==0)return tt%mo*tt%mo;
else {return tt%mo*tt%mo*N%mo;}
}

ll pp[6000][6000];
int main(){
//cout<<fixed<<setprecision(15);

lcin(n,m);

rep1(i,1,m){
pp[i][0]=1;
rep1(k,1,n){
pp[i][k]=pp[i][k-1]*i;
pp[i][k]%=mo;
}
}

ll res=0;

rep1(i,1,m){
rep1(k,1,n){
if(k+2<=n){res+=pp[m][n-k-2]*(pp[m-i+1][k]-pp[m-i][k])%mo*max((ll)0,n-k+1-2)*(i-1)%mo*(i-1)%mo;
}
if(k!=n){res+=2*pp[m][max((ll)0,n-k-1)]*(pp[m-i+1][k]-pp[m-i][k])%mo*(i-1)%mo;
}
if(k==n){res+=pp[m-i+1][k]-pp[m-i][k];
res%=mo;
}

}
}

eout(res);
}

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__)
#define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();}

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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector <int> vi;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
constexpr ll nax = 2e5+6969, INF = 2e9+6969, mod = 998244353;

ll Pow(ll a, ll b)
{
	if(b < 0) return 0;
	ll w = 1;
	while(b)
	{
		if(b&1) w = (w * a) % mod;
		a = (a * a) % mod;
		b >>= 1;
	}
	return w;
}

int main()
{
	ll n,m, ans = 0;
	cin >> n >> m;
	ll last = Pow(m, n-2);
	ll inv = Pow(m, mod - 2);

	for(ll x = 1; x <= m; x++)
	{
		ll A = last, B = 1;
		for(ll d = 1; d < n; d++)
		{
			ll cur = (n - d) * A % mod * B % mod;
			ans = (ans + cur) % mod;
			//  cout << "For d=" << d << ", x=" << x << " A,B,ans " << A << " " << B << " " << ans << "\n";
			A = (A * inv) % mod;
			B = (B * (m-x)) % mod;
		}
	}
	ans = n * Pow(m, n) - ans;
	ans %= mod;
	if(ans < 0) ans += mod;

	cout << ans << "\n";

	return 0;
}


#include <bits/stdc++.h>
#define fir first
#define sec second
#define PB push_back
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define sz(a) (a).size()

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i < (int)(n); ++i)
#define foreach(itr, c) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)

template <class T> void chkmax(T& x, T y) { x = (x > y ? x : y); }
template <class T> void chkmin(T& x, T y) { x = (x < y ? x : y); }

typedef long long LL;
typedef pair <int, int> pii;
typedef vector <int> vi;

const int N = 1e5 + 5;

int n, x, a[N];

int main(){
    scanf("%d%d", &n, &x);
    rep1(i, n + 1){
        scanf("%d", &a[i]);
        if (a[i] != x) printf("%d ", a[i]);
    }
    printf("\n");
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;

const int N = (int) 2e5 + 7;
int n;
int m;
int k;
int x[N];
bool is[N];
ld dp[N];

ld algo(ld dp0) {
  ld sum = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (is[i]) {
      dp[i] = dp0;
    } else {
      dp[i] = 1 + sum / (ld) m;
    }
    sum += dp[i];
    sum -= dp[i + m];
  }
  return dp[0];
 /// cout << dp0 << " " << dp[0] << "\n";
}

bool ok(ld dp0) {
  return dp0 - algo(dp0) < 1e-14;
}

const ld INF = (ld) 1e12;

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

 /// freopen ("input", "r", stdin);

  cin >> n >> m >> k;
  for (int i = 1; i <= k; i++) {
    int x;
    cin >> x;
    is[x] = 1;
  }
  ld l = 0, r = INF;
  if (ok(r)) {
    cout << "-1\n";
    return 0;
  }
  for (int i = 1; i <= 100; i++) {
    ld m = (l + r) * 0.5;
    if (ok(m)) {
      l = m;
    } else {
      r = m;
    }
  }
  cout << fixed << setprecision(3) << l << "\n";
  return 0;
  ld x = 0;
  while (1) {
    algo(x);
    ld y = dp[0];
    if (fabs(x - y) < 1e-14) {
      break;
    }
    x = y;
  }
  cout << fixed << setprecision(6) << x << "\n";
}


#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define Pii pair<int,int>
#define Pll pair<long long,long long>
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
//vector<vector<ll>> dp(n,vector<ll>(n))
//2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
//3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d)));

using namespace std;

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

    string s; cin >> s;
    ll M; cin >> M;
    ll ma = -1;
    rep(i,s.size()) chmax(ma,s[i]-'0');

    if(s.size()==1){
        if((s[0]-'0')<=M) cout << 1 << endl;
        else cout << 0 << endl;
        return 0;
    }

    ll ok = ma,ng=1e18*2;

    while(ng-ok>1)
    {
        ll mid = (ok+ng)/2LL;
        vector<ll> v;
        ll m = M;
        while(m>0){
            v.pb(m % mid);
            m /= mid;
        }
        reverse(ALL(v));
        if(s.size()<v.size()) ok = mid;
        else if(s.size()>v.size()) ng = mid;
        else{
            rep(i,s.size()){
                if((ll)(s[i]-'0')<v[i]){ ok = mid; break;}
                if((ll)(s[i]-'0')>v[i]){ ng = mid; break;}
            }
            if(ok!=mid&&ng!=mid) ok=mid;
        }
    }

    cout << ok-ma << endl;
    return 0;
}
// g++ main.cpp -o a.out && ./a.out

// 9223372036854775807

#include <bits/stdc++.h>
using namespace std;
int N, Q;
const int maxn = 2e5 + 5;
typedef long long ll;
inline ll read()
{
    ll x = 0;
    int s = 1;
    char c = getchar();
    while (c < 48 || c > 57)
    {
        if (c == '-')
            s = -1;
        c = getchar();
    }
    while (c >= 48 && c <= 57)
    {
        x = (x << 3) + (x << 1) + c - '0';
        c = getchar();
    }
    return x * s;
}
int main()
{
    int N, Q;
    N = read();
    ll a, t;
    ll l = -1e9, r = 1e9, b = 0;
    for (int i = 0; i < N; i++)
    {
        a = read();
        t = read();
        if (t == 1)
        {
            l += a;
            r += a;
            b += a;
        }
        else if (t == 2)
        {
            l = max(l, a);
            r = max(r, a);
        }
        else
        {
            l = min(l, a);
            r = min(r, a);
        }
    }
    Q = read();
    while (Q--)
    {
        ll x = read() + b;
        if (x < l)
            cout << l;
        else if (x > r)
            cout << r;
        else
            cout << x;
        cout << ' ';
    }
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int a, b, c;
    cin >> a >> b >> c;
    cout << a + b + c - min(min(a, b), c) << endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define int long long
#define ordered_set tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update>
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
typedef unsigned long long ull;
#define f(i,a,n) for(int i=a;i<n;i++)
#define f1(i,a,n) for(int i=a;i<=n;i++)
#define fab(i,a,b) for(int i=a;i<=b;i++)
#define fr(i,a) for(int i=a;i>=0;i--)
#define sc second
#define ft first
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define maxN 1000000
const int INF=1e18;
/*---------------------------------------------------------------------------------------------------------------------------*/
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
void google(int t) {cout << "Case #" << t << ": ";}
int mul_mod(int a,int b,int m){ a%m; b%m; return ((((a*b)%m)+m)%m);}
int mul_add(int a,int b,int m){ a%m; b%m; return ((((a+b)%m)+m)%m);}
int mul_sub(int a,int b,int m){ a%m; b%m; return ((((a-b)%m)+m)%m);}
bool cmp(pair<int,int> a,pair<int,int> b){if(a.ft+a.sc!=b.ft+b.sc) return a.ft+a.sc<b.ft+b.sc; return a.ft<b.ft;}
bool isPrime(int n){if(n==1 or n==0) return false; if(n==2 or n==3) return true; if(n%2==0 or n%3==0) return false; for(int i=5;i*i<=n;i+=6){if(n%i==0 or n%(i+2)==0)return false;}return true;}
int power(int base,int n,int modulo){int res=1; while(n){if(n&1)res=mul_mod(res,base,modulo); base=mul_mod(base,base,modulo);n>>1;} return res;}
int dig(int n){int cnt=0; while(n){cnt++; n>>1;} return cnt;}
bool isPalindrome(string s){int n=s.size(); int l=0,r=n-1; bool flag=true; while(l<r){ if(s[l]!=s[r]){flag=false;break;}l++;r--;}if(flag)return true; return false;}
/*---------------------------------------------------------------------------------------------------------------------------*/
void solve(){
    int a,b,c;
    cin>>a>>b>>c;
    int ans=max((a+b),(b+c));
    ans=max(ans,(a+c));
    cout<<ans;
}
int32_t main(){
    fastio();
    int tc=1;
    //scanf("%lld",&tc);
    //cin>>tc;
    //cin.get();
    while (tc--)
    {
        solve();
    }
   return 0;
}


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

typedef long long ll;
const int N = 2e5 + 7;

char s[N];
int main() {
    cin >> (s + 1);
    int n = strlen(s + 1);
    ll ans = 0;
    char last = '0';
    int i;
    for (i = 1; i <= n - 2; i++) {
        if (s[i] == s[i + 1] && s[i + 2] != s[i] && last != s[i]) {
            ans += 1ll*(n - (i + 2)) + 1;
           // cout << ans << endl;

            last = s[i];
            i++;
        } else {
            if (s[i] == last) {
                ans--;
            }
        }

    }
    for (; i <= n; i++) {
        if (s[i] == last) {
            ans--;
        }
    }
    cout << ans << endl;

}

//a

#include <bits/stdc++.h>
#define ll long long
ll md = 1000000007;
#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);
#define pb push_back
#define g(v, i, j) get<i>(v[j])
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define srt(v) sort(v.begin(), v.end())
#define rsrt(v) sort(v.begin(), v.end(), greater<int>())
#define fi(i, a, b) for (int i = a; i < b; i++)
#define fll(i, a, b) for (ll i = a; i < b; i++)
using namespace std;
//using namespace std::chrono;
template <typename T>
T pw(T a, T b)
{
    T c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m);
        m = (m * m), b /= 2;
    }
    return c;
}
template <typename T>
T ceel(T a, T b)
{
    if (a % b == 0)
        return a / b;
    else
        return a / b + 1;
}
template <typename T>
T my_log(T n, T b)
{
    T i = 1, ans = 0;
    while (1)
    {
        if (i > n)
        {
            ans--;
            break;
        }
        if (i == n)
            break;
        i *= b, ans++;
    }
    return ans;
}
template <typename T>
T gcd(T a, T b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
template <typename T>
T mysqt(T a)
{
    T ans = 1;
    while (ans * ans <= a)
        ans++;
    ans--;
    return ans;
}
ll pwmd(ll a, ll b)
{
    ll c = 1, m = a;
    while (b)
    {
        if (b & 1)
            c = (c * m) % md;
        m = (m * m) % md;
        b /= 2;
    }
    return c;
}
ll modinv(ll n)
{
    return pwmd(n, md - 2);
}
ll inverse(ll i)
{
    if (i == 1)
        return 1;
    return (md - ((md / i) * inverse(md % i)) % md + md) % md;
}
bool sortbysec(const pair<ll, ll> &a,
               const pair<ll, ll> &b)
{
    return (a.second < b.second);
}
/* auto start = high_resolution_clock::now();
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);
    cerr << "Time taken: "
         << duration.count()<< "ms" <<"\n";*/
vector<char> capl = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> sml = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
template <typename T>
string conv(T a)
{
    string b;
    T e = 2;
    T c = my_log(a, e);
    c++, b = '1';
    for (T i = 0; i < c - 1; i++)
    {
        T k = a >> (c - 2 - i);
        if (k & 1)
            b += '1';
        else
            b += '0';
    }
    if (a == 0)
        return "0";
    return b;
}
int main()
{
    fast;
    string s;
    cin >> s;
    ll n = s.length(), ans = 0;
    vi cnt(26);
    for (ll i = n - 1; i >= 1; i--){
        if (s[i] == s[i - 1]){
            char c = s[i];
            ans += n - 1 - i - cnt[c - 'a'];
            while (s[i] == c) i--, cnt[c - 'a']++;
            i++;
            fi(j, 0, 26){
                if (j == c - 'a') cnt[j] = n  - i;
                else cnt[j] = 0;
            }
            continue;
        }
        cnt[s[i] - 'a']++;
    }
    cout << ans;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <queue>

#define lc u << 1
#define rc u << 1 | 1

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
LL n,s,k;

LL exgcd(LL a,LL b,LL &x,LL &y)
{
    if(!b)
    {
        x = 1,y = 0;
        return a;
    }
    LL d = exgcd(b,a % b,y,x);
    y -= x * (a / b);
    return d;
}

void solve()
{
    scanf("%lld%lld%lld",&n,&s,&k);
    LL x,y;
    LL d = exgcd(k,n,x,y);
    n /= d;
    if(s % d) puts("-1");
    else
    {
        printf("%lld\n",(-x * (s / d) % n + n) % n);
    }
}

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #else

    #endif // LOCAL
    int T = 1;
    scanf("%d",&T);
    while(T --)
    {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll n, ll m){
  if(n < m) swap(n, m);
  if(n % m == 0) return m;
  return gcd(m, n % m);
}
ll extgcd(ll a, ll b, ll& x, ll& y){
	if (b == 0) {
    	x = 1;
    	y = 0;
    	return a;
  	}
  	ll gcd = extgcd(b, a % b, x, y);
  	ll oldX = x;
  	x = y;
  	y = oldX - a / b * y;
  	return gcd;
}
int main(){
  int t;
  cin >> t;
  for(int loop = 0; loop < t; loop++){
    ll n, s, k, x, y;
    cin >> n >> s >> k;
    ll g = gcd(n, k);
    if(s % g != 0){
      cout << -1 << "\n";
      continue;
    }
    n /= g; k /= g; s /= g;
    int a = extgcd(n, k, x, y);
    x *= s; y *= s;
    //cout << n << " " << x << " " << k << " " << y << " " << s << " ";
    if(y < 0) y %= n;
    if(y > 0){
      y -= (y / n) * n;
    }
    if(y > 0) y -= n;
    cout << -y << "\n";
  }
}


#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 998244353LL
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n,k,x,y;

//moddivとnCkとgarner
//c剰余下でaをbで割った値
ll moddiv(ll a,ll b,ll c){
	ll x0=c,x1=b,x2,n0=0LL,n1=1LL,n2,t=a%b,m,ans;
	if (t==0LL) return a/b;
		for(int i=0;i<900;i++){
      m=x0/x1;
      x2=x0-x1*m;
      n2=(n0-m*n1)%c;
      if (x2==1LL){
        ans=(n2+c)%c;
        break;
      }
      x0=x1;x1=x2;
      n0=n1;n1=n2;
    }
	return (a+((t*ans)%c)*b-t)/b;
}

//nCk
vi mulmod(301);//0!,1!,2!,3!,4!,,,,
void first_nCk(){
  mulmod[0]=1LL;
  for(ll i=1;i<301;i++){
    mulmod[i]=mulmod[i-1]*i%MOD;
  }
}

ll nCk(ll nn,ll kk){
  return moddiv(mulmod[nn],mulmod[kk]*mulmod[nn-kk]%MOD,MOD);
}

ll modinv(ll a,ll b){
  return moddiv(1,a,b);
}

int main(){
  ll ans=0;
  first_nCk();
  cin >> n >> k;
  k++;
  vi a(n),tmsum(k),datasum(k);
  vvi data(n, vi(k));
  REP(i,n) cin>>a[i];

  for(ll i=0;i<n;i++){
    ll gege=1;
    ll ggg=1;
    for(ll j=0;j<k;j++){
      data[i][j]=gege;
      gege=a[i]*gege%MOD;
      tmsum[j]+=ggg;
      ggg=ggg*a[i]*2%MOD;
    }
  }

  for(ll i=0;i<n;i++){
    for(ll j=0;j<k;j++){
      datasum[j]+=data[i][j];
    }
  }

  for(ll j=0;j<k;j++){
    tmsum[j]%=MOD;
    datasum[j]%=MOD;
  }

  for(ll i=1;i<k;i++){
    ans=0;
    ll tmp=MOD;
    for(ll j=0;j<i+1;j++){
      tmp=tmp+datasum[i-j]*datasum[j]%MOD*nCk(i,j)%MOD;
    }
    tmp=(tmp-tmsum[i])%MOD;
    cout<<(moddiv(tmp,2,MOD))<<endl;
  }
  return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define ar array
#define ll long long

const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;

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

void solve() {
    // (M + N) choose (sum A_i + N)
    int N, M;
    cin >> N >> M;
    int sum = 0;

    for (int i = 0; i < N; i++) {
        int a;
        cin >> a;
        sum += a;
    }

    int n = M + N;
    int r = sum + N;
    ll choose;

    if (n < r) {
        choose = 0;
    } else {
        choose = 1;

        for (int i = 0; i < r; i++)
            choose = choose * (n - i) % MOD * qexp(i + 1, MOD - 2, MOD) % MOD;
    }
    cout << choose << "\n";
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc = 1;
    // cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t  << ": ";
        solve();
    }
}


#include<bits/stdc++.h>
#include<string.h>
#define ll long long
#define pf printf
#define sf scanf
#define ff first
#define ss second
#define clr clear()
#define sz size()
#define pb push_back
#define mk make_pair
#define pi acos(-1)
#define inf 200000000000
#define mod 998244353
#define ull unsigned long long int
#define f(i,k,n) for(i=k;i<n;i++)
#define fr(i,n,k) for(i=n;i>=k;i--)
#define ent(a) scanf("%lld",&a)
#define ent2(a,b) scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a) memset(a,0,sizeof(a))
#define vec(v,s) vector<ll>v[s]
#define arr(a,s) ll a[s];
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
random_device rd;
mt19937 random(rd());*/
//knight and king//
int dr[]= {2,2,-2,-2,1,-1,1,-1};
int dc[]= {1,-1,1,-1,2,2,-2,-2};
int dr1[]= {0,0,1,1,1,-1,-1,-1};
int dc1[]= {1,-1,1,0,-1,0,1,-1};
int dr2[]= {0,0,1,-1};
int dc2[]= {1,-1,0,0};
////////////////////////////
using namespace std;
#define ma 200005

ll in[ma],a[ma];
vector<ll>adj[ma];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll i,j,tc,w,q,x,y,n;
    string s;
    while(cin>>n)
    {
       cin>>s;
       stack<ll>q;

       for(i=0;i<s.sz;i++)
       {
           if(s[i]=='x')
           {
               if(q.sz>1)
               {
                   ll r=q.top();
                   q.pop();
                   ll r1=q.top();
                   q.pop();
                   if(s[r1]=='f'&&s[r]=='o')continue;
                   q.push(r1);q.push(r);
                   q.push(i);
               }
               else q.push(i);
           }
           else q.push(i);
       }
       cout<<q.sz<<endl;

    }
    return 0;
}

#pragma region Region_1

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
using ll = long long;
using P = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VC = vector<char>;
using VVC = vector<VC>;
#define MOD 1000000007
const int INF = 1e9 + 10;
const long long INFL = 2e18 + 10;  // ll max > 9*10^18

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

template <class T>
T sum(const vector<T>& v) {
    T res = 0;
    for (size_t i = 0; i < v.size(); ++i) res += v[i];
    return res;
}

/////////////////////////////////////////////////////////
// print like python
// https://qiita.com/Lily0727K/items/06cb1d6da8a436369eed
/////////////////////////////////////////////////////////
void print() { cout << endl; }

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail) != 0) cout << " ";
    print(forward<Tail>(tail)...);
}

template <class T>
void print(vector<T>& vec) {
    for (auto& a : vec) {
        cout << a;
        if (&a != &vec.back()) cout << " ";
    }
    cout << endl;
}

template <class T>
void print(vector<vector<T>>& df) {
    for (auto& vec : df) {
        print(vec);
    }
}

#pragma endregion Region_1
/////////////////////////////////////////////////////////

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    string s;
    cin >> s;
    VC v;
    int cnt=0;
    for (char c : s) {
        if (c != 'x' || v.size() < 2) {
            v.push_back(c);
            continue;
        }
        if (v.back() == 'o' && v[v.size() - 2] == 'f') {
            v.pop_back();
            v.pop_back();
            cnt++;
        } else {
            v.push_back(c);
        }
    }
    print(s.size()-cnt*3);

    return 0;
}


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

int main() {
  int N;
  cin >> N;
  long long int S = 0;
  vector<long long int> V(2 * N);
  for (int i = 0; i < 2 * N; i++) {
    cin >> V.at(i);
    S += V.at(i);
  }
  priority_queue<long long int, vector<long long int>, greater<long long int>> Q;
  long long int T = 0;
  for (int i = 0; i < N; i++) {
    Q.push(V.at(N - 1 - i));
    Q.push(V.at(N + i));
    T += Q.top();
    Q.pop();
  }
  cout << S - T << endl;
}

#include <bits/stdc++.h>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;

class rep {
    struct Iter {
        usize itr;
        constexpr Iter(const usize pos) noexcept : itr(pos) {}
        constexpr void operator++() noexcept { ++itr; }
        constexpr bool operator!=(const Iter& other) const noexcept {
            return itr != other.itr;
        }
        constexpr usize operator*() const noexcept { return itr; }
    };
    const Iter first, last;

  public:
    explicit constexpr rep(const usize first, const usize last) noexcept
        : first(first), last(std::max(first, last)) {}
    constexpr Iter begin() const noexcept { return first; }
    constexpr Iter end() const noexcept { return last; }
};

template <class T> using Vec = std::vector<T>;

template <class T> using Heap = std::priority_queue<T, Vec<T>, std::greater<T>>;

void B_main() {
    usize N;
    std::cin >> N;
    Vec<u64> V(2 * N);
    for (auto& x : V) {
        std::cin >> x;
    }
    Heap<u64> heap;
    for (const auto i : rep(0, N)) {
        heap.push(V[N - i - 1]);
        heap.push(V[N + i]);
        heap.pop();
    }
    u64 ans = 0;
    while (!heap.empty()) {
        ans += heap.top();
        heap.pop();
    }
    std::cout << ans << '\n';
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    B_main();
    return 0;
}


#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
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; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
//#include <unistd.h>
void solve(){
        int n;
        cin>>n;
        V<int> a(n);
        for(int i=0;i<n;i++){
                cin>>a[i];
                a[i]--;
        }
        int t=0;
        V<int> ans;
        V<bool> ok(n,false);
        while(1){
              bool ok=true;
                for(int j=0;j<n;j++){
                        if(a[j]<j){
                              ok=false;
                               if(j%2==t){
                                      if(t==0){
                                             swap(a[0],a[1]);
                                             ans.emplace_back(1);
                                      }else{
                                            swap(a[1],a[2]);
                                            ans.emplace_back(2);
                                      }
                                      t^=1;
                               }
                               while(a[j]<j){
                                       swap(a[j-1],a[j]);
                                       ans.emplace_back(j);
                                       j--;
                                       t^=1;
                               }
                               break;
                        }
                }
                if(ok)break;
        }
      //  print(a);
        cout<<ans.size()<<"\n";
        print(ans);
}
int main(){
      int t;
      cin>>t;
      while(t--)solve();
}

/**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define ld double
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define F(i,a,b) for(i=a;i<b;i++)
//#include <ext/pb_ds/assoc_container.hpp> // policy based data structure header files
//#include <ext/pb_ds/tree_policy.hpp> // policy based data structure header files
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds; // for pbds
//#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // have functions like order_of_key, find_by_order
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781641;
const ll M=1e18;
const ll MAXN=100200;
ll i,j,mask;
void solve()
{
    ll n;
    cin>>n;
    vector<ll> v(n), ans;
    F(i,0,n) {
        cin>>v[i];
    }
    for(i=0; ; i++) {
        bool check=true;
        F(j,0,n-1) {
            if(v[j]>v[j+1]) {
                check=false;
                break;
            }
        }
        if(check)
            break;
        ll counter=i%2;
        while(counter+1<n && v[counter]<v[counter+1])
            counter+=2;
        if(counter+1>=n) {
            ans.pb(counter-2);
            swap(v[counter-2], v[counter-1]);
        } else {
            ans.pb(counter);
            swap(v[counter], v[counter+1]);
        }
    }
    cout<<ans.size();
    nl
    for(auto x:ans) {
        cout<<(x+1)<<" ";
    }
}
int main()
{
    IOS
    /*#ifndef ONLINE_JUDGE
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
    #endif // ONLINE_JUDGE*/
    ll t,test=1;
    cin>>test;
    F(t,1,test+1) {
        //cout<<"Case #"<<t<<": ";
        solve();
        nl
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
//#define int ll
#define ll long long
#define db double
#define rep(i,be,en) for(int i=be;i<=en;i++)
const int INF = 0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
//const double pi=acos(-1);
const int mod=1e9+7;
const double eps = 1e-8;
using namespace std;
const int N=1e5+7;
const int M=1e6+7;

inline int read(){
    char c=getchar();int x=0,f=1;
    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 T,n,m,fl,ans;
int a[N],b[N],c[N];
vector<int> g[N];

void dfs(int u,int fa){
	if(c[a[u]]==0){
		c[a[u]]=1;
		b[u]=1;
		for(int v:g[u]){
			if(v==fa) continue;
			dfs(v,u);
		}
		c[a[u]]=0;
	}else{
		for(int v:g[u]){
			if(v==fa) continue;
			dfs(v,u);
		}
	}
	return ;
}

signed main(){
	n=read();
	rep(i,1,n){
		a[i]=read();
	}
	rep(i,2,n){
		int u=read(),v=read();
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1,-1);
	rep(i,1,n){
		if(b[i]) printf("%d\n",i);
	}

}

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

#define watch(x) cerr << (#x) << ": " << (x) << endl

typedef long long LL;

const int N = 200009;
int n,m;

void nope() {
    cout << -1 << endl;
    exit(0);
}

void pr(int a,int b) {
    cout << a << ' ' << b << '\n';
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;

    if(n == 1) {
        if(m == 0) {
            pr(1, 2);
            return 0;
        } else {
            nope();
        }
    }

    int tp = n-m-2;

    if(m < 0 || tp < 0) {
        nope();
    }

    for(int i = 0; i < n; ++i) {
        if(i < tp) {
            pr(2*i+1, 2*i+2);
        } else if (i < n-1) {
            pr(2*i+2, 2*i+3);
        } else {
            pr(2*tp+1, 2*n);
        }
    }

    return 0;
}


#line 2 "/home/snow/competitive-programming/competitive-programming-library/snow/template.hpp"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

constexpr long long INF = 1LL << 60;
constexpr long double PI = 3.141592653589;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define debug(x) cout << #x << ": " << (x) << '\n'

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> tpl;

template <typename T = ll>
using vec = vector<T>;

template <typename T = ll>
using vec2 = vector<vector<T>>;

template <typename T = ll>
using vec3 = vector<vector<vector<T>>>;

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

template < typename T >
inline bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
#line 4 "/home/snow/competitive-programming/competitive-programming-library/snow/io/setup.hpp"

namespace snow{

struct IoSetup {
    IoSetup() {
        std::cin.tie(nullptr);
        std::ios::sync_with_stdio(false);
        std::cout << std::fixed << std::setprecision(10);
    }
} iosetup;

}
#line 5 "/home/snow/competitive-programming/competitive-programming-library/snow/io/helper.hpp"

template< typename T1, typename T2 >
std::ostream &operator << (std::ostream &os, const std::pair< T1, T2 > &p) {
    os << p.first << " " << p.second;
    return os;
}

template< typename T1, typename T2 >
std::istream &operator >> (std::istream &is, std::pair< T1, T2 > &p) {
    is >> p.first >> p.second;
    return is;
}

template< typename T1, typename T2, typename T3 >
std::ostream &operator << (std::ostream &os, const std::tuple< T1, T2, T3 > &t) {
    auto &[a, b, c] = t;
    os << a << " " << b << " " << c;
    return os;
}

template< typename T1, typename T2, typename T3 >
std::istream &operator >> (std::istream &is, std::tuple< T1, T2, T3 > &t) {
    auto &[a, b, c] = t;
    is >> a >> b >> c;
    return is;
}

template< typename T >
std::ostream &operator << (std::ostream &os, const std::vector< T > &v){
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i] << (i + 1 != v.size() ? " " : "");
    }
    return os;
}

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

template< typename T >
std::ostream &operator << (std::ostream &os, const std::set< T > &st){
    int ct = 0;
    for(auto& s : st) os << s << (++ct != st.size() ? " " : "");
    return os;
}

template<class... T>
void input(T&... a){
    (std::cin >> ... >> a);
}

void print() {
    std::cout << '\n';
}
template<class T, class... Ts>
void print(const T& a, const Ts&... b){
    std::cout << a;
    (std::cout << ... << (std::cout << ' ', b));
    std::cout << '\n';
}

int drop() {
    std::cout << '\n';
    exit(0);
}
template<class T, class... Ts>
int drop(const T& a, const Ts&... b){
    std::cout << a;
    (std::cout << ... << (std::cout << ' ', b));
    std::cout << '\n';
    exit(0);
}
#line 4 "main.cpp"
using namespace snow;

// #include "snow/algorithm/binary-search.hpp"

// #include "atcoder/modint"
// using namespace atcoder;
// using mint = modint1000000007;

int main() {
    ll A, B;
    cin >> A >> B;

    ll ans = 0;

    for(ll g = 1; g <= B; ++g){
        ll k = ceil(ld(A) / g);
        ll l = B / g;

        if(k < l) chmax(ans, g);
    }

    print(ans);

    return 0;
}

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

int main(){
    int l,r,m,n;
    cin>>l>>r;
    int nowmax=0;
    for(int i=1;i<=r-l;i++){
    	if(l%i!=0)	m=(l-l%i)+i;
    	else m=l;

    	n=(r-r%i);
    	if(m!=n){
    		nowmax=i;
		}
	}
	cout<<nowmax;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
  int n,x;
  cin>>n>>x;

  vector<int> a(n);

  for(int i = 0; i < n; i++){
    cin >> a[i];
  }
int c = 0;
  for(int i = 0; i < n; i++){
    if(a[i] != x){
      cout << a[i] << ' ';
      c++;
    }
  }
  if(c == 0){cout << endl;}
}

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

// ---------- Debugger

#define TRACE

string to_string(string s) {
    return '"' + s + '"';
}

string to_string(char * s) {
    return to_string((string) s);
}

string to_string(const char * s) {
    return to_string((string) s);
}

string to_string(bool b) {
    return (b ? "true" : "false");
}

template < typename A >
string to_string(A v);

template < typename A, typename B >
string to_string(pair < A, B > p) {
    return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template < typename C > struct rge { C b, e; };
template < typename C > rge<C> range(C i, C j) { return rge<C>{i, j}; }

template < typename C >
string to_string(rge<C> v) {
    bool first = true;
    string res = "[";
    for(auto it = v.b; it != v.e; it++) {
        if (!first) {
            res += ", ";
        }
        first = false;
        res += to_string(*it);
    }
    res += "]";
    return res;
}

template < typename A >
string to_string(A v) {
    return to_string(range(v.begin(), v.end()));
}

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template < typename Head >
void __f(const string names, Head arg1) {
    cerr << names << " : " << to_string(arg1) << endl;
}
template < typename Head, typename...Tail >
void __f(const string names, Head arg1, Tail...args) {
    int b = 0, comma = names.size();
    for(int i=0;i<names.size();i++) {
        if(names[i] == ',' && b == 0) {
            comma = i;
            break;
        }
        if(names[i] == '(' || names[i] == '{' || names[i] == '[' || names[i] == '<') b++;
        else if(names[i] == ')' || names[i] == '}' || names[i] == ']' || names[i] == '>') b--;
    }
    // size_t comma = names.find(', ');
    cerr << names.substr(0, comma) << " : " << to_string(arg1) << " | ";
    __f(names.substr(comma + 1), args...);
}
#else
#define trace(...)
#endif

// ---------- Debugger

#define LL long long
#define FI ios_base::sync_with_stdio(false); cin.tie(NULL);
#define PREC cout << setprecision(10) << fixed;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 9;
const int MX = 1e5 + 5;

int main() {
    int n, x;
    int t;
    cin >> n >> x;
    vector<int> ans;
    for(int i=0;i<n;i++) {
        cin >> t;
        if(t != x) {
            ans.push_back(t);
        }
    }
     for(int i=0;i<ans.size();i++) {
            cout << ans[i] << " ";
        }
        cout << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<unordered_map>
#include<random>
#include<array>
#include<cassert>
using namespace std;
#define INF ((1<<30)-1)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()

struct TrieNode{
	int n = 0;
	long long sum = 0;
	TrieNode* l = nullptr, * r = nullptr;
};

class Trie {
public:
	Trie() { root = new TrieNode(); }

	void insert(int x) {
		TrieNode* t = root;
		for (int i = 30; i >= 0; i--) {
			t->n++;
			t->sum += x;
			if (x >> i &1) {
				if (t->r == nullptr) t->r = new TrieNode;
				t = t->r;
			}
			else {
				if (t->l == nullptr) t->l = new TrieNode;
				t = t->l;
			}
		}
		t->n++;
		t->sum += x;
	}

	void erase(int x) {
		TrieNode* t = root;
		for (int i = 30; i >= 0; i--) {
			t->n--;
			t->sum -= x;
			if (x >> i & 1) {
				t = t->r;
			}
			else {
				t = t->l;
			}
		}
		t->n--;
		t->sum -= x;
	}

	pair<int, long long> query(int x) const{
		pair<int, long long > ret(0, 0);
		TrieNode* t = root;
		for (int i = 30; i >= 0; i--) {
			if (t == nullptr)break;
			if (x >> i & 1) {
				if (t->l) {
					ret.first += t->l->n;
					ret.second += t->l->sum;
				}
				t = t->r;
			}
			else {
				t = t->l;
			}
		}
		return ret;
	}
private:
	TrieNode* root;
};

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

	int n, m, q;
	cin >> n >> m >> q;

	vector<int> a(n, 0), b(m, 0);

	Trie ta, tb;
	rep(i, n) ta.insert(0);
	rep(i, m)tb.insert(0);

	long long ans = 0;
	rep(_, q) {
		int t, x, y;
		cin >> t >> x >> y;
		x--;

		if (t == 1) {
			auto p = tb.query(a[x]);
			ans -= (long long)p.first * a[x];
			ans += p.second;

			p = tb.query(y);
			ans += (long long)p.first * y;
			ans -= p.second;

			ta.erase(a[x]);
			ta.insert(y);
			a[x] = y;
		}
		else {
			auto p = ta.query(b[x]);
			ans -= (long long)p.first * b[x];
			ans += p.second;

			p = ta.query(y);
			ans += (long long)p.first * y;
			ans -= p.second;

			tb.erase(b[x]);
			tb.insert(y);
			b[x] = y;
		}
		cout << ans << endl;
	}

	return 0;
}

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<set>
typedef long long ll;
typedef unsigned un;
typedef std::vector<int> P;
typedef std::pair<int,int> pii;
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 f*x;}
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
template <typename T> bool umax(T& a,T t){if(t>a)return a=t,1;return 0;}
template <typename T> bool umin(T& a,T t){if(t<a)return a=t,1;return 0;}
/**********/
const int MAXN = 200011;
struct one{int t,x,y;}a[MAXN];
int fx[MAXN],diff=0;
int place(int val){return std::lower_bound(fx+1,fx+diff+1,val)-fx;}
struct sgt
{
	struct node
	{
		ll cnt,sum;
	}t[MAXN<<2|1];
#define rt t[num]
	void modify(un pos,ll k,un l=1,un r=diff,un num=1)
	{
		rt.cnt+=k,rt.sum+=k*fx[pos];
		if(l==r)return;
		un mid=(l+r)>>1;
		if(pos<=mid)modify(pos,k,l,mid,num<<1);
		else modify(pos,k,mid+1,r,num<<1|1);
	}
	ll Qcnt(un ql,un qr,un l=1,un r=diff,un num=1)
	{
		if(ql>qr)return 0;
		if(ql<=l&&r<=qr)return rt.cnt;
		un mid=(l+r)>>1;
		ll res=0;
		if(ql<=mid)res+=Qcnt(ql,qr,l,mid,num<<1);
		if(qr>mid)res+=Qcnt(ql,qr,mid+1,r,num<<1|1);
		return res;
	}
	ll Qsum(un ql,un qr,un l=1,un r=diff,un num=1)
	{
		if(ql>qr)return 0;
		if(ql<=l&&r<=qr)return rt.sum;
		un mid=(l+r)>>1;
		ll res=0;
		if(ql<=mid)res+=Qsum(ql,qr,l,mid,num<<1);
		if(qr>mid)res+=Qsum(ql,qr,mid+1,r,num<<1|1);
		return res;
	}
}t[2];
int seq[2][MAXN];
ll f[2][MAXN];
int main()
{
	int n1=read(),n2=read(),m=read();
	fx[++diff]=0;
	for(int i=1;i<=m;++i)a[i].t=read()-1,a[i].x=read(),fx[++diff]=a[i].y=read();
	std::sort(fx+1,fx+diff+1),diff=std::unique(fx+1,fx+diff+1)-fx-1;
	t[0].modify(1,n1),t[1].modify(1,n2);
	for(int i=1;i<=n1;++i)seq[0][i]=1;
	for(int i=1;i<=n2;++i)seq[1][i]=1;
	ll ans=0;
	for(int i=1;i<=m;++i)
	{
		int tp=a[i].t,val=place(a[i].y);
		int& pre=seq[tp][a[i].x];
		///printf("%d to %d(%d)\n",pre,val,a[i].y);
		if(pre<=val)
		{
			//printf("%d\n",a[i].y-fx[pre]);
			ans+=t[!a[i].t].Qcnt(1,pre)*(a[i].y-fx[pre])+ t[!a[i].t].Qcnt(pre+1,val)*a[i].y-t[!a[i].t].Qsum(pre+1,val);
		}
		else
		{
			ans-=t[!a[i].t].Qcnt(1,val)*(fx[pre]-a[i].y)+ t[!a[i].t].Qcnt(val+1,pre)*fx[pre]-t[!a[i].t].Qsum(val+1,pre);
		}
		t[a[i].t].modify(pre,-1);
		t[a[i].t].modify(pre=val,1);
		printf("%lld\n",ans);
	}
	return 0;
}


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

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
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...);
}
#define pi 3.141592653589
#define MOD 1000000007
#define to() int tt; cin>>tt; while(tt--)
#define pb push_back
#define in insert
#define mp make_pair
#define ff first
#define ss second
#define si(s) s.size()
#define fori(it,v) for(it=v.begin();it!=v.end(); it++)
#define for1(i,low,high) for(int i=low;i<high;i++)
#define for2(i,low,high) for(int i=low;i<=high;i++)
#define rev(i,high,low)  for(int i=high;i>=low ;i--)
#define all(x) x.begin(), x.end()
#define fil(x,i) memset(x,i,sizeof(x));
#define setbits(x) __builtin_popcount(x)
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
void i_o() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin) ;
    freopen("output.txt", "w", stdout) ;
#endif
}
int n,m,k;
vector<vector<int> >g;
vector<int>c;
vector<int>d[20];
int dp[1<<18][20];
void bfs(int i){
	d[i][c[i]]=0;
	queue<int>q;
	q.push(c[i]);
	while(!q.empty()){
		int cur=q.front(); q.pop();
		for(int j:g[cur]){
			if(d[i][j]>d[i][cur]+1){
				if(d[i][j]==1e7) q.push(j);
				d[i][j]=d[i][cur]+1;
			}
		}
	}
}

int fun(int mask,int i){
	if(mask==0|| mask==1<<i)
		return 0;
	if(dp[mask][i]!=-1) return dp[mask][i];
	int ans=1e7;
	if(i==k){
		for(int j=0;j<k;j++){
			ans=min(ans,fun(mask^(1<<j),j)+1);
		}
	}
	else
		for(int j=0;j<k;j++){
			if(!(mask&(1<<j))) continue;
			ans=min(ans,fun(mask^(1<<j),j)+d[j][c[i]]);
		}
	return dp[mask][i]=ans;
}

int main()
{
    boost; i_o();
    cin>>n>>m;
    g.resize(n+1);
    for1(i,0,m){
    	int a,b; cin>>a>>b;
    	g[a].pb(b); g[b].pb(a);
    }
    cin>>k;
	c.resize(k);
	for1(i,0,k) d[i].resize(n+1,1e7);
    for1(i,0,k) cin>>c[i];
    for1(i,0,k) bfs(i);
  /*  for1(i,0,k){
    	cout<<"src= "<<c[i]<<endl;
    	for1(j,1,n+1) cout<<d[i][j]<<" ";
    	cout<<endl;
    }
    */
    fil(dp,-1);
 /*
    for1(mask,0,(1<<k)) for1(i,0,k+1) dp[mask][i]=1e7;
    for1(i,0,k) dp[1<<i][i]=d[0][i]=0;
    for(int mask=0;mask<(1<<k);mask++){
    	for(int i=0;i<k;i++){
    			if(!(mask&(1<<i))) continue;
    			for(int j=0;j<k;j++){
    				 if(!(mask&(i<<j))) continue;
    				dp[mask][i]=min(dp[mask][i],dp[mask^i][j]+d[c[j]][c[i]]);
    			}
    	}
    }
    */
    int ans=fun((1<<k)-1,k);
    if(ans>=1e7) ans=-1;
    cout<<ans<<endl;
    return 0;
}

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

int n,m,c[110],depth[110],ans[10010];
vector <pair <pii,int> > g[110];
vector <pii> e;
bool vis[110];

void dfs(int pos,int par,int d)
{
	vis[pos]=true;
	depth[pos]=d;
	bool bk=false;
	rep(i,g[pos].size())
	{
		if(g[pos][i].fi.fi==par&& !bk)
		{
			bk=true;
			continue;
		}
		if(vis[g[pos][i].fi.fi])
		{
			if(depth[g[pos][i].fi.fi]<d) ans[g[pos][i].fi.se]=(g[pos][i].se==0 ? 1:2);
		}
		else
		{
			ans[g[pos][i].fi.se]=(g[pos][i].se==0 ? 1:2);
			dfs(g[pos][i].fi.fi,pos,d+1);
		}
	}
}

int main()
{
	cin>>n>>m;
	int x,y;
	rep(i,m)
	{
		scanf("%d%d",&x,&y);
		e.pb(mpr(x,y));
	}
	repn(i,n) scanf("%d",&c[i]);
	rep(i,m)
	{
		if(c[e[i].fi]>c[e[i].se]) ans[i]=1;//->
		else if(c[e[i].fi]<c[e[i].se]) ans[i]=2;//<-
		else
		{
			g[e[i].fi].pb(mpr(mpr(e[i].se,i),0));
			g[e[i].se].pb(mpr(mpr(e[i].fi,i),1));
		}
	}
	repn(i,n)
	{
		if(vis[i]) continue;
		dfs(i,0,1);
	}
	rep(i,m) puts(ans[i]==1 ? "->":"<-");
	return 0;
}


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

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin>>n;
  if (n==1){
    cout<<"1\n";
    return 0;
  }
  if (n==2){
    cout<<"1 2\n";
    return 0;
  }
  vector<int> div(n+1,1);
  for(int i=3;i<=n;i++){
    for(int j=2;j*j<=i;j++){
      if (i%j==0){
	div[i]=i/j;
	break;
      }
    }
  }
  vector<int> ans(n+1);
  ans[0]=0;
  ans[1]=1;
  for(int i=2;i<=n;i++){
    ans[i]=ans[div[i]]+1;
  }
  for(int i=1;i<=n;i++){
    if (i!=1) cout<<" ";
    cout<<ans[i];
  }
  cout<<"\n";
  return 0;
}

#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <tuple>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>

//#include <bits/stdc++.h>
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define FOR(i,i0,n) for(int (i)=(i0);(i)<(n);(i)++)
#define FORR(i,i0,n) for(int (i)=(n)-1; (i)>=(i0);(i)--)
#define SORT(x) sort(x.begin(),x.end())
#define SORTR(x) sort(x.begin(),x.end(),greater<int>())

#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple

using namespace std;
using ll = long long;
using LL = long long;

typedef std::pair<int, int> pii;
typedef std::pair<int, double> pid;
typedef std::vector<int> vi;
typedef std::vector<pii> vii;

const double PI = 3.14159265358979323846264338327950L;
const int mod = 1e9+7;

struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

const ll INF = 1LL<<60;

struct container {
    int bottom, top, id;
    container(int id, int bottom, int top) : top(top), bottom(bottom), id(id) {
    }
};

struct Edge {
    ll to, from, cost;
    Edge(ll to, ll from, ll cost) :to(to), from(from), cost(cost) {}
    Edge(): to(-1), from(-1), cost(0){}
};

struct Data {
    ll cost,val;
    Data(ll cost=0, ll val=0) : cost(cost), val(val){}
    bool operator<(const Data& a) const {
        return cost > a.cost;
    }
};

struct edge{
    int a,b,c;
    edge(int a = -1,int b = -1, int c = -1):a(a),b(b),c(c){}
    bool operator<(const edge& a) const {
        return c > a.c;
    }
};

struct combination {
  vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
};

void solve(){
    int n;
    cin >> n;
    const int NMAX = 100100;
    int cnt[NMAX] = {};
    int p = 2;

    while(p<NMAX){
        int cur = p;
        cnt[cur] = 1;
        while(cur+p < NMAX){
            cur += p;
            int tmp = cur;
            while(tmp%p==0){
                cnt[cur]++;
                tmp /= p;
            }
        }
        while(p < NMAX && cnt[p] != 0){
            p++;
        }
    }

    rep(i,n)  cout << cnt[i+1]+1 << " ";
}

int main() {
    int T = 1;
    // cin >> T;

    while (T--) {
        solve();
        // cout << endl;
    }
}


#include<iostream>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
int N;
vector<pair<int,int> >S,T;
const double EPS=1e-7;
bool eq(double x,double y){return abs(x-y)<EPS;}
main()
{
	cin>>N;
	for(int i=0;i<N;i++)
	{
		int x,y;cin>>x>>y;
		S.push_back(make_pair(x,y));
	}
	for(int i=0;i<N;i++)
	{
		int x,y;cin>>x>>y;
		T.push_back(make_pair(x,y));
	}
	if(N==1)
	{
		cout<<"Yes"<<endl;
		return 0;
	}
	for(pair<int,int>p:S)
	{
		pair<int,int>q=T[0];
		map<int,vector<double> >s,t;
		for(pair<int,int>r:S)if(r!=p)
		{
			int dx=r.first-p.first,dy=r.second-p.second;
			s[dx*dx+dy*dy].push_back(atan2(dy,dx));
		}
		for(pair<int,int>r:T)if(r!=q)
		{
			int dx=r.first-q.first,dy=r.second-q.second;
			t[dx*dx+dy*dy].push_back(atan2(dy,dx));
		}
		vector<double>X;
		for(pair<int,vector<double> >PP:s)
		{
			vector<double>P=PP.second,Q=t[PP.first];
			if(Q.size()!=P.size())
			{
				X.clear();
				break;
			}
			vector<double>now;
			sort(P.begin(),P.end());
			sort(Q.begin(),Q.end());
			for(int i=0;i<P.size();i++)
			{
				double X=P[i]-Q[0];
				if(X<0)X+=2*M_PI;
				bool ok=true;
				for(int j=0;j<Q.size();j++)
				{
					int id=i+j;
					if(id>=P.size())id-=P.size();
					double Y=P[id]-Q[j];
					if(Y<0)Y+=2*M_PI;
					if(!eq(X,Y))
					{
						ok=false;
						break;
					}
				}
				if(ok)now.push_back(X);
			}
			if(X.empty())X=now;
			else
			{
				sort(now.begin(),now.end());
				vector<double>nX;
				int xi=0;
				for(double p:now)
				{
					while(xi<X.size()&&X[xi]+EPS<p)xi++;
					if(xi<X.size()&&eq(X[xi],p))
					{
						xi++;
						nX.push_back(p);
					}
				}
				X=nX;
			}
			if(X.empty())break;
		}
		if(!X.empty())
		{
			cout<<"Yes"<<endl;
			return 0;
		}
	}
	cout<<"No"<<endl;
}

#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>
#include <locale>
#include <memory>
#include <stdexcept>
#include <utility>
#include <string>
#include <fstream>
#include <ios>
#include <iostream>
#include <iosfwd>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <streambuf>
#include <complex>
#include <numeric>
#include <valarray>
#include <exception>
#include <limits>
#include <new>
#include <typeinfo>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <climits>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdlib>
#include <cstddef>
#include <cstdarg>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cwchar>
#include <cwctype>
using namespace std;
static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const double PI2 = 8.0 * atan(1.0);
using ll = long long;
using ull = unsigned long long;

#define ALL(c) (c).begin(), (c).end()
#define CLEAR(v) memset(v,0,sizeof(v))
#define MP(a,b) make_pair((a),(b))
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ABS(a) ((a)>0?(a):-(a))
template<class T> T MIN(const T& a, const T& b) { return a < b ? a : b; }
template<class T> T MAX(const T& a, const T& b) { return a > b ? a : b; }
template<class T> void MIN_UPDATE(T& a, const T& b) { if (a > b) a = b; }
template<class T> void MAX_UPDATE(T& a, const T& b) { if (a < b) a = b; }

constexpr int mo = 1000000007;
int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
int qp(int a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int N;
vector<complex<double>> S;
vector<complex<double>> T;

bool check(int from, int to) {
	vector<complex<double>> TT;
	complex<double> offset = T[from];
	double theta = arg(T[to] - T[from]);
	for (auto p : T) {
		p -= offset;
		p /= polar(1.0, theta);
		TT.push_back(p);
	}

	for (int i = 0; i < N; ++i) {
		bool found = false;
		for (int j = 0; !found && j < N; ++j) {
			if (abs(S[i] - TT[j]) < EPS) {
				found = true;
			}
		}

		if (!found) {
			return false;
		}
	}
	return true;
}

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

	cin >> N;
	if (N == 1) {
		cout << "Yes" << endl;
		return 0;
	}

	REP(n, N) {
		double a, b;
		cin >> a >> b;
		S.emplace_back(a, b);
	}
	complex<double> offset = S[0];
	double theta = arg(S[1] - S[0]);
	for (auto& p : S) {
		p -= offset;
		p /= polar(1.0, theta);
	}

	REP(n, N) {
		double c, d;
		cin >> c >> d;
		T.emplace_back(c, d);
	}

	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < N; ++j) {
			if (i == j) {
				continue;
			}

			if (check(i, j)) {
				cout << "Yes" << endl;
				return 0;
			}
		}
	}
	cout << "No" << endl;
}


#include<bits/stdc++.h>
#define ll long long
#define ri register int
using namespace std;
int main(){
    int a,b;
    cin>>a>>b;
    if(a == b) cout<<a<<endl;
    else cout<<(3-a-b)<<endl;
    return 0;
}

#include <bits/stdc++.h>

int main() {
  using namespace std;
  cin.tie(nullptr)->sync_with_stdio(false);
  int n;
  cin >> n;
  string s;
  cin >> s;
  if (s[0] != s.back()) {
    cout << "1\n";
    exit(0);
  } else {
    for (int i = 1; i + 1 < n - 1; ++i)
      if (s[i] != s[0] and s[i + 1] != s[0]) {
        cout << "2\n";
        exit(0);
      }
  }
  cout << "-1\n";
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int v,t,s,d;
    cin>>v>>t>>s>>d;
    float x = (float)d/v;
    int flag =1;
    if(x>=(float)t && x<=(float)s)
      flag =0;

    if(flag == 1)
       cout<<"Yes"<<endl;
    else
       cout<<"No"<<endl;
    return 0;
}

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < (t); ++i)
#define dsrep(i,t,s) for(int i = (t)-1; i >= (s); --i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define bn(x) ((1<<(x))-1)
#define newline puts("")
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using PQ = priority_queue<T,vc<T>,greater<T> >;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using P = pair<int,int>;
using T3 = tuple<int,int,int>;
using vi = vc<int>;
using vvi = vv<int>;
using vl = vc<ll>;
using vp = vc<P>;
using vt = vc<T3>;
int getInt(){int x;scanf("%d",&x);return x;}
vi pm(int n, int s=0) { vi a(n); iota(rng(a),s); return a;}
template<typename T>istream& operator>>(istream&i,vc<T>&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const vc<T>&v,const string& d=""){stringstream s;rep(i,sz(v))(i?s<<d:s)<<v[i];return s.str();}
template<typename T>ostream& operator<<(ostream&o,const vc<T>&v){if(sz(v))o<<join(v," ");return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
vc<string> split(const string& s,char d=' '){vc<string> r(1);for(char c:s)if(c==d)r.pb("");else r.back()+=c;return r;}
string operator*(const string& s,int t){return join(vc<string>(t,s));}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>T dup(T x, T y){return (x+y-1)/y;}
template<typename T>ll suma(const vc<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}
template<typename T>void uni(vc<T>& a){sort(rng(a));a.erase(unique(rng(a)),a.end());}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;

struct Solver {
  void solve() {
    int k,n,m;
    scanf("%d%d%d",&k,&n,&m);
    vl a(k);
    cin>>a;
    vl b(k);
    rep(i,k) b[i] = a[i]*m/n;
    int r = m-suma(b);
    assert(0 <= r && r < k);
    vp p;
    rep(i,k) p.eb(a[i]*m%n,i);
    sort(rrng(p));
    rep(i,r) b[p[i].se]++;
    cout<<b<<endl;
  }
};

int main() {
  int ts = 1;
  // scanf("%d",&ts);
  rrep(ti,ts) {
    Solver solver;
    solver.solve();
  }
  return 0;
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
typedef long long ll;
using namespace std;
using P=pair<int,int>;
int main()
{
    string s;
    cin>>s;
    string t="ZONe";
    int ans=0;
    // rep(i,s.size()){
    //     if(s[i]==t[0]) cnt++;
    //     if(cnt==1&&s[i]==t[1])cnt++;
    //     if(cnt==2&&s[i]==t[2]) cnt++;
    //     if(cnt==3&&s[i]==t[3]) cnt++;
    //     if(cnt==4){
    //         ans++;
    //         cnt=0;
    //     }
    // }
    rep(i,s.size()-t.size()+1){
        int cnt=0;
        rep(j,t.size()){
            if(s[j+i]==t[j]) cnt++;
            else {
                cnt=0;
                continue;
            }
            if(cnt==4) {
                ans++;
                cnt=0;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false);  cin.tie(NULL)
#define ll unsigned long long int
#define Test ll t; cin>>t; while(t--)
#define en cout<<endl;
#define ub upper_bound
#define lb lower_bound
#define pb push_back
#define PI 3.141592653589793238
#define str string s; cin>>s; ll len=s.length();
#define flag ll flag=0;
#define MAX 1000000000
#define pob pop_back
using namespace std;

int main()
{ fast;
   ll n,ans=0;
   cin>>n;
   vector<char> v;
   char ar[n];
   for(ll i=0;i<n;i++) cin>>ar[i];
   for(ll i=0;i<n;i++)
   {
       v.pb(ar[i]);
       ll k=v.size();
       if(k>=3)
       {
           if(v[k-1]=='x' && v[k-2]=='o' && v[k-3]=='f')
           {ans++;
               v.pob(); v.pob(); v.pob();
           }
       }
   }
   cout<<n-(3*ans);
}


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

int main()
{
    int N,M;
    string a;

    cin >> N >> M;

    for(int i=0; i<M; i++) {
        cin >> a;
    }

    for(int y=0; y<N; y++) {
        for(int x=0; x<N; x++)
        cout << ".";
        cout << endl;
    }
};

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

vector<vector<char>> mat;
vector<pair<int,string>> in;
vector<string> in2;
vector<string> in3;
vector<string> in4;
vector<string> in5;
vector<string> in6;
vector<string> in7;
vector<string> in8;
vector<string> in9;
vector<string> in10;
vector<string> in11;
vector<string> in12;
vector<string> concat_in;
int n,m;

void answer_print(){
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cout << mat[i][j] ;
            if(j==n-1) cout << endl;
        }
    }
}

bool isPut(int x,int y,string str){
    if(mat[y][x] != '.') return false;
    if(n-x+1 < str.size()) return false;
    return true;
}

void Put(int x,int y,string str){
    for(int i = x;i<x+str.size();i++){
        mat[y][i] = str[i-x];
    }
}

void solve(){
    int k=0;

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(isPut(j,i,concat_in[k])) {
                Put(j,i,concat_in[k]);
                k++;
            }
        }
    }
}

int main(){

    // input
    cin >> n >> m;
    mat.resize(n);in.resize(m);
    for(int i=0;i<n;i++) mat[i].resize(n,'.');

    for(int i=0;i<m;i++) {
        cin >>in[i].second;
        in[i].first = in[i].second.size();
    }

    for(int i=0;i<in.size();i++){
        switch(in[i].first){
            case 2 : in2.push_back(in[i].second);break;
            case 3 : in3.push_back(in[i].second);break;
            case 4 : in4.push_back(in[i].second);break;
            case 5 : in5.push_back(in[i].second);break;
            case 6 : in6.push_back(in[i].second);break;
            case 7 : in7.push_back(in[i].second);break;
            case 8 : in8.push_back(in[i].second);break;
            case 9 : in9.push_back(in[i].second);break;
            case 10 : in10.push_back(in[i].second);break;
            case 11 : in11.push_back(in[i].second);break;
            case 12 : in12.push_back(in[i].second);break;
            default: break;
        }
    }
    for(int i=0;i<in2.size();i++) concat_in.push_back(in2[i]);
    for(int i=0;i<in3.size();i++) concat_in.push_back(in3[i]);
    for(int i=0;i<in4.size();i++) concat_in.push_back(in4[i]);
    for(int i=0;i<in5.size();i++) concat_in.push_back(in5[i]);
    for(int i=0;i<in6.size();i++) concat_in.push_back(in6[i]);
    for(int i=0;i<in7.size();i++) concat_in.push_back(in7[i]);
    for(int i=0;i<in8.size();i++) concat_in.push_back(in8[i]);
    for(int i=0;i<in9.size();i++) concat_in.push_back(in9[i]);
    for(int i=0;i<in10.size();i++) concat_in.push_back(in10[i]);
    for(int i=0;i<in11.size();i++) concat_in.push_back(in11[i]);
    for(int i=0;i<in12.size();i++) concat_in.push_back(in12[i]);

    solve();

    answer_print();

}


/* これを翻訳している間、あなたはあなたの人生のいくつかの貴重な瞬間を無駄にしました */

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

#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define S(x) (int)(x).size()
#define L(x) (int)(x).length()
#define ld long double
#define mem(x,y) memset(x,y,sizeof x)

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update> ordered_set;

const int mod = 1e9+7;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
const int maxn = 60;

ll C[maxn + 1][maxn + 1];

void solve()
{
	int a,b;
	cin>>a>>b;
	ll K;
	cin>>K;
	/*string s = "aaabbb";
	do
	{
		cout<<s<<'\n';
	}while(next_permutation(all(s)));
	*/
	C[0][0] = 1;
	for (int n = 1; n <= maxn; ++n)
	{
	    C[n][0] = C[n][n] = 1;
	    for (int k = 1; k < n; ++k)
	    C[n][k] = C[n - 1][k - 1] + C[n - 1][k];
	}
	int n=a+b;
	for(int i=0;i<n;i++)
	{
		if(a+b==1)
		{
			if(a==1) cout<<'a';
			else cout<<'b';
			break;
		}
		if(a>0)
		{
			if(C[a+b-1][b] >= K)
			{
				a--;
				cout<<'a';
				continue;
			}
			K-=C[a+b-1][b];
			b--;
			cout<<'b';
		}
		else
		{
			b--;
			cout<<'b';
		}
	}
	cout<<'\n';
}

int main()
{
	IOS
	int t=1;
	//cin>>t;
	while(t--)
	{
		solve();
	}
}

#include<bits/stdc++.h>
using namespace std;
int a,b;
long long k;
long long C(int x,int y){
	if(x<0||x<y)return 0;
    long long ans=1;
    for(long long i=0;i<y;++i)ans*=x-i,ans/=i+1;
    return ans;
}
int main(){
	scanf("%d%d%lld",&a,&b,&k);
	while(a||b){
		if(a&&k<=C(a+b-1,a-1))putchar('a'),a--;
		else k-=C(a+b-1,a-1),putchar('b'),b--;
	}
	return 0;
}


#include <cstdio>
using namespace std;

using LL = long long;

char s[7], t[7];

int score(const char* cards)
{
	int cnt[10];
	for(int i=0; i<10; i++) cnt[i] = i;
	for(int i=0; i<5; i++)
		cnt[cards[i] - '0'] *= 10;
	int res = 0;
	for(int x: cnt) res += x;
	return res;
}

int main()
{
	int k;
	scanf("%d%s%s", &k, s, t);
	int cnt[10];
	for(int i=1; i<10; i++) cnt[i] = k;
	for(int i=0; i<4; i++)
		cnt[s[i] - '0'] --,
		cnt[t[i] - '0'] --;
	LL win = 0LL;
	for(int x=1; x<10; x++)
		if(cnt[x])
		{
			s[4] = '0' + x;
			int sscore = score(s);
			for(int y=1; y<10; y++)
				if(cnt[y])
				{
					t[4] = '0' + y;
					if(sscore > score(t))
						win += cnt[x] * LL(cnt[y] - (x == y));
				}
		}
	LL tmp = 9LL * k - 8LL;
	printf("%.8lf\n", double(win) / tmp / double(tmp - 1LL));
	return 0;
}

#include<iostream>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
int main()
{
  int n,i;
  ll s=0,res=0;
  map<ll,ll> m;
  decltype(m)::iterator it;
  cin >> n;
  vector<ll> a(n);
  for(i=0;i<n;i++)
    cin >> a[i];
  m[0]=1;
  for(i=0;i<n;i++){
    if(i%2==0)
      s+=a[i];
    else
      s-=a[i];
    it=m.find(s);
    if(it!=m.end())
      it->second++;
    else
      m.insert(make_pair(s,1));
  }
  for(it=m.begin();it!=m.end();it++){
    s=it->second;
    res+=s*(s-1)/2;
  }
  cout << res << endl;
  return 0;
}


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

#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf
#define MOD 1000000007

ll myceil(ll a,ll b){return (a + (b - 1)) / b;}
ll myfloor(ll a,ll b){return a / b;}
int ReLU(int x){return x >= 0 ? x : 0;}
int gcd(int a, int b){ return (a % b) ? gcd(b, a % b) : b;}
int lcm(int a, int b){ return (a / gcd(a, b)) * b;}
template<class t,class u> void chmax(t& a,u b){if(a < b) a = b;}
template<class t,class u> void chmin(t& a,u b){if(b < a) a = b;}
template<class T> void inner_product(const vector<pair<T, T>>& vec, T& product){for(const auto& e:vec) product += e.first * e.second;}

int factorial(int n){
    if(n == 0) return 1;
    else return factorial(n - 1) * n;
}
int is_prime(int x){
    if(x <= 1) return 0;
    for(int i = 2; i * i <= x; i++){
        if(x % i == 0) return 0;
    }
    return 1;
}
int ngcd(int n, int a[])
{
    int i, d;
    d = a[0];
    for (i = 1; i < n && d != 1; i++)
        d = gcd(a[i], d);
    return d;
}
int my_calc(const int a, const int b, const char& ope){
    if(ope == '+') return a + b;
    else if(ope == '-') return a - b;
    else return 0;
}

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

    int n, a, b;
    cin >> n >> a >> b;

    int ans = n - a + b;

    cout << ans << '\n';

    return 0;
}

#include <iostream>

using namespace std;

int main(){
   int a, b, c, d;

   cin >> a >> b >> c >> d;

   cout << b - c << endl;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <map>
using namespace std;

int main() {
  int N;
  cin>>N;
  cout<<N-1<<endl;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
     long long int n;
     cin>>n;
     long long int r=sqrt(2*n);
     long long int ans=0;
     for(long long int i=1;i<=r;i++)
     {
         if(i%2!=0)
         {

            if(n%i==0)
            {
              long long int temp=n/i;
              long long int diff=i/2;
              long long int gr=i;
              long long int smallest=temp-diff;
              if(temp-diff>0&&(gr+(2*smallest-1))>r)
              {
                  ans+=2;
              }
              else
              {
                  ans++;
              }

            }
         }
         else
         {
             long long int check=i/2;
             long long int div=n%i;
             if(div==check)
             {
                 long long int temp_n=n-check;
                 temp_n=temp_n/i;
                 long long int new_a=temp_n-(check-1);
                 long long int gr=i;
                long long int smallest=new_a;
                if(smallest>0&&(gr+(2*smallest-1))>r)
                {
                    ans+=2;
                }
                else
                {
                    ans++;
                }
                 //ans++;
             }

         }

     }
     cout<<ans;
return 0;
}


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

const int maxn = 2e5+8, inf = 1e18+9, mod  = 1e9+7;

int n, m, a[maxn], b[maxn], p[maxn], vs[maxn];

void solve() {
    int i, j;
    //cin >> n;
    scanf("%lld", &n);
    vector<pair<int, int>> vec;
    for (i = 1; i <= n; i++) scanf("%lld", &a[i]); //cin >> a[i];
    for (i = 1; i <= n; i++) scanf("%lld", &b[i]); //cin >> b[i];
    for (i = 1; i <= n; i++) scanf("%lld", &p[i]); //cin >> p[i];
    for (i = 1; i <= n; i++) if (a[i] <= b[p[i]] && i != p[i]) return (void)(printf("-1\n"));
    for (i = 1; i <= n; i++) if (!vs[i]) {
        int start = i; vs[i] = 1;
        if (i == p[i]) continue;
        for (j = p[i]; j != i; j = p[j]) {
            vs[j] = 1;
            if (a[j] > a[start]) start = j;
        }
        for (j = p[start]; j != start; j = p[j]) vec.push_back({start, j});
    }
    //cout << vec.size() << endl;
    printf("%lld\n", (int)vec.size());
    for (auto [x, y] : vec) printf("%lld %lld\n", x, y); //cout << x << " " << y << endl;
}

signed main() {
    //ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); //cout << fixed << setprecision(15);
    int t = 1; //cin >> t;
    while (t--) solve();
    return 0;
}

#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<list>
#include<bitset>
#include<stack>
#include<memory>
#include<numeric>
#include <utility>

using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for(int i = 0;i < (n);i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod=998244353;
const llint inf=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-10;
template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}

int main(void){
	//限界値が軽い方から処理
	int n,i;cin>>n;
	vector<int>a(n);
	vector<int>b(n);
	vector<int>p(n);
	vector<int>g(n);
	vector<pair<int,int>>sa(n);
	for(i=0;i<n;i++){cin>>a[i];sa[i]=mp(a[i],i);}
	for(i=0;i<n;i++){cin>>b[i];}
	for(i=0;i<n;i++){cin>>p[i];p[i]--;g[p[i]]=i;}
	for(i=0;i<n;i++){
		if(p[i]!=i){if(a[i]<=b[p[i]]){cout<<-1<<endl;return 0;}}
	}
	SO(sa);
	vector<pair<int,int>>ans;
	for(i=0;i<n;i++){
		int t=sa[i].sec;
		if(t==p[t]){continue;}
		int s=g[t];
		swap(p[t],p[s]);
		swap(g[p[t]],g[p[s]]);
			ans.pub({t+1,s+1});
	}
	cout<<ans.size()<<endl;
	for(auto it:ans){cout<<it.fir<<" "<<it.sec<<endl;}
}


#include <bits/stdc++.h>

#define MOD 1000000007
#define MAXN 1000000
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define vd vector<double>
#define vll vector<long long>
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define pi pair<int, int>
#define pll pair<long long, long long>
#define mp make_pair
#define fi first
#define se second
#define rep(i,s,n) for(ll i = s;i <= n;i++)
#define repi(n) for (int i = 0; i < n; i++)
#define repj(n) for (int j = 0; j < n; j++)
#define deb_vec(a) for(auto i : a) cout << i << " "; cout << "\n"
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
#define t_cases ll t; cin >> t; while(t--)
#define debug(a) cout << a << "\n"

using namespace std;

void solve() {

    int a, b, c; cin >> a >> b >> c;
    if (a >= 0 and b >= 0) {

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

    } else if (a < 0 and b >= 0) {

        if (c & 1) cout << "<";
        else if (abs(a) > abs(b)) cout << ">";
        else if (abs(a) < abs(b)) cout << "<";
        else cout << "=";

    } else if (a >= 0 and b < 0) {

        if (c & 1) cout << ">";
        else if (abs(a) > abs(b)) cout << ">";
        else if (abs(a) < abs(b)) cout << "<";
        else cout << "=";

    } else {

        if (c & 1) {
            if (abs(a) > abs(b)) cout << "<";
            else if (abs(a) < abs(b)) cout << ">";
            else cout << "=";
        } else {
            if (abs(a) > abs(b)) cout << ">";
            else if (abs(a) < abs(b)) cout << "<";
            else cout << "=";
        }

    }

}

int main() {

    FAST;

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

    solve();

}

/*
*/

#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <assert.h>
#include <math.h>
#include <memory.h>
#include <stdio.h>

#include <algorithm>
#include <complex>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd
#define left asdgashgrketwjklrej
#define have adsgagshdshfhds
#define ends asdgahhfdsfshdshfd
#define prev asdgSHJsfgsdfhdsh
#define hash asdgasdgasdgdfrywewery

#define eps 1e-12
#define M_PI 3.141592653589793
#define bsize 1024

#define ldouble long double
using namespace std;

const int bs = 1000000007;

const int N = 200031;

int tests;

long long count_splits(long long len){
	// we have to split len over 3 blocks, zeroes allowed.
	// this is C(len+2,2)
	long long A=len+2;
	long long B=len+1;
	if (A%2)
		B/=2;
	else
		A/=2;
	return A*B%bs;
}

long long solver(long long n,long long a,long long b){
	long long free_spots=n-a-b;
	if (free_spots<0)
		return 0;
	return count_splits(free_spots);
}

int main() {
    //freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    // cin.tie(0);

    cin>>tests;
    for (;tests;--tests){
    	long long n,a,b;
    	cin>>n>>a>>b;
    	long long ans=solver(n,a,b);
    	ans=ans*2%bs;
    	long long all_placements=1ll*(n-a+1)*(n-b+1)%bs;
    	long long bad=all_placements-ans;
    	if (bad<0)
    		bad+=bs;
    	long long really_bad=1ll*bad*bad%bs;
    	long long good=all_placements*all_placements%bs-really_bad;
    	if (good<0)
    		good+=bs;
//    	cout<<bad<<" "<<really_bad<<" "<<all_placements<<endl;
    	cout<<good<<endl;
    }

    //cin.get(); cin.get();
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int,int>
#define piii pair<int,pii>
#define sp " "
#define nl "\n"
#define all(x) x.begin(),x.end()
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define int ll

using namespace std;

constexpr int N = 2e5+5;
constexpr int INF = 1e9+5;
constexpr int mod = 1e9+7;

int n,ans,st[N];

void update(int x,int val){
    for(x++;x<=n;x+=x&-x)
        st[x]+=val;
}

int get(int x){
    int res=0;
    for(x++;x>0;x-=x&-x)
        res+=st[x];
    return res;
}

signed main(){
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    fastio()
    cin >> n;
    vector<pii> a(n),b(n);
    for(int i=0;i<n;i++){
        int x;
        cin >> x;
        a[i].ff=x+i;
        a[i].ss=i;
    }
    for(int i=0;i<n;i++){
        int x;
        cin >> x;
        b[i].ff=x+i;
        b[i].ss=i;
    }
    sort(all(a));
    sort(all(b));
    vector<int> s(n);
    for(int i=0;i<n;i++){
        if(a[i].ff!=b[i].ff){
            cout << -1 << nl;
            return 0;
        }
        s[a[i].ss]=b[i].ss;
    }
    for(int i=n-1;i>=0;i--){
        ans+=get(s[i]);
        update(s[i],1);
    }
    cout << ans << nl;
}

#include<bits/stdc++.h>
#include<vector>
#include<math.h>
#include<string>
using namespace std;
#define MOD 998244353
#define MAX 200005
#define PMAX 18
#define PRECISION 0.000001
#define INF 1000000000000000000
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
vector<int> pos[MAX*2][2];
int arr[MAX][2];
int permut[MAX];
map<int,int> M;
int segment_tree[MAX*4];
void update(int index,int l,int r,int ind)
{
    if(l>ind||r<ind) return;
    if(l==r)
    {
        segment_tree[index]=1;
        return;
    }
    int m=(l+r)/2;
    update(index*2,l,m,ind);
    update(index*2+1,m+1,r,ind);
    segment_tree[index]=segment_tree[index*2]+segment_tree[index*2+1];
}
int query(int index,int l,int r,int L,int R)
{
    if(l>R||r<L) return 0;
    if(L<=l&&r<=R)
    {
        return segment_tree[index];
    }
    int m=(l+r)/2;
    return query(index*2,l,m,L,R)+query(index*2+1,m+1,r,L,R);
}
int main()
{
    int n,i,j;
    scanf("%d",&n);
    for(j=0;j<2;j++)
    {
        for(i=0;i<n;i++)
        {
            scanf("%d",&arr[i][j]);
        }
        for(i=0;i<n-1-i;i++)
        {
            swap(arr[i][j],arr[n-1-i][j]);
        }
    }
    for(j=0;j<2;j++)
    {
        for(i=0;i<n;i++)
        {
            int ss=M.size();
            if(!M.count(arr[i][j]-i))
            {
                M[arr[i][j]-i]=ss;
                ss++;
            }
            pos[M[arr[i][j]-i]][j].push_back(i);
        }
        //M.clear();
    }
    for(i=0;i<MAX;i++)
    {
        int a=pos[i][0].size(),b=pos[i][1].size();
        if(a!=b)
        {
            printf("-1");
            return 0;
        }
        for(j=0;j<pos[i][0].size();j++)
        {
            permut[pos[i][1][j]]=pos[i][0][j];
            //printf("%d<-%d\n",pos[i][1][j],pos[i][0][j]);
        }
    }
    long long ans=0;
    for(i=0;i<n;i++)
    {
        //printf("%d ",permut[i]);
        if(permut[i]<n-1)
        {
            ans=ans+query(1,0,n-1,permut[i]+1,n-1);
        }
        update(1,0,n-1,permut[i]);
    }
    //printf("\n");
    printf("%lld",ans);
    return 0;
}


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

typedef long long ll;

int main() {
    int n, k;
    string s;
    cin >> n >> k >> s;
    rep(i, k) {
        string t = "";
        rep(j, n) {
            char c = s[(j * 2) % n], d = s[(j * 2 + 1) % n];
            if (c == d) t.push_back(c);
            else if (c == 'R' && d == 'S') t.push_back(c);
            else if (c == 'P' && d == 'R') t.push_back(c);
            else if (c == 'S' && d == 'P') t.push_back(c);
            else if (c == 'S' && d == 'R') t.push_back(d);
            else if (c == 'R' && d == 'P') t.push_back(d);
            else if (c == 'P' && d == 'S') t.push_back(d);
        }
        s = t;
    }
    cout << s[0] << "\n";
}