#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define int           long long
#define S             second
#define F             first
#define pb            push_back
#define all(c)        (c).begin(),(c).end()
#define rall(c)       (c).rbegin(),(c).rend()
#define lb            lower_bound
#define ub            upper_bound
#define si(c)         (int)((c).size())
#define lcm(a, b)      (a * (b / __gcd(a,b)))
#define inf           (int)(1e10)
#define endl          '\n'
#define mp            make_pair
#define time(s)       (double(clock()-s)/double(CLOCKS_PER_SEC))
#define debug(args...)    _F(#args, args)
#define vi            std::vector<int>
#define pii           pair<int, int>
#define vpi           vector<pii>
#define ordered_set   tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

template<typename T> void _F(const char *name, T arg1){ cerr << name << " = " << arg1 << endl;}
template<typename T, typename... Args> void _F(const char *names, T arg1, Args... args)
{ const char *name = strchr(names, ',');cerr.write(names, name-names) << " = " << arg1 << endl;_F(name+2, args...);}

template< typename T1, typename T2 > istream& operator>>(istream& in, pair<T1, T2> &q){ in >> q.F >> q.S; return in;}
template< typename T1, typename T2 > ostream& operator<<(ostream& out, pair<T1, T2> &q){ out << q.F << " " << q.S; return out;}
template< typename T1, typename T2 > pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F+p2.F, p1.S+p2.S};}
template< typename T1, typename T2 > pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F-p2.F, p1.S-p2.S};}
template< typename T1, typename T2 > bool operator<(pair<T1, T2> p1, pair<T1, T2> p2){ return p1 < p2 ;}

template<typename T> void Unique(vector<T> &v) {
    sort(all(v)), v.resize(distance(v.begin(), unique(all(v))));
}

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

void solve() {
	int n;
	cin >> n;

	int p[n], pos[n];
	for(int i = 0; i < n; i++) {
		cin >> p[i], p[i]--;
		pos[p[i]] = i;
	}
	vpi ans;
	int mx = 0;
	for(int i = 0; i < n-1; i++) {
		if(pos[i] == i && mx == i) No();
		int t = pos[i];
		for(int j = pos[i]; j > max(i, mx); j--) {
			ans.pb(mp(j, j-1));
			swap(p[j], p[j-1]);
			swap(pos[p[j]], pos[p[j-1]]);
		}
		mx = max(mx, t);
		// cout << mx << endl;
		// for(int j = 0; j < n; j++)
		// 	cout << p[j] << " ";
		// cout << endl;
	}
	for(int i = 0; i < n; i++) {
		if(p[i] != i)No();
	}
	for(auto u : ans) {
		u.F += 1, u.S += 1;
		cout << u.S << endl;
	}
}

signed main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    start = clock();

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

    int test = 1;
    // cin >> test;

    cout << fixed << setprecision(12);

    for(int i = 1; i <= test; ++i){
    	solve();
    }

    cerr << time(start);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll,ll>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main(){
  int n;
  cin>>n;
  vector<int> p(n);
  rep(i,n) cin>>p[i];
  int tmp=0;
  vector<int> ans;
  bool flag=0;
  while(tmp!=n-1){
    for(int i=tmp+1; i<n; i++){
      if(p[i]==tmp+1){
        for(int a=i; a>=tmp+1; a--){
          ans.push_back(a);
          p[a]=p[a-1];
        }
        p[tmp]=tmp+1;
        tmp=i;
        break;
      }
      if(i==n-1) flag=1;
    }
    if(flag) break;
  }
  bool ok=1;
  rep(i,n){
    if(p[i]!=i+1) ok=0;
  }
  if(ok && ans.size()==n-1){
    rep(i,n-1) cout<<ans[i]<<" ";
    cout<<endl;
  }
  else cout<<-1<<endl;
}


	/*It is better to taste failure
	  than to regret*/

            #include <algorithm>
        #include <bitset>
        #include <cassert>
        #include <chrono>
        #include <cmath>
        #include <complex>
        #include <cstdio>
        #include <cstdlib>
        #include <cstring>
        #include <ctime>
        #include <deque>
        #include <functional>
        #include <iomanip>
        #include <iostream>
        #include <iterator>
        #include <limits>
        #include <climits>
        #include <list>
        #include <map>
        #include <numeric>
        #include <queue>
        #include <random>
        #include <ratio>
        #include <set>
        #include <sstream>
        #include <stack>
        #include <string>
        #include <unordered_map>
        #include <unordered_set>
        #include <utility>
        #include <vector>
        using namespace std;

        typedef long long ll;
        typedef long double LD;
        #define int ll
        #define double LD
        #define pb push_back
        #define mp make_pair
        #define REP(i,n) for (int i = 0; i < n; i++)
        #define loop(i,n) for (int i = 0; i < n; i++)
        #define FOR(i,a,b) for (int i = a; i < b; i++)
        #define REPD(i,n) for (int i = n-1; i >= 0; i--)
        #define FORD(i,a,b) for (int i = a; i >= b; i--)
        #define remax(a,b) a = max(a,b)
        #define remin(a,b) a = min(a,b)
        #define mem(a, b) memset(a, b, sizeof a)
        #define append push_back
        #define all(v) v.begin(),v.end()
        #define F first
        #define S second
        #define print(x) cout << (x) << endl
        #define PRECISION(x) cout << fixed << setprecision(x)
        #define FAST_IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
        #define Test(){int t;cin >> t;for(int i=1;i<=t;i++){cout << "Case #" << i << ": ";solve();}}
        #define test(){int t; cin>>t; for(int i=1;i<=t;i++){solve();}}
        #define vi vector<int>
        #define vii vector<pair<int,int> >
        #define vci vector<pair<char,int> >
        #define vic vector<pair<int,char> >
        #define N 200005
        #define pb push_back
        const double PI=acos(-1);
        const int MOD = 1000000007;
        const int FMOD = 998244353;

        void solve()
        {
        	int a,b,c,d;
        	cin>>a>>b>>c>>d;

        	cout<<a*d-b*c<<endl;
		}
        signed main()
        {
        //	test();
        solve();
		}

#include<iostream>

using namespace std;
int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    cout<<a*d-b*c;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>

#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll> > mat;

string s[2005];
int n,m,dis[2005][2005],used[26];

bool good(int i,int j)
{
	if(i<0||i>=n||j<0||j>=m)return 0;
	if(dis[i][j]>-1)return 0;
	if(s[i][j]=='#')return 0;
	return 1;
}

int main()
{ios_base::sync_with_stdio(false);
cin>>n>>m;
rep(i,0,n-1)cin>>s[i];
rep(i,0,n-1)rep(j,0,m-1)dis[i][j]=-1;

queue<pii> q;

rep(i,0,n-1)rep(j,0,m-1)
{
	if(s[i][j]=='S')
	{
		q.push({i,j});
		dis[i][j]=0;
	}
}

while(!q.empty())
{
	pii p=q.front();
	q.pop();
	char c=s[p.ff][p.ss];
	int d=dis[p.ff][p.ss];
	if(c>='a'&&c<='z')
	{
		if(used[c-'a']==0)
		{
			used[c-'a']=1;
			rep(i,0,n-1)rep(j,0,m-1)
			{
				if(dis[i][j]==-1&&s[i][j]==c)
				{
					dis[i][j]=1+d;
					q.push({i,j});
				}
			}
		}
	}
	if(good(p.ff-1,p.ss))
	{
		q.push({p.ff-1,p.ss});
		dis[p.ff-1][p.ss]=1+d;
	}
	if(good(p.ff+1,p.ss))
	{
		q.push({p.ff+1,p.ss});
		dis[p.ff+1][p.ss]=1+d;
	}
	if(good(p.ff,p.ss-1))
	{
		q.push({p.ff,p.ss-1});
		dis[p.ff][p.ss-1]=1+d;
	}
	if(good(p.ff,p.ss+1))
	{
		q.push({p.ff,p.ss+1});
		dis[p.ff][p.ss+1]=1+d;
	}
}
rep(i,0,n-1)
{
	rep(j,0,m-1)
	{
		if(s[i][j]=='G')ol(dis[i][j]);
	}
}
return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int cnt;
ll n;
unordered_set<ll> factor;

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

    for (int i = 1; (ll)i * i <= 2 * n; ++i)
    {
        if (2 * n % i == 0)
        {
            factor.insert(i);
            factor.insert(2 * n / i);
        }
    }

    for (auto item: factor)
        cnt += (((2 * n / item) - item + 1) % 2 == 0);

    cout << cnt;
}

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

int n;
vector<int> a;
int k;
vector<pair<int, int>> c;
int m;
int maxi = 0;
void f(vector<int> a, vector<pair<int, int>> v, int k) {
	if(k == 0) {
		int cc = 0;
		// for(auto e: a) cout << e << ' ';
		// cout << endl;
		for(int i = 0; i < m; i++) {
			// cout << c[i].first << c[i].second << endl;
			if((a[c[i].first] > 0) and (a[c[i].second] > 0)) {
				cc++;
			}
		}
		// cout << cc << endl;
		maxi = max(maxi, cc);
		return;
	}
	// cout << v[k].first << ' ' << v[k].second << endl;
	a[v[k].first] += 1;
	f(a, v, k - 1);
	a[v[k].first] -= 1;
	a[v[k].second] += 1;
	f(a, v, k - 1);
	a[v[k].second] -= 1;
}

int32_t main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t;
    t = 1;
    while(t--) {
    	int n;
    	cin >> n;
    	// si - sx = n
    	// si - n = sx
    	//
    	// i*(i+1) - 2n = x(x+1)
    	//2n + x*(x+1) = (i * (i + 1))
    	// 2n + x2 + x = i2+i
    	// 2n = i2 - x2 + i - x
    	// 2n = (i-x)(i + x + 1)
    	// 2n = l(l + 2x + 1)
    	//
    	int ans = 0;
    	for(int l = 1; l * (l + 1) <= 2 * n; l++) {
    		if((2 * n) % l) continue;
    		int y = (2 * n);
    		y /= l;
    		int z = y - l - 1;
    		if(z % 2 == 0) ans += 2;
    	}
    	cout << ans;
    	return 0;
    }

    return 0;
}


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

using ll = long long;
constexpr int INF = (int)1e9;
constexpr long long LINF = (long long)1e18;
//constexpr long long MOD = 1e9 + 7;
constexpr long long MOD = 998244353;
constexpr double pi = 3.141592653589793238;

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

    set<int> ans;
    for (int i = 0; i < n; ++i) {
        int a;
        cin >> a;
        if (ans.count(a)) {
            cout << "No" << endl;
            return 0;
        }
        else {
            ans.insert(a);
        }
    }

    cout << "Yes" << endl;

    return 0;
}

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

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

#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;
//using mint = modint;

const vector<int> dx = {1, 1,  1, 0,  0, 0, -1, -1, -1};
const vector<int> dy = {1, 0, -1, 1, -1, 0,  1,  0, -1};
const double PI = 3.141592653589793;
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;
const ll mod2 = 998244353;

const int MAX = 1000000;
const ll MOD = 998244353;
const ll big = 1ll<<60;

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

int main(){
   int n;
   cin >> n;
   vector<int>a(n), b(n);
   rep(i, 0, n){
       cin >> a[i];
       a[i]--;
       b[i] = i;
   }
   sort(a.begin(),a.end());
   if(a==b){
       cout << "Yes" << endl;
   }else{
       cout << "No" << endl;
   }
   return 0;
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <assert.h>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <functional>
#include <chrono>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=a;i<b;++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,a) rrepi(i,a,0)
#define rrepi(i,a,b) for(ll i=a-1;i>=b;--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
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; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
    for (auto i = first;i != last;i++){
        cin >> (*i);
    }
}
template<class itr> void array_output(itr start,itr goal){
    string ans = "",k = " ";
    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
    if (!ans.empty()) ans.pop_back();
    PRINT(ans);
}
ll gcd(ll a, ll b) {
    return a ? gcd(b%a,a) : b;
}

const ll INF = 1e18;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll MOD3 = 1e6;
const ll EPS = 1e-10;
int sgn(const double a){
    return (a < -EPS ? -1 : (a > EPS ? +1 : 0));
}
typedef pair<int,int> pi;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> tri;
typedef pair<double,double> point;
typedef complex<double> Point;
typedef string::const_iterator State;
const ll MAX = 125000;
constexpr ll nx[4] = {-1,0,1,0};
constexpr ll ny[4] = {0,1,0,-1};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    ll n;
    cin >> n;
    ll ans = INF;
    rep(i,n){
        ll a,p,x;
        cin >> a >> p >> x;
        if (x-a > 0) chmin(ans,p);
    }
    if (ans == INF) PRINT(-1);
    else PRINT(ans);
}

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int main() {
	int N;
	cin >> N;
	string S, T;
	cin >> S >> T;

	vector<int> A(N + 1), B(N + 1);
	REP(i, N) {
		A[i + 1] = A[i] ^ (S[i] == '1');
		B[i + 1] = B[i] ^ (T[i] == '1');
	}

	cout << [&]() -> ll {
		ll ans = 0;
		int pos = -1, col = 0;
		REP(i, N + 1) {
			int cur = i < pos ? A[pos] : A[i];

			if (cur != B[i]) {
				++pos;
				chmax(pos, i);
				while (pos <= N && A[pos] != B[i]) ++pos;
				if (pos > N) return -1;
				ans += pos - i;
			}
		}
		return ans;
	}() << endl;

	return 0;
}


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

int main(){
	string s;
	cin>>s;
	if(s[0]==s[1] and s[1]==s[2] and s[0]==s[2])
		cout<<"Won"<<endl;
	else
		cout<<"Lost"<<endl;
	return 0;
}

#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include<bits/stdc++.h>
using namespace std;
#define rep(i,s,t) for(ll i = (ll)(s); i < (ll)(t); i++)
#define rrep(i,s,t) for(ll i = (ll)(s-1);(ll)(t) <= i; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> Pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
constexpr ll INF = numeric_limits<ll>::max()/4;
constexpr ll n_max = 2e5+10;
#define int ll
const long double pi = 3.14159265358979323846;
const long double eps = 1e-12;

template <typename T>
int comp(T a){
    if (abs(a) < eps) return 0;
    return a > 0 ? 1 : -1;
}

template <typename T>
int comp(T a, T b){
    // a > b -> 1, a < b -> -1
    return comp(a - b);
}

template <typename A, typename B>
string to_string(pair<A, B> p);
string to_string(const string &s) {return '"' + s + '"';}
string to_string(const char *c) {return to_string((string) c);}
string to_string(bool b) {return (b ? "true" : "false");}
template <size_t N>
string to_string(bitset<N> v){
    string res = "";
    for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
    return res;
}
template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for(const auto &x : v) {
        if(!first) res += ", ";
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}

void debug_out() {cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

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

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    string s;cin >> s;
    auto YES = [](bool ok){
        cout << (ok ? "Won" : "Lost") << endl;
    };

    YES(s[0] == s[1] && s[1] == s[2]);
}


#include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
long long power(long long x, long long t) {
  long long ret = 1;
  for (; t; t >>= 1, x = x * x % P) {
    if (t & 1) {
      ret = ret * x % P;
    }
  }
  return ret;
}
int main() {
  int N, M;
  cin >> N >> M;
  long long ans = power(M, N) * N % P;
 /* cerr << "ans = " << ans << '\n';
  vector<vector<long long>> pw(5001, vector<long long> (5001));
  for (int i = 1; i <= 5000; ++i) {
    pw[i][0] = 1;
    for (int j = 1; j <= 5000; ++j) {
      pw[i][j] = pw[i][j - 1] * i % P;
    }
  }
  auto pre = pw;
  for (int i = 1; i <= 5000; ++i) {
    for (int j = 1; j <= 5000; ++j) {
      pre[i][j] += pre[i - 1][j];
      pre[i][j] %= P;
    }
  }*/
  for (int i = 1; i < N; ++i) {
    long long tmp = 0;
    for (int k = 1; k < M; ++k) {
      tmp += power(k, i - 1) * power(M, N - i - 1) % P;
    }
    if (i == 1) {
      ans -= power(M, N - 2) * (N - 1) % P;
      ans += P;
      ans %= P;
    }
    tmp = tmp * (N - i) % P;
    ans -= tmp;
    ans += P;
    ans %= P;
  } /*
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      if (j == i + 1) {
        ans -= power(M, N - 2);
        ans += P;
        ans %= P;
      }
      long long tmp = 0;
      tmp += pre[M - 1][j - i - 1];
      tmp = tmp * pw[M][N - (j - i + 1)] % P;
      ans -= tmp;
      ans += P;
      ans %= P;
      for (int k = 1; k < M; ++k) {
        ans -= power(k, j - i - 1) * power(M, N - (j - i + 1)) % P;
        ans += P;
        ans %= P;
      }
    }
  }*/
  cout << ans << '\n';
  return 0;
}

#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
#define rep(i, n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=(1LL<<60);
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    ll h, w;
    cin >> h >> w;
    ll sum = 0, mi = INF, a;
    rep(i, h)rep(j, w) {
    	cin >> a;
    	sum += a;
    	mi= min(mi, a);
    }
    cout << sum - mi * h * w << endl;
    return 0;
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
vii adj(30);
vector<bool>vis(30);
int n,m;
vector<int>comp;
void get(int v){
	comp.pb(v);
	vis[v] = 1;
	for(int x:adj[v]){
		if(!vis[x])get(x);
	}
}
ll cnt = 0;
ll ans = 1;
void re(int cur,vector<int>comp,vector<int>color){
	if(cur == sz(comp)){
		cnt++;
		return;
	}
	vector<bool>a(4,0);
	int v = comp[cur];
	for(int x:adj[v]){
		a[color[x]] = 1;
	}
	for(int i=1;i<=3;i++){
		if(!a[i]){
			color[v] = i;
			re(cur+1,comp,color);
		}
	}

}
int main()
{
	cin>>n>>m;
	for(int i=0;i<m;i++){
		int v,u;
		cin>>v>>u;
		adj[v].pb(u);
		adj[u].pb(v);
	}
	for(int i=1;i<=n;i++){
		if(!vis[i]){
			cnt = 0;
			comp.clear();
			get(i);
			//for(int x:comp)cout<<x<<" ";
			//cout<<'\n';
			vector<int>color(30);
			re(0,comp,color);
			ans*=cnt;
		}
	}
	cout<<ans;

}

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

int main()
{
  int N, M, A, B;
  cin >> N >> M;
  vector<vector<int>> graph(N);
  for (int i = 0; i < M; i++)
  {
    cin >> A >> B;
    A--, B--;
    graph.at(A).push_back(B);
    graph.at(B).push_back(A);
  }
  vector<bool> checked(N,false);
  vector<int> colors(N,-1), blank(N,-1);
  pair<queue<int>, vector<int>> now_state;
  queue<pair<queue<int>, vector<int>>> big_que;
  queue<int> que;
  int now_node;
  bool is_OK;
  long long ans = 0, tmp_ans;
  for (int i = 0; i < N; i++)
  {
    if (checked.at(i))
      continue;
    colors = blank;
    colors.at(i) = 0;
    que.push(i);
    big_que.push(make_pair(que,colors));
    que.pop();
    checked.at(i) = true;
    tmp_ans = 0;
    while (!big_que.empty())
    {
      now_state = big_que.front();
      big_que.pop();
      is_OK = true;
      while (!now_state.first.empty())
      {
        now_node = now_state.first.front();
        for (int j = 0; j < graph.at(now_node).size(); j++)
        {
          if (now_state.second.at(graph.at(now_node).at(j)) == now_state.second.at(now_node))
          {
            is_OK = false;
            break;
          }
        }
        if (!is_OK) break;
        for (int j = 0; j < graph.at(now_node).size(); j++)
        {
          if (now_state.second.at(graph.at(now_node).at(j)) >= 0)
            continue;
          now_state.second.at(graph.at(now_node).at(j)) = (now_state.second.at(now_node)+1)%3;
          now_state.first.push(graph.at(now_node).at(j));
          big_que.push(now_state);
          now_state.second.at(graph.at(now_node).at(j)) = (now_state.second.at(now_node)+2)%3;
          checked.at(graph.at(now_node).at(j)) = true;
        }
        now_state.first.pop();
      }
      if (is_OK)
        tmp_ans++;
    }
    tmp_ans *= 3;
    if (ans)
      ans *= tmp_ans;
    else
      ans += tmp_ans;
  }
  cout << ans << endl;
}


#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <tuple>
#include <numeric>
using namespace std;

int main(int argc, char* argv[]){
	int N;
	cin >> N;
	vector<int>P(N);
	for (int i=0; i<N; i++){
		cin >> P[i];
	}
	vector<int> num_list(300000, 0);
	int min=0;
	for(int i=0; i<N; i++){
		num_list[P[i]] = 1;
		for(int num=min; num<300000; num++){
			if(num_list[num]==0){
				cout << num<< endl;
				if(num > min){
					min = num;
				}
				break;
			}
		}
	}

}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ll long long

int a[200010], b[200010];
ll d[200010];

int main() {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; ++i) {
        scanf("%d", &a[i]);
    }
    for(int i = 0; i < n; ++i) {
        scanf("%d", &b[i]);
    }
    int maxa = a[0];
    d[0] = 1ll*maxa*b[0];
    printf("%lld\n", d[0]);
    for(int i = 1; i < n; ++i) {
        maxa = std::max(maxa, a[i]);
        d[i] = std::max(d[i-1], 1ll*b[i]*maxa);
        printf("%lld\n", d[i]);
    }
    return 0;
}


#include<iostream>
#include<ctype.h>
using namespace std;
int main(void)

{
    char S,T;
    cin >> S;
    cin >> T;

    if(S == 'Y')
    {
        T = toupper(T);
    }
    cout << T << endl;
    return 0;

}

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

int main () {
	char S, T;
	cin >> S >> T;

	if (S == 'Y') {
		T = T - 0x20;
		cout << T << endl;
		return 0;
	}
	if (S == 'N') {
		cout << T << endl;
		return 0;
	}
}


#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep3(i, s, n) for (int i = (s); i > (int)(n); i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define sz(x) ((int)(x).size())
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

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

    int X, Y; cin >> X >> Y;
    bool f;

    cout << (abs(X - Y) < 3 ? "Yes\n" : "No\n");

    return 0;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = ll(1e18) + 5;
struct edge {
    ll to, cost;
};

int main() {
    ll N, M;
    cin >> N >> M;
    vector<vector<edge>> g(N);
    rep(i, M) {
        ll A, B, C;
        cin >> A >> B >> C;
        --A;
        --B;
        g[A].push_back(edge{B, C});
    }

    rep(i, N) {
        vector<ll> d(N, INF);
        priority_queue<P, vector<P>, greater<P>> pq;
        pq.push(P(0, i));
        while (!pq.empty()) {
            P p = pq.top();
            pq.pop();
            if (p.first > d[p.second]) {
                continue;
            }

            for (edge e : g[p.second]) {
                if (d[e.to] > p.first + e.cost) {
                    d[e.to] = p.first + e.cost;
                    pq.push(P(d[e.to], e.to));
                }
            }
        }
        if (d[i] == INF) {
            cout << -1 << "\n";
        } else {
            cout << d[i] << "\n";
        }
    }

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
	ios::sync_with_stdio(0);
	ll a,b,x,y;
	cin>>a>>b>>x>>y;
	if(a<=b) cout<<min(x+(b-a)*y,(b-a)*2*x+x)<<endl;
	else cout<<min(x+(a-b-1)*y,(a-b-1)*2*x+x)<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
using namespace std;

#define N 2005
#define ll long long
const int mod=1e9+7;
int n,m;
string s;
ll sum1[N][N],sum2[N][N],sum3[N][N];
int dp[N][N],map[N][N];
int main()
{
	scanf("%d%d\n",&n,&m);
	for (int i=1 ; i<=n ; i++){
		cin>>s;
		for (int j=1 ; j<=m ; j++)
		    map[i][j]=s[j-1]=='.' ?1:0;
	}
	dp[1][1]=1;
	for (int i=1 ; i<=n ; i++){
	    for (int j=1 ; j<=m ; j++){
	    	if (i==1 && j==1) continue;
	    	if (map[i][j]==0) dp[i][j]=0;
	    	else{
	    	    sum1[i][j]=(sum1[i][j-1]+dp[i][j-1])%mod;
	    	    sum2[i][j]=(sum2[i-1][j]+dp[i-1][j])%mod;
	    	    sum3[i][j]=(sum3[i-1][j-1]+dp[i-1][j-1])%mod;
	    	    dp[i][j]=(sum1[i][j]+sum2[i][j]+sum3[i][j])%mod;
	        }
		}
	}
	printf("%d\n",dp[n][m]%mod);
	return 0;
}

#include <bits/stdc++.h>
#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 MP make_pair

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MOD = 1e9 + 7;

int N;
LL a[3005], dp[3005], dp2[3005];
LL sum[3005];

int main()
{
    scanf("%d", &N);
    rep1(i, N) scanf("%lld", &a[i]);
    rep1(i, N) a[i] += a[i - 1];

    dp2[0] = 1;
    LL ret = 0;
    rep1(k, N) {
        rep(i, k) sum[i] = 0;
        rep1(i, N) {
            sum[a[i - 1] % k] += dp2[i - 1];
            if (sum[a[i - 1] % k] >= MOD) sum[a[i - 1] % k] -= MOD;
            dp[i] = sum[a[i] % k];
        }
        ret += dp[N];
        if (ret >= MOD) ret -= MOD;
        rep(i, N + 1) dp2[i] = dp[i];
    }

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

    return 0;
}


#define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
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; }
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
int main(){
	int N,A,B;
	scan(N>>A>>B);
	prin(N-A+B);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
    for (in_vec_cout res : vec) {cout << res << " ";}
    cout << endl;
}

const ll inf = 1e18;
const ll mod = 1e9 + 7;

ll N, X, S, T;
vector<ll> a, b, A, B;

int main() {
    cin >> N >> X;
    rep(i,N) {
        ll x; cin >> x;
        if (i%2) a.push_back(x);
        else b.push_back(x);
    }
    S = a.size(), T = b.size();
    rep(bit,1<<S) {
        ll t = 0;
        rep(i,S) if ((bit>>i)&1) t += a[i];
        A.push_back(t);
    }
    rep(bit,1<<T) {
        ll t = 0;
        rep(i,T) if ((bit>>i)&1) t += b[i];
        B.push_back(t);
    }
    sort(all(A));
    sort(all(B));
    ll ans = 0;
    for (ll x : A) {
        if (x>X) break;
        ll l = -1, r = B.size();
        while (r-l>1) {
            ll m = (l+r)/2;
            if (x+B[m]<=X) l = m;
            else r = m;
        }
        chmax(ans,x+B[l]);
    }
    Cout(ans);
}


#include <bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);

using namespace std;

int main() {
    FastIO;

    int n , a , b;
    cin >> n >> a >> b;
    cout << n-a+b;

    return 0;
}

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

// 総数を1000000007（素数）で割った余り
const long long mod = 1e9 + 7;

using ll = long long;
using pii  = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>
#define Graph vector<vector<int>>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

void func(long long N, long long A, long long B){

}

int main(){

    long long N;
    scanf("%lld",&N);
    long long A;
    scanf("%lld",&A);
    long long B;
    scanf("%lld",&B);
    cout << N-A+B << endl;
    return 0;
}


#include <bits/stdc++.h>
#define pan(i,l,r) for(int i=l ; i < r; i++)
#define ll long long int
#define pb push_back
#define PI 3.141592653
#define pll pair<ll,ll>
#define V vector
#define F first
#define S second
#define INF INT_MAX

using namespace std;

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

void solve(){
    ll a, b, c;
    cin >> a >> b >> c;
    map<ll,ll> m;
    m[1] = 6;
    m[6] = 1;
    m[2] = 5;
    m[5] = 2;
    m[3] = 4;
    m[4] = 3;
    cout << m[a]+m[b]+m[c] << endl;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int tst = 1;
    //cin >> tst;
    while(tst--)
        solve();
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int a, b , c;
    cin >> a >> b >> c;
    cout << 21 - (a + b + c) << '\n';
}


#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define F first
#define S second
#define mod 1000000007
using namespace std;

ll add(ll a,ll b){ return ((a%mod)+(b%mod))%mod; }
ll mul(ll a,ll b){ return ((a%mod)*(b%mod))%mod; }
ll sub(ll a,ll b){ ll curr=((a%mod)-(b%mod))%mod; if(curr<0) curr+=mod; return curr; }

ll power(ll a,ll b){
	if(b==0) return 1;
	if(b==1) return a;
	ll res=power(a,b/2);
	res=mul(res,res);
	if(b%2) res=mul(res,a);
	return res;
}
void solve(){
	double a,b; cin>>a>>b;
	double ans=(100.0*a-100.0*b)/a;
	cout << ans <<endl;
}

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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
#define INF 1e9
#define rep(i, n) for(int i = 0; i < n; i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define all(x) (x).begin(),(x).end()

int main() {
    int A,B;
    cin >> A >> B;
    double ans;
    ans = 100.0 * (A - B) / A;
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ll n ;
    cin >> n;
    vector<ll> v(n);
    for(ll i=0; i<n; i++)
        cin >> v[i];
    sort(v.rbegin(),v.rend());
    ll suff[n+10];
    suff[n-1] = v[n-1];
    for(ll i=n-2; i>=0; i--)
    {
        suff[i] = suff[i+1] + v[i];
    }

    ll res = 0;
    for(ll i=0; i<n-1; i++)
    {
        res += v[i]*(n-i-1) - suff[i+1] ;
    }
    cout << res << endl;
}

#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector< pii >
#define vpll vector< pll >
#define mpii map<int,int>
#define mpll map<ll,ll>
#define MOD 1000000007
#define all(v) v.begin(),v.end()
#define s(v) v.size()
#define test ll t;cin>>t;while(t--)
#define vec vector<ll>
#define read0(v,n) for(int i=0;i<n;i++)cin>>v[i];
#define read1(v,n) for(int i=1;i<=n;i++)cin>>v[i];
#define trav(a,x) for (auto& a: x)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define cut(x) {cout<<x;return 0;}
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORB(i,a,b) for(int i=a;i>=b;i--)

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define nl '\n'
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
#define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
ll gcd(ll a, ll b)
{
	if (b==0)return a;
	return gcd(b, a % b);
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}
ll bpow(ll a, ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%MOD;
		b/=2;
		a=(a*a)%MOD;
	}
	return ans;
}
bool octal(ll n)
{
    while(n>0)
    {
        ll p=n%8;
        if(p==7)return 1;
        n/=8;
    }
    return 0;
}
bool decimal(ll n)
{
    while(n>0)
    {
        ll p=n%10;
        if(p==7)return 1;
        n/=10;
    }
    return 0;
}
ll dp[200006];
int main()
{

	fast
	int n;
	cin>>n;
	vec v(n);read0(v,n);
	sort(all(v));
	ll sum=0;
	vec temp;
	FOR(i,1,s(v)-1)
	{
	    temp.pb(v[i]-v[i-1]);
	}
	ll m=n-1;
	dp[0]=temp[0];
	sum=dp[0];
	FOR(i,1,m-1)
	{
	    dp[i]=dp[i-1]+temp[i];
	    sum+=dp[i];
	}
	ll prev=0;
	ll k=sum;
	FOR(i,1,m-1)
	{
	   // cout<<sum<<' '<<prev<<nl;
	    prev+=(m-i+1)*temp[i-1];
	    sum+=k-prev;
	}
	cut(sum)

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()

int did[202020];
vector<int> ret;

int main() {
    int n;
    cin>>n;
    vector<int> p(n),r(n);
    rep(i,n){
        cin>>p[i];
        p[i]--;
        r[p[i]]=i;
    }

    rep(i,n){
        int x=r[i];
        while(x>i){
            if(did[x-1]==1){
                cout<<-1<<endl;
                return 0;
            }
            did[x-1]=1;
            ret.push_back(x);
            swap(p[x-1],p[x]);
            r[p[x]]=x;
            r[p[x-1]]=x-1;
            x--;
        }
    }
    rep(i,n-1){
        if(did[i]==0){
            cout<<-1<<endl;
            return 0;
        }
    }

    for(auto e : ret) cout<<e<<endl;
}

#include<bits/stdc++.h>
#define lint long long
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define N 28
using namespace std;

lint log5(lint n){
    lint ans = 0;
    while(n > 0){
        ans++;
        n/=5;
    }
    return ans;
}

int bb(const vector<lint> &pot5, lint val){
    if(val > pot5.back()) return -1;
    int l=0, r = (int) pot5.size() -1;
    while(l <= r){
        int m = (l+r)/2;
        if(pot5[m] == val) return m;
        if(pot5[m] < val) l = m + 1;
        else r = m-1;
    }
    return -1;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    lint n;
    cin>>n;
    vector<lint> pot5(N);
    pot5[0] = 1;
    for(int i=1; i<N; i++){
        pot5[i] = pot5[i-1] * 5LL;
        assert(pot5[i] > pot5[i-1]);
    }
    assert(pot5.back() > (lint) 1e18);

    lint pot = 1;
    int id = 0;
    while(pot <= n){

        int id5 = bb(pot5, n - pot);
        if(id > 0 && id5 > 0){
            cout<<id<<" "<<id5<<"\n";
            return 0;
        }
        pot *= 3LL;
        id++;

    }
    cout<<"-1\n";
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long int_;
inline int readint(){
	int a = 0; char c = getchar(), f = 1;
	for(; c<'0'||c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c&&c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}

const int MaxN = 400005;
pair<int,int> a[MaxN];
pair<int,int> b[MaxN];
vector<int> v;

int main(){
	int n = readint();
	rep(i,1,n<<1){
		a[i].first = readint();
		a[i].second = i;
		b[i] = a[i]; // copy
	}
	sort(b+1,b+(n<<1)+1);
	rep(i,1,n<<1)
		if(v.empty())
			v.push_back(i), putchar('(');
		else{
			int t = v.back();
			if((a[t] > b[n]) == (a[i] > b[n]))
				v.push_back(i), putchar('(');
			else v.pop_back(), putchar(')');
		}
	putchar('\n');
	return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std; using ll = long long; using ld = long double;  using pll = pair<ll, ll>;
using vl = vector<ll>; using vll = vector<vl>; using vlll = vector<vll>; using vpll = vector<pll>;
using vs = vector<string>; using tll = tuple<ll, ll, ll>; using vtll = vector<tll>;
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; }
const ld PI = 3.1415926535897932;
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define co(i) cout << i << endl;
void in() {}
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
	cerr << " " << h;
	if (sizeof...(t) > 0) cerr << " :";
	debug_out(t...);
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> vec) {
	for (size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
	return os;
}
ll ctoi(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2;
	case '3': return 3; case '4': return 4; case '5': return 5;
	case '6': return 6; case '7': return 7; case '8': return 8;
	case '9': return 9;  default: return 0;
	}
}
bool pairCompare(const pll firstElof, pll secondElof)
{
	return firstElof.second < secondElof.second;
}
//AtCoder以外ではコメントアウトを忘れずに！
//#include <atcoder/all>
//using namespace atcoder;
//**Snippetリスト**//
//rep, vin, all, iteLoop, bitSearch, bitList, nod, LIS, digitDP, treeDP, Rerooting//
//gcdlcm, isPrime, eratos, primeFactorize, Npow, combination, divisor, modinv, doubling//
//dfs, bfs, dijkstra, WarshallFloyd, BellmanFord, UnionFind, Kruskal, RMQ, LCA, FordFulkerson//
//**AC Libraryリスト**//
//pow_mod, inv_mod, floor_sum, convolution, convolution_ll, modint//
//fenwicktree, segtree, lazysegtree, string//
ll i, j, k, l; ll N, M, K, H, W, X, Y, Z, R, Q;
ll MOD = 1000000007, INF = 1LL << 60, ans = 0, z = 0, o = 1;
vector<vtll> path;
//***********//
inline long long mod2(long long a, long long m) {
	return (a % m + m) % m;
}

// 拡張 Euclid の互除法
// ap + bq = gcd(a, b) となる (p, q) を求め、d = gcd(a, b) をリターンします
long long extGcd(long long a, long long b, long long& p, long long& q) {
	if (b == 0) { p = 1; q = 0; return a; }
	long long d = extGcd(b, a % b, q, p);
	q -= a / b * p;
	return d;
}

// 中国剰余定理
// リターン値を (r, m) とすると解は x ≡ r (mod. m)
// 解なしの場合は (0, -1) をリターン
pair<long long, long long> ChineseRem(long long b1, long long m1, long long b2, long long m2) {
	long long p, q;
	long long d = extGcd(m1, m2, p, q); // p is inv of m1/d (mod. m2/d)
	if ((b2 - b1) % d != 0) return make_pair(0, -1);
	long long m = m1 * (m2 / d); // lcm of (m1, m2)
	long long tmp = (b2 - b1) / d * p % (m2 / d);
	long long r = mod2(b1 + m1 * tmp, m);
	return make_pair(r, m);
}
int main() {
	ll T;
	cin >> T;
	for (l = 0; l < T; l++) {
		ll X, Y, P, Q;
		cin >> X >> Y >> P >> Q;
		ll ans = INF;
		for (i = 0; i < Y; i++) {
			for (j = 0; j < Q; j++) {
				ll m1 = X * 2 + Y * 2;
				ll b1 = X + i;
				ll m2 = P + Q;
				ll b2 = P + j;
				pll Ans = ChineseRem(b1, m1, b2, m2);
				//debug(b1,m1, b2,m2, Ans.first,Ans.second);
				if (Ans.second != -1) {
					chmin(ans, Ans.first);
				}
			}
		}
		//debug(1000000000999999999 - ans);
		if (ans == INF) cout << "infinity" << endl;
		else cout << ans << endl;
		//debug(ChineseRem(3, 2, 5, 3).first);
	}
}


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

//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,fma")

typedef long long ll;
typedef unsigned long long ull;

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

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

typedef set<int> si;
typedef set<ll> sll;

typedef map<int, int> mii;
typedef map<ll, ll> mll;

const double pi = acos(-1.0);
const long long INF = 2e18;

#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz size()
#define in insert
#define endl "\n"

#define sqr(a) ((a) * (a))
#define jor(a) !(a&1) //jor means EVEN number
#define bjor(a) (a&1) //bjor means ODD number

#define all(a)  a.begin(), a.end()
#define mem(arr, b) memset(arr, b, sizeof(arr)) //memset only for -1 and 0
//fill(a, a+n, 5) //fill for any value

#define for0(i,b) for(int i=0;i<(b);i++)
#define for1(i,b) for(int i=1;i<=(b);i++)
#define forab(i,a,b) for(int i=(a);i<=(b);i++)
#define forba(i,b,a) for(int i=(b);i>=(a);i--)
#define rep(i,a,b,c) for(int i=(a);i!=(b);i+=(c))

#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define fileRead freopen("input.txt", "r", stdin);
#define fileWrite freopen("output.txt", "w", stdout);

void print(int a[], int n){ // for testing or debugging
    for(int i=0;i<n;i++)cout<<a[i]<<" "; cout<<endl;
}

#define watch(x) cout << (#x) << " is " << (x) << endl;

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

int main()
{
    FAST
    int a,b; cin>>a>>b;
    cout<<a*2+100 - b;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

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

    ll n; cin >> n;
    string s, t; cin >> s >> t;

    ll ans = 0;
    queue<ll> p, q;
    zep(i, 0, n){
        if(t[i] == '1')q.push(i);
        if(s[i] == '1'){
            if(!p.empty()){
                ans += i - p.front();
                p.pop();
            }else if(!q.empty()){
                ans += i - q.front();
                q.pop();
            }else{
                p.push(i);
            }
        }
    }

    if(p.empty() && q.empty()){
        print(ans)
    }else{
        print(-1)
    }

    return 0;
}


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

const int N = 50 + 9;

pair<int, int> a[N];
int x[N];

int main(){
    int n, m, q;
    scanf("%d %d %d", &n, &m, &q);
    for (int i = 1; i <= n; ++i){
        scanf("%d %d", &a[i].first, &a[i].second);
    }
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= m; ++i){
        scanf("%d", x + i);
    }
    for (int i = 1; i <= q; ++i){
        int l, r;
        scanf("%d %d", &l, &r);
        vector<int> vc;
        for (int j = 1; j <= m; ++j){
            if (!(l <= j && j <= r)) vc.push_back(x[j]);
        }
        sort(vc.begin(), vc.end());
        priority_queue<int> pq;
        int ans = 0;
        for (int j = 0, sz = vc.size(), k = 1; j < sz; ++j){
            while (k <= n && a[k].first <= vc[j]){
                pq.push(a[k].second);
                ++k;
            }
            if (!pq.empty()){
                ans += pq.top();
                pq.pop();
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

#pragma GCC optimize("Ofast")
//#pragma GCC target ("sse4")

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 2;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

void solve() {
	int n, m, q; cin >> n >> m >> q;
	vector<int> w(n), v(n);
	rep(i, n)cin >> w[i] >> v[i];
	vector<int> x(m);
	rep(i, m)cin >> x[i];
	vector<P> vp;
	rep(i, n) {
		vp.push_back({ v[i],w[i] });
	}
	sort(all(vp), greater<P>());
	rep(i, q) {
		int l, r; cin >> l >> r; l--; r--;
		multiset<int> st;
		rep(j, m) {
			if (l <= j && j <= r)continue;
			st.insert(x[j]);
		}
		int ans = 0;
		for (P p : vp) {
			int val = p.first;
			int wei = p.second;
			if (st.find(wei) != st.end()) {
				ans += val; st.erase(st.find(wei));
			}
			else {
				st.insert(wei);
				auto itr = st.find(wei);
				itr++;
				if (itr != st.end()) {
					ans += val; st.erase(itr);
				}
				st.erase(st.find(wei));
			}
		}
		cout << ans << "\n";
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
	solve();
	return 0;
}


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

#include<cstdio>

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

ll dph[2000], dphw[4000], *naname = dphw + 2000;
const int mod = 1e9 + 7;

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

	int H = 0, W = 0;
	char c;
	while ((c = getchar_unlocked()) >= '0') H = H * 10 + c - '0';
	while ((c = getchar_unlocked()) >= '0') W = W * 10 + c - '0';

	getchar_unlocked();
	dph[0] = 1;
	naname[0] = 1;
	ll w = 1;
	ll tmp = 1;
	rep1(i, W - 1) {
		tmp = w;
		if (getchar_unlocked() == '.') {
			dph[i] = w;
			naname[i] = w;
			w = w * 2 % mod;
		}
		else {
			dph[i] = 0;
			naname[i] = 0;
			w = 0;
		}
	}
	getchar_unlocked();

	rep1(i, H - 1) {
		if (getchar_unlocked() == '.') {
			tmp = dph[0];
			w = tmp;
			dph[0] = (tmp + dph[0]) % mod;
			naname[-i] = (tmp + naname[-i]) % mod;
		}
		else {
			w = 0;
			dph[0] = 0;
			naname[-i] = 0;
		}
		if (!(i & 3)) {
			rep1(j, W - 1) {
				if (getchar_unlocked() == '.') {
					tmp = (w + dph[j] + naname[j - i]) % mod;
					w = (w + tmp) % mod;
					dph[j] = (tmp + dph[j]) % mod;
					naname[j - i] = (tmp + naname[j - i]) % mod;
				}
				else {
					w = 0;
					dph[j] = 0;
					naname[j - i] = 0;
				}
			}
		}
		else {
			rep1(j, W - 1) {
				if (!(j & 3)) {
					if (getchar_unlocked() == '.') {
						tmp = (w + dph[j] + naname[j - i]) % mod;
						w = (w + tmp) % mod;
						dph[j] = (tmp + dph[j]) % mod;
						naname[j - i] = (tmp + naname[j - i]) % mod;
					}
					else {
						w = 0;
						dph[j] = 0;
						naname[j - i] = 0;
					}
				}
				else {
					if (getchar_unlocked() == '.') {
						tmp = w + dph[j] + naname[j - i];
						w = w + tmp;
						dph[j] = tmp + dph[j];
						naname[j - i] = tmp + naname[j - i];
					}
					else {
						w = 0;
						dph[j] = 0;
						naname[j - i] = 0;
					}
				}
			}
		}
		getchar_unlocked();
	}

	printf("%lld\n", tmp % mod);

	Would you please return 0;
}


//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FIO ios_base::sync_with_stdio(false); cin.tie(0);
#define trav(x,a) for (auto& x: a)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define mem(a,v) memset((a), (v), sizeof (a))
#define endl "\n"
#define case(t) cout << "Case #" << (t) << ": "
#define reada(a, n) for (int _i = 0; _i < (n); _i++) read(a[_i])
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;

template<class T> void read(T& x) { cin >> x; }
template<class X, class Y> void read(pair<X,Y>& a) { read(a.first), read(a.second); }
template<class T, size_t U> void read(array<T,U>& x) { for (int i = 0; i < U; i++) read(x[i]); }
template<class T> void read(vector<T>& x) { trav(y, x) read(y); }
template<class T, class... O> void read(T& x, O&... y) { read(x), read(y...); }

string to_string(const char& x) { return string(1,x); }
string to_string(const char*& x) { return (string)x; }
string to_string(const string& x) { return x; }
template<class T, class U> string to_string(const pair<T,U>& x) { return to_string(x.first) + " " + to_string(x.second); }
template<class T, size_t U> string to_string(const array<T,U>& x) { string ret = ""; for (int i = 0; i < U; i++) ret += (!i ? "" : " ") + to_string(x[i]); return ret; }
template<class T> string to_string(const vector<T>& x) { string ret = ""; bool f = 0; trav(y, x) ret += (!f ? "" : " ") + to_string(y), f = 1; return ret; }
template<class T> string to_string(const set<T>& x) { string ret = ""; bool f = 0; trav(y, x) ret += (!f ? "" : " ") + to_string(y), f = 1; return ret; }
void print() { cout << endl; }
template<class T> void pr(const T& x) { cout << to_string(x); }
template<class T, class... O> void print(const T& x, const O&... y) { pr(x); if (sizeof...(y)) pr(" "); print(y...); }

int a[N], b[N];

ll solvePre(int l, int r) {
  deque<int> q;
  ll ret = 0;
  for (int i = r; i >= l-1; i--) {
    if (sz(q) && a[i] + sz(q) == b[q.back()])
      ret += sz(q);
    while (sz(q) && a[i] + sz(q) == b[q.back()])
      q.pop_back();
    q.push_front(i);
  }
  if (sz(q) > 1) {
    print(-1);
    exit(0);
  }
  return ret;
}

void flip(int l, int r) {
  reverse(a + l, a + r + 1);
  reverse(b + l, b + r + 1);
  for (int i = l; i <= r; i++)
    a[i] *= -1, b[i] *= -1;
}

ll solveSuff(int l, int r) {
  flip(l-1, r+1);
  ll ret = solvePre(l, r);
  flip(l-1, r+1);
  return ret;
}

ll solve(int l, int r) {
  for (int i = l; i <= r+1; i++)
    if (a[i] <= b[i])
      return solvePre(l, i-1) + solveSuff(i, r);
  return 0;
}

int main() {
  FIO
  int n, l; read(n, l);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = 1; i <= n; i++)
    read(b[i]);
  a[0] = 0, a[n+1] = l+1;
  b[0] = 0, b[n+1] = l+1;
  ll ans = 0;
  for (int i = 1, j = 1; i <= n+1; i++)
    if (a[i] == b[i])
      ans += solve(j, i-1), j = i+1;
  print(ans);
  return 0;
}

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

int main(void)
{
    ll N, M;
    cin >> N >> M;
    vector<string> S(N);
    for (ll i = 0; i < N; i++)
    {
        cin >> S[i];
    }

    ll a = 1;
    ll b = 0;
    for (ll i = 1; i < N; i++)
    {
        ll count = 0;
        for (ll j = 0; j < M; j++)
        {
            if (S[0][j] != S[i][j])
            {
                count ++;
            }
        }
        if (count % 2 == 0)
        {
            a++;
        }
        else
        {
            b++;
        }
    }
    cout << a * b << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//

template<typename T>
struct BinaryIndexedTree {
	using value_type = T;
	using const_reference = const value_type &;
	using F = std::function<value_type(const_reference, const_reference)>;
	using size_type = std::size_t;

	BinaryIndexedTree(size_type n, const F & f, const_reference id_elem) : n(n), f(f), id_elem(id_elem) {
		node.resize(n + 1, id_elem);
	}

	size_type size() const noexcept {
		return n;
	}

	void add(size_type i, const_reference x) {
		assert(i < size());
		++i;
		for (; i <= size(); i += i & -i) node[i] = f(node[i], x);
	}

	// [0, i)
	value_type sum(size_type i) const {
		assert(i <= size());
		value_type res = id_elem;
		for (; i > 0; i -= i & -i) res = f(res, node[i]);
		return res;
	}

	// sum[0, r] <= x を満たす最小の r を返す (存在しなければ size())
	size_type lower_bound(const_reference x) const {
		size_type res = 0;
		size_type s = id_elem, w = 1;
		while (w < size()) w <<= 1;
		for (; w > 0; w >>= 1) {
			if (res + w <= size()) {
				value_type cur = f(s, node[res + w]);
				if (cur < x) {
					res += w;
					s = cur;
				}
			}
		}
		return res;
	}

private:
	size_type n;
	F f;
	value_type id_elem;
	std::vector<value_type> node;
};

int main() {
	int N;
	cin >> N;
	vector<int> A(N);
	REP(i, N) scanf("%d", &A[i]);

	BinaryIndexedTree<int> bit(N, [](int a, int b) { return a + b; }, 0);
	ll ans = 0;
	REP(i, N) {
		ans += bit.sum(N) - bit.sum(A[i]);
		bit.add(A[i], 1);
	}

	REP(i, N) {
		printf("%lld\n", ans);
		if (i + 1 == N) break;
		ans -= bit.sum(A[i]);
		ans += bit.sum(N) - bit.sum(A[i] + 1);
	}
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int, int> >
#define vpll vector <pair <long long, long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define vb vector <bool>
#define vvb vector <vb>
#define mp make_pair
#define vvll vector <vll>
#define vsi vector <si>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MANX MAXN
#define itn int
#define dbg(x); {cout << #x << "=" << x << ", ";}
#define in(x); { for (auto &to : x) cin >> to;}
#define out(x); { for (auto &to : x) cout << to << " "; cout << '\n'; }

const ll INFLL = 1e18;
const int MAXN = 3e5+100;
const ll INF = 1e9;
const int mod1 = 1e9+7;
const int mod2 = 1e9+21;
int f[MAXN];
void add(int pos,int x)
{
    for (;pos<MAXN;pos|=pos+1)
        f[pos]+=x;
}

int res(int l)
{
    int sum=0;
    for (;l>=0;l=((l+1)&l)-1)
        sum+=f[l];
    return sum;
}

int res(int l,int r)
{
    if (l>r)
        return 0;
    return res(r)-res(l-1);
}
void solve()
{
    int n;
    cin>>n;
    vector <int> p(n);
    in(p);
    ll ans=0;
    for (int i=0;i<n;i++)
    {
        ans+=res(p[i]+1,n-1);
        add(p[i],1);
    }
    cout<<ans<<'\n';
    for (int i=0;i<n-1;i++)
    {
        ans-=p[i];
        ans+=n-1-p[i];
        cout<<ans<<'\n';
    }
}

int main() {
    #ifdef Mip182
    freopen("a.in", "r", stdin);
    #else
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    #endif
    int _t;
    _t=1;
//    cin>>_t;
    while (_t--)
        solve();
    #ifdef Mip182
    cout<<'\n'<<"Time : "<<(double)(clock())/CLOCKS_PER_SEC<<'\n';
    #endif
}


#include <bits/stdc++.h>
#define mod 1000000007
#define fast_io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define vi vector<int>
#define vl vector<ll>
#define REP(s, e) for (auto i = s; i <= e; i++)
#define show(s) cout<<s<<" "
#define MAXI 2147483647
#define MAXL 9223372036854775807
#define FOR(a,b) for (ll i = a; i < b; i++)
using namespace std;
typedef signed long long int ll;

int main() {
    fast_io;
    int n; cin >> n;
    vector<string> opers = {"dummy"};
    for (int i = 0; i < n; i++)
    {
        string inp; cin >> inp;
        opers.push_back(inp);
    }
    vector<pair<ll,ll>> dp;
    dp.push_back({1,0});
    for (int i = 1; i <= n; i++)
    {
        if (opers[i] == "OR")
        {
            dp.push_back({dp[i-1].first * 2 + dp[i-1].second, dp[i-1].second});
        } else
        {
            dp.push_back({dp[i-1].first, dp[i-1].second * 2 + dp[i-1].first});
        }
    }
    ll ans1 = dp[n].first;
    dp.clear();
    dp.push_back({0,1});
    for (int i = 1; i <= n; i++)
    {
        if (opers[i] == "OR")
        {
            dp.push_back({dp[i-1].first * 2 + dp[i-1].second, dp[i-1].second});
        } else
        {
            dp.push_back({dp[i-1].first, dp[i-1].second * 2 + dp[i-1].first});
        }
    }
    ll ans2 = dp[n].first;
    cout << ans1 + ans2;
}

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

int main(){
  int N;cin>>N;
  vector<long> A(N);
  for(int i=0;i<N;i++) cin>>A.at(i);
  map<long,vector<int>> x;
  long cnt=1,m=0;
  x[0].push_back(-1);
  for(int i=0;i<N;i++){
    m+=cnt*A[i];
    x[m].push_back(i);
    cnt*=-1;
  }
  long ans=0;
  for(auto [c,v]:x){
    m=v.size();
    ans+=m*(m-1)/2;
  }
  cout<<ans<<endl;
}


#include <bits/stdc++.h>

#define ll long long
#define map unordered_map
#define set unordered_set
#define l_l pair<ll, ll>
#define P pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>
#define mp make_pair
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rev(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define revs(i, n) for (int i = ((int)(n)); i > 0; --i)
// 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; }

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);
ll H, W;

// template <class T> void plus_mod(T &a, const T &b) {a = (a + b) % MOD;}

ll grid[2005][2005];

ll dp[2005][2005];

ll TE;

ll calc(ll y, ll x) {
  if (dp[y][x] != INF) return dp[y][x];

  ll te = y + x;
  bool is_taka = (te % 2 == 0);

  ll plus = is_taka ? grid[y][x] : -grid[y][x];

  if (y == H - 1 && x == W - 1) {
    dp[y][x] = -plus;
    return -plus;
  }

  ll v;
  if (x < W - 1 && y < H - 1) {
    //
    ll v1 = calc(y, x + 1);
    ll v2 = calc(y + 1, x);

    if (is_taka) {
      v = max(v1, v2);
    } else {
      v = min(v1, v2);
    }

  } else if (x < W - 1) {
    v = calc(y, x + 1);
  } else if (y < H - 1) {
    v = calc(y + 1, x);
  }
  v -= plus;

  dp[y][x] = v;

  return v;
}

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

void show_table(ll I, ll J) {
  rep(i, I) {
    rep(j, J) { cout << dp[i][j] << " "; }
    cout << "" << endl;
  }
  cout << "" << endl;
}
int main() {
  // std::cout << std::fixed << std::setprecision(10);

  Fill(dp, INF);

  // + 青
  // - 赤
  scanf("%lld %lld", &H, &W);

  rep(y, H) {
    string line;
    cin >> line;

    rep(x, W) {
      char ch = line[x];
      if (ch == '-') grid[y][x] = -1;
      if (ch == '+') grid[y][x] = 1;
    }
  }

  // cout << (calc(1, 0)) << endl;

  if (H == 1 && W == 1) {
    cout << ("Draw") << endl;
    return 0;
  }

  ll TE = H + W - 2;

  ll c = calc(0, 0);

  ll v;

  if (W >= 2 && H >= 2) {
    ll v1 = dp[0][1];
    ll v2 = dp[1][0];
    v = max(v1, v2);
  } else if (W >= 2) {
    v = dp[0][1];
  } else {
    v = dp[1][0];
  }

  // cout << "v:" << (v) << endl;

  if (v == 0) {
    cout << ("Draw") << endl;
  } else if (v >= 1) {
    cout << ("Takahashi") << endl;
  } else {
    cout << ("Aoki") << endl;
  }

  // show_table(H, W);
}

// https://atcoder.jp/contests/abc207/editorial/2153
#include<bits/stdc++.h>
using namespace std;

int main(){
    int N; cin >> N;
    vector<double> a(N),b(N),c(N),d(N);
    for(int _=0; _<2; _++){
        for(int i=0; i<N; i++) cin >> a[i] >> b[i];
        int x = 0, y = 0;
        for(int i=0; i<N; i++){
            x += a[i];
            y += b[i];
            a[i] *= N;
            b[i] *= N;
        }
        for(int i=0; i<N; i++){
            a[i] -= x;
            b[i] -= y;
        }
        swap(a,c);
        swap(b,d);
    }
    for(int i=0; i<N; i++){
        if(a[i]!=0 || b[i]!=0){
            swap(a[i],a[0]);
            swap(b[i],b[0]);
        }
    }
    string ans = "No";
    const double eps = 1e-6;
    for(int i=0; i<N; i++){
        double angle = atan2(d[i],c[i])-atan2(b[0],a[0]);
        bool flag = true;
        for(int j=0; j<N; j++){
            double A = a[j]*cos(angle)-b[j]*sin(angle);
            double B = a[j]*sin(angle)+b[j]*cos(angle);
            bool flag2 = false;
            for(int k=0; k<N; k++){
                if(std::abs(A-c[k])<=eps && std::abs(B-d[k])<=eps) flag2 = true;
            }
            flag &= flag2;
        }
        if(flag) ans = "Yes";
    }
    cout << ans << endl;
}


/* OVERKILLED */

// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2")

#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include<climits>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>
#include <chrono>
using namespace std;
#define ld long double
#define int long long
#define pii pair<int,int>
#define pb push_back
#define endl '\n'
const int inf = LLONG_MAX;

#define pi acos(-1)

#define init(x,a) memset(x,a,sizeof(x))
#define all(c) c.begin(),c.end()
//int const mod = 998244353;
int const mod = 1e9 + 7;
int const hashmod = 1610612741;
#define bitcount(x)       __builtin_popcountll(x)
#define l first
#define r second

int toint(const string &s) {stringstream ss; ss << s; int x; ss >> x; return x;}
string tostring ( int number ) {stringstream ss; ss << number; return ss.str();}
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define haha ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// * Using PBDS *
/*typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
               os; */

struct cmp
{
	bool operator() (pair<int, int> a, pair<int, int> b) {

		return a.r > b.r;

	}
};

int modexp(int x, int n) {
	if (n == 0) {
		return 1;
	}
	else if (n & 1) {
		return (modexp((x * x) % mod, n / 2) % mod * x % mod) % mod;
	}
	else {
		return (modexp((x * x) % mod, n / 2) % mod);
	}
}

int modinv(int n)
{
	return modexp(n, mod - 2) % mod;
}

int fact[400005];

void fact_pre() {
	fact[0] = 1;
	for (int i = 1; i <= 400000; i++) {
		fact[i] = (fact[i - 1] * i) % mod;
	}
}

int ncr(int n, int r) {
	if (r > n) return 0;
	int a = fact[n];
	a *= modinv((fact[n - r] * fact[r]) % mod);
	return a % mod;
}

int gcd(int a, int b) {
	if (b == 0) {
		return a;
	}
	return gcd(b, a % b);
}
bool comp1(pii a, pii b) {
	return a.l > b.l;
}

bool comp2(pair<pii, int> a, pair<pii, int> b) {

	if (a.l.l == b.l.l)
		return a.r < b.r;

	return (a.l.l < b.l.l );
}
bool comp(string a, string b) {

	return a[0] > b[0];
}
#define nax   10000005

int t1 = 0;

void go() {
	int n;
	cin >> n;
	int arr[n];
	int ans = inf;
	for (int i = 0; i < n; i++)
		cin >> arr[i];
	int pre[n];
	pre[0] = arr[0];
	for (int i = 1; i < n; i++) {
		pre[i] = pre[i - 1] | arr[i];
	}
	for (int i = 0; i < (1 << n); i++) {
		int cn = i;
		vector<int> v(n, 0);
		int cx = 0;
		int cn1 = 0;
		for (int j = 0; j < n; j++) {
			if (((i >> j) & 1) == 1)
			{
				v[j] = 1;
			}
		}
		for (int j = 0; j < n; j++) {
			if (v[j] == 1) {
				cn1 |= arr[j];
				cx ^= cn1;
				cn1 = 0;
			}
			else {
				cn1 |= arr[j];
			}
		}
		cx ^= cn1;
		ans = min(ans, cx);

	}
	cout << ans;

}

signed main() {

	haha
	int t;
	t = 1;

	while (t--) {

		go();
	}

}

#include <bits/stdc++.h>

#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define log_2(a) (log((a)) / log(2))
#define ln '\n'

using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;

static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;

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

template<typename T>
void printArray(vector<T> &printVec) {
    if(printVec.empty()) return;
    cout << printVec[0];
    for(int i = 1; i < printVec.size(); ++i) cout << " " << printVec[i];
    cout << endl;
}

vector<LL> Div(LL n) {
    vector<LL> ret;
    for(LL i = 1; i * i <= n; ++i) {
        if(n % i == 0) {
            ret.pb(i);
            if(i * i != n) ret.pb(n / i);
        }
    }
    sort(all(ret));
    return ret;
}

LL res = LINF;
void dfs(vector<LL> &v, int pos, LL val, LL xo) {
    if(pos == v.size()) {
        res = min(res, xo ^ val);
        return;
    }

    dfs(v, pos + 1, val | v[pos], xo);
    xo ^= val;
    dfs(v, pos + 1, v[pos], xo);

    return;
}

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

    int N;
    cin >> N;
    vector<LL> v(N);
    for(int i = 0; i < N; ++i) {
        cin >> v[i];
    }

    dfs(v, 0, 0, 0);
    cout << res << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef long double ld;
#define REP(i, n) for(ll i = 0LL; i < (ll)(n); i++)
#define REPR(i, n) for (ll i = (ll)(n) - 1; i >= 0; i--)
#define FOR(i, n, m) for(ll i = (ll)n; i < (ll)(m); i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007
#define INF (1LL << 62)

#define PI (acos(-1))
#define PRINT(x) std::cout << x << endl

ll gcd(ll a, ll b) { return b ? gcd(b,a%b) : a;}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll countDigit(ll n) { return floor(log10(n) + 1); }
typedef pair <ll,ll> P;
static const ll dx[8] = {0,1,0,-1, 1, -1, 1, -1}, dy[8] = {1,0,-1,0,1,1,-1,-1};

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

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

int main()
{
  char S, T;
  cin >> S >> T;
  if (S == 'Y') {
    char t = T - 'a' + 'A';
    cout << t << endl;
  } else cout << T << endl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void Yes() {cout << "Yes\n";}
void No() {cout << "No\n";}
void YES() {cout << "YES\n";}
void NO() {cout << "NO\n";}

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

	string s, t;
	cin >> s >> t;
	if (s == "Y") t[0] += 'A' - 'a';
	cout << t << '\n';
}


#include <bits/stdc++.h>

using namespace std;
using namespace std::chrono;
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0)

#define ceil(x) static_cast<ll>(ceil(x))
#define floor(x) static_cast<ll>(floor(x))
#define pow(x, y) static_cast<ll>(pow(x, y))
#define gcd(x, y) static_cast<ll>(__gcd(x, y))
#define lcm(x, y) static_cast<ll>(a * (b / gcd(x, y)))

#define F first
#define S second
#define eb emplace_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define FI(i, a, b) for (ll i{a}; i <= b; i++)
#define FD(i, a, b) for (ll i{a}; i >= b; i--)

typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vpii;
typedef pair<ll, ll> pi;

const ll mod{static_cast<ll>(1e9) + 7};
const ld Pi{3.14159265358979323846};

auto int_from(istream &in) -> int
{
    int x;
    in >> x;
    return x;
}

auto lng_from(istream &in) -> ll
{
    ll x;
    in >> x;
    return x;
}

auto str_from(istream &in) -> string
{
    string x;
    in >> x;
    return x;
}

auto chr_from(istream &in) -> char
{
    char x;
    in >> x;
    return x;
}

int main()
{
    IOS;
    int n{int_from(cin)};
    vi v;
    bool ok{1};
    FI(i, 1, n)
    {
        v.eb(int_from(cin));
    }
    sort(all(v));
    FI(i, 0, n - 1)
    {
        if (v[i] != i + 1)
        {
            ok = 0;
            break;
        }
    }
    if (ok)
    {
        cout << "Yes\n";
    }
    else
    {
        cout << "No\n";
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int n,x,v[1020],z;
int main() {
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x;
		z+=v[x]++;
	}
	if(z!=0){
		cout<<"No";
	}else{
		cout<<"Yes";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = (1LL << 60);
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
int main()
{
    ll n;
    cin >> n;
    vector<pair<ll, ll>> x(n), y(n);
    rep(i, n) cin >> x[i].first >> y[i].first;
    rep(i, n)
    {
        x[i].second = i;
        y[i].second = i;
    }
    sort(x.begin(), x.end());
    sort(y.begin(), y.end());
    vector<ll> ans;
    if (x[n - 1].second == y[n - 1].second && x[0].second == y[0].second)
        ans.push_back(max(abs(x[n - 1].first - x[0].first), abs(y[n - 1].first - y[0].first)));
    else{
        ans.push_back(abs(x[n - 1].first - x[0].first));
        ans.push_back(abs(y[n - 1].first - y[0].first));
    }
    if (x[n - 1].second==y[n-1].second&&x[1].second==y[1].second)
        ans.push_back(max(abs(x[n - 1].first - x[1].first), abs(y[n - 1].first - y[1].first)));
    else{
        ans.push_back(abs(x[n - 1].first - x[1].first));
        ans.push_back(abs(y[n - 1].first - y[1].first));
    }
    if(x[0].second==y[0].second&&x[n-2].second==y[n-2].second)
        ans.push_back(max(abs(x[n - 2].first - x[0].first), abs(y[n - 2].first - y[0].first)));
    else{
        ans.push_back(abs(x[n - 2].first - x[0].first));
        ans.push_back(abs(y[n - 2].first - y[0].first));
    }

    sort(ans.begin(), ans.end());
    cout << ans[(int)ans.size()-2] << endl;
}

#include <bits/stdc++.h>
#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define per(i,n) for(long long i = (long long)(n-1);i >= 0;--i)
#define REP(i,s,t) for(long long i=s;i<t;i++)
#define PER(i,s,t) for(long long i=s;i>t;--i)
#define all(x) x.begin(),x.end()
#define sp(n) setprecision(n)
typedef long long ll;
using namespace std;
int main(){
  int n,k,m;
  cin>>n>>k>>m;
  int l=0;
  rep(i,n-1){int j;
             cin>>j;
             l+=j;}
  if(l+k<n*m){cout<<-1<<endl;}
  else cout<<max((n*m-l),0)<<endl;
}


#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define overload4(_1, _2, _3, _4, name, ...) name
#define overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = n; i--;)
#define rrep2(i, n) for (ll i = n; i--;)
#define rrep3(i, a, b) for (ll i = b; i-- > (a);)
#define rrep4(i, a, b, c) for (ll i = (a) + ((b) - (a)-1) / (c) * (c); i >= (a); i -= c)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define elif else if
ll beki(ll i)
{
    return 1ll < i;
}
#define skip(M) \
    if (M)      \
    continue
#define all(v) v.begin(), v.end()
#define allm(x, y, M) for (auto [(x), (y)] : (M))
#define alls(i, S) for (auto(i) : (S))
#define pb push_back
#define fi first
#define se second
#pragma warning(disable : 4312)
template <class X>
void print(X x)
{
    cout << x << endl;
}
template <class... T>
void print(T &...t)
{
    (..., (cout << t << " "));
}
void print(vl x)
{
    for (ll i : x)
    {
        cout << i << " ";
    }
    cout << endl;
}
void print(vector<PP> x)
{
    for (PP i : x)
    {
        cout << i.first << " " << i.second << endl;
    }
    cout << endl;
}
template <class X>
int lbound(X a, vector<X> &Y)
{
    return lower_bound(all(Y), a) - Y.begin();
}
template <class X>
int ubound(X a, vector<X> &Y)
{
    return upper_bound(all(Y), a) - Y.begin();
}
template <class... T>
void zero(T &...t)
{
    (..., (t = 0));
}
template <class... T>
void cl(T &...t)
{
    (..., (t.clear()));
}
template <class... T>
void in(T &...t)
{
    (..., (cin >> t));
}
template <class... T>
void put(vl &V, int n)
{
    ll k;
    rep(i, n)
    {
        cin >> k;
        V.pb(k);
    }
}
int max_index(vl &V)
{
    return max_element(all(V)) - V.begin();
}
int min_index(vl &V)
{
    return min_element(all(V)) - V.begin();
}
ll sum(vl &V)
{
    return accumulate(all(V), 0);
}
void Uni(vl &V)
{
    V.erase(unique(all(V)));
}
const ll INF = (1LL << 61) - 1;
const ll MOD = 1000000007;
const ll MOD2 = 998244355;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, u, v, m;
ll codeforces = 1;
string S, T;
vl A, B, C, D, ANS;
vector<PP> VP, VP2;
set<ll> s;
vector<vl> g(MAX_N);
void solve()
{
    in(n);
    rep(i, n)
    {
        cin >> a >> b >> c;
        A.pb(a);
        B.pb(b);
        C.pb(c);
    }

    ans = INF;
    rep(i, n)
    {
        if (A[i] < C[i])
            ans = min(ans, B[i]);
    }

    if (ans == INF)
        print(-1);
    else
        print(ans);
}
int main()
{
    // cout<<fixed<<setprecision(15);
    cin.tie(0);
    ios::sync_with_stdio(false);
    //cin >> codeforces;
    while (codeforces--)
    {
        solve();
    }
}

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

int main(){
    ll k, n, m;    cin  >> k >> n >> m;
    vector<int> a(k), b(k);
    vector<pair<double, ll>> v(k);
    rep(i, k)   cin >> a[i];
    ll sum = 0;
    rep(i, k){
        b[i] = (m*a[i])/n;
        sum += b[i];
        v[i].first = ((double)(m*a[i]))/(double)n - b[i];
        v[i].second = i;
    }
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    rep(i, m-sum){
        b[v[i].second]++;
    }
    for(auto x : b)
        cout << x << ' ';
    cout << endl;
    return 0;
}


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

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

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

    vec_int a(N), b(N);
    rep(i,N)cin>>a.at(i);
    rep(i,N)cin>>b.at(i);

    vec_int max_a(N);
    max_a.at(0) = a.at(0);
    for(int i=1;i<N;i++){
        max_a.at(i) = max(max_a.at(i-1), a.at(i));
    }

    vec_int max_mult(N);
    for(int i=0;i<N;i++){
        max_mult.at(i) = b.at(i) * max_a.at(i);
    }

    int max_val = 0;
    for(int i=0;i<N;i++){
        max_val = max(max_mult.at(i), max_val);
        cout<<max_val<<endl;
    }

    return 0;
}

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

void solve(long long N, std::vector<long long> a, std::vector<long long> b) {
    ll maxA = 0;
    ll maxC = 0;
    rep(i, N) {
        maxC = max(maxC, a[i] * b[i]);
        maxC = max(maxC, maxA * b[i]);
        maxA = max(maxA, a[i]);
        cout << maxC << "\n";
    }
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main() {
    long long N;
    scanf("%lld", &N);
    std::vector<long long> a(N);
    for (int i = 0; i < N; i++) {
        scanf("%lld", &a[i]);
    }
    std::vector<long long> b(N);
    for (int i = 0; i < N; i++) {
        scanf("%lld", &b[i]);
    }
    solve(N, std::move(a), std::move(b));
    return 0;
}


#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
vector<p> v;
const int maxn=1e6+10;
ll a[maxn], b[maxn];
ll k, n, m;

int main()
{
	ll sum=0;
	cin>>k>>n>>m;
	for(int i=0; i<k; i++){
		cin>>a[i];
		b[i]=a[i]*m/n;
		sum+=b[i];
		p temp;
		temp.first=(double)(a[i]*m-b[i]*n);
		temp.second=i;
		v.push_back(temp);
	}

	sort(v.begin(), v.end(), greater<p>());

    for(int i=0; i<m-sum; i++){
    	b[v[i].second]+=1;
    }

    for(int i=0; i<k; i++)
    	cout<<b[i]<<" ";
    cout<<endl;
	return 0;
}

//vec[i]の範囲外エラー表示
#define _GLIBCXX_DEBUG
//includeとusing
#include <bits/stdc++.h>
using namespace std;
//型名省略
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using P = pair<T,T>;
template <class T> using M = map<T,T>;
template <class T> using S = set<T>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T,V<T>,greater<T>>;
using ll = long long;
using st = string;
using vl = V<ll>;
using vvl = V<V<ll>>;
using vc = V<char>;
using vvc = V<V<char>>;
using vs = V<st>;
using vvs = V<V<st>>;
using vb = V<bool>;
using vvb = V<V<bool>>;
//定数
const ll INF = 1e9;
const ll MOD = 1e9+7;
const vl dx = {1,0,-1,0,1,0,-1,0};
const vl dy = {0,1,0,-1,0,1,0,-1};
//マクロ
#define re(n) for(ll _ = 0; _ < (ll) n; _++)
#define rep(i,n) for(ll i = 0; i < (ll) n; i++)
#define rrep(i,n) for(ll i = (ll) n - 1; i >= 0;i--)
#define repp(i,x,n) for(ll i = (ll) x; i < (ll) n; i++)
#define rrepp(i,x,n) for(ll i = (ll) n - 1; i >= x; i--)
#define bitrep(i,n) for(ll i = 0; i < (1<<(ll)n); i++)
#define each(x,A) for(auto &(x): (A))
#define ALL(A) A.begin(), A.end()
#define SIZE(A) ll(A.size())
#define pb(a) push_back(a)
#define mp make_pair
#define PRECISE_COUT cout << setprecision(15) << fixed
//入力
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){ cin>>head; CINT(move(tail)...);}
#define CIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define DCIN(...) double __VA_ARGS__;CINT(__VA_ARGS__)
#define VCIN(A) for(auto &a : (A)) cin >> a
//出力
#define COUT(a) cout << a << endl
#define TEST(i,a) cout << (ll) i << " : " << (ll) a << endl
#define vout(A) for(auto &a : (A)) cout << a << " ";cout << endl
//正誤判定
void Yes(bool ans){cout << (ans? "Yes" : "No") << endl;}
void YES(bool ans){cout << (ans? "YES" : "NO") << endl;}
//余り切り上げ
ll ceil(ll a,ll b) {return (a + b - 1) / b;}
//最大値・最小値
template <typename T> bool chmax(T &a, const T& b) {if(a < b) {a = b; return true;} return false;}
template <typename T> bool chmin(T &a, const T& b) {if(a > b) {a = b; return true;} return false;}
//最大公約数
ll gcd(ll a,ll b){if(a<b)swap(a,b); if(a%b==0)return b; return gcd(b,a%b);}
//最小公倍数
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
//n乗の余り
ll modpow(ll x,ll n,ll mod){ll res=1;for(ll i=0;i<n;i++){res=res*x%mod;}return res;}
ll MODpow(ll x,ll n){ll res=1;for(ll i=0;i<n;i++){res=res*x%MOD;}return res;}
//グリッドグラフの範囲外エラー
bool range(ll ny, ll nx, ll H, ll W){if(ny < 0 || ny >= H || nx < 0 || nx >= W) return false; return true;}

/*----------------------------------------------------------------------*/

//main関数
int main() {
    CIN(N);
    ll sum = 0;
    re(N){
        CIN(a,b);
        sum += (b - a + 1) * (a + b) / 2;
    }
    COUT(sum);
}


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <array>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <random>
#include <chrono>
#include <utility>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <sstream>
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(bool x) {cerr << (x ? "true" : "false");}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}

template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";}
template <typename T, typename... V>void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef HOME
#warning CHECK int:ll::INT_MAX:LLONG_MAX
#define maxn 20
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define maxn 2000006
#define gcd __gcd
#define debug(x...)
#endif

#define ff first
#define endl '\n'
#define ss second
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define PI 3.14159265358979323846264338327950L
#define f(i,x,n) for(int i=x;i<=n;i++)
#define fr(i,x,n) for(int i=x;i>=n;i--)
struct _ { ios_base::Init i; _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } } _;

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

int main() {

	long long sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy;
	cout << fixed << setprecision(15);

	cout << (long double)(gx * sy + sx * gy) / (sy + gy);

	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<int>>;
const int INT_INF = 2147483647;
//stoi(s) : string→int stoll(s) :string→longlong  int→string to_string(i)
const double PI = acos(-1.0);
//小数点の表し方 cout << fixed << setprecision(5);
const ll LLMAX = 9223372036854775805;

int main(){
    double sx,sy,gx,gy;
    cin >> sx >> sy >> gx >> gy;

    double ans = sx - (sx - gx)/(sy + gy) * sy;
    cout << fixed << setprecision(10) << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define pow(n,m) powl(n,m)
#define sqrt(n) sqrtl(n)
const ll MAX = 5000000000000000000;
const ll MOD = 0;
random_device rd;
mt19937 mt(rd());
int main(){
    ll N,M;
    cin >> N >> M;
    vector<string> S(M),ans(N),T;
    map<string,ll> ss;
    ll m = MAX;
    for(ll i = 0;i < M;i++) cin >> S[i];
    for(ll i = 0;i < M;i++) ss[S[i]] = i + 1;
    for(ll i = 0;i < M;i++) m = min(m,(ll)S[i].size());
    for(ll t = 0;t < 4000;t++){
        string a = S[mt() % M];
        for(ll i = 0;i < 10;i++){
            string b = S[mt() % M];
            for(ll j = min(a.size(),b.size());j >= 0;j--){
                ll ok = 1;
                for(ll k = 0;k < j;k++){
                    if(a[a.size() - j + k] != b[j]) ok = 0;
                }
                if(ok){
                    ll k = j;
                    while(a.size() < N && k < b.size()){
                        a += b[k];
                        k++;
                    }
                    break;
                }
            }
            if(a.size() + b.size() <= N){
                a += b;
            }
        }
        while(a.size() < N) a += (char)('A' + (mt() % 8));
        T.emplace_back(a);
    }
    for(ll i = 0;i < N;i++){
        ll C = 0;
        string A = "____________________";
        for(ll j = 0;j < T.size();j++){
            ll c = 0;
            string a = T[j] + T[j];
            for(ll k = 0;k < N;k++){
                for(ll l = m;l < m + 5;l++){
                    if(ss[a.substr(k,l)] != 0) c++;
                }
            }
            if(C < c){
                C = c;
                A = T[j];
            }
        }
        string a = A + A;
        for(ll k = 0;k < N;k++){
            for(ll l = m;l < m + 5;l++){
                if(ss[a.substr(k,l)] != 0) ss[a.substr(k,l)] = 0;
            }
        }
        ans[i] = A;
    }
    for(ll i = 0;i < N;i++) cout << ans[i] << endl;
}

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

using namespace std;
int main(){
    int N,Q;
    cin >> N >> Q;
    vector<unsigned long long> A(N);
    vector<unsigned long long> low(N);
    for (int i = 0; i < N; i++)
    {
        cin >> A[i];
        low[i]=A[i]-i-1;
    }

    vector<unsigned long long> K(Q);
    for (int i = 0; i < Q; i++)
    {
        cin >> K[i];
        if (low[N-1]<K[i])
        {
            K[i]+=N;
        }else{
            int idx = lower_bound(low.begin(),low.end(),K[i])-low.begin();
            K[i] +=idx;
        }
        cout << K[i] << endl;
    }

    return 0;
}


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

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

vector<int> v;

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

	int n; cin >> n; v.resize(n);
	for (auto& x : v) { cin >> x; }
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());

	set<int> ans; for(auto &x : v)
		for(int i = 1; i * i <= x; i++)
			if (x % i == 0) {
				if (i <= v[0]) ans.insert(i);
				if (x / i <= v[0]) ans.insert(x / i);
			}

	int res = 0; for (auto& x : ans) {
		int g = 0; for (auto& y : v)
			if (y % x == 0) g = gcd(y, g);
		res += g == x;
	}
	cout << res << '\n';
	cin.ignore(2); return 0;
}

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

// 約数列挙
vector<int> get(int n) {
    vector<int> ret;
    for (int i = 1; i * i <= n; ++i) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) {
                ret.push_back(n / i);
            }
        }
    }
    return ret;
}

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

class FGCDOrMIN {
public:
    void solve(std::istream& cin, std::ostream& cout) {
        int N;
        cin >> N;
        vector<int> A(N);
        REP(i, N) cin >> A[i];

        int mina = INF;
        map<int, int> m;
        REP(i, N) {
            for (auto n : get(A[i])) {
                if (m.count(n) == 0) {
                    m[n] = A[i];
                } else {
                    m[n] = gcd(m[n], A[i]);
                }
            }
            mina = min(mina, A[i]);
        }
        int res = 0;
        for (auto kv : m) {
            if (kv.second <= mina && kv.first == kv.second) {
                res++;
            }
        }
        cout << res << endl;
    }
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    FGCDOrMIN solver;
    solver.solve(in, out);
    return 0;
}


//in dp prefix suffix sum helps..
#include<iostream>
#include<vector>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stack>
#include <iterator>
#include <map>
#include<list>
#include <fstream>
#include<unordered_map>
#include<set>
#include<queue>
#define int  long long
#define double long double
#define pb   push_back
#define mp   make_pair
#define pii  pair<int,int>
#define pip pair<int, pair<int, int> >
#define viv vector<vector<int> >
#define vip vector< pair<int, int > >
#define vi vector<int>
#define mii  map<int, int>
#define li list<int>
#define qu queue<int>
#define vd vector<double>
#define set set<int>
#define stack stack<int>
#define fi first
#define se second
#define mem(x, y) memset(x, y, sizeof(x))
#define ps(x,y) fixed<<setprecision(y)<<x
using namespace std;
// author :: Anurag Anand.
using namespace std;
int z=  1e9+7;
//int z= 998244353;
int gcd(int a, int b){
    if(a==0)return b;
    if(b==0)return a;
    return gcd(b, a%b);
}
int power(int a,int b)
{
	int res=1;
	while(b)
	{
		if(b&1)
		{
			res=(res*a)%z;
			b--;
		}
		else
		{
			a=(a*a)%z;
			b=b>>1;
		}
	}
	return res;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    cout.tie(NULL);
	int t;
	t=1;
	//cin>>t;
	while(t--){
		int x;
		cin>>x;
		if(x>=0) cout<<x<<"\n";
		else cout<<"0\n";
	}
}

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

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

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

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

  long long x = 2;
  for (int i = 3; i < N+1; i++) {
    x = lcm(x, i);
  }

  cout << x + 1 << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int mxn=3e3+3;
int n,m;
int par[mxn];
inline int find(int x){return par[x]==x?x:par[x]=find(par[x]);}
inline void uni(int x,int y){par[find(x)]=find(y);}
set<int>s1,s2;
int main(){
	ios_base::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=1;i<=n+m;++i)par[i]=i;
	uni(1,n),uni(1,n+1),uni(1,n+m);
	for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){
		char c;cin>>c;
		if(c=='#')uni(i,n+j);
	}
	for(int i=1;i<=n;++i)s1.insert(find(i));
	for(int i=1;i<=m;++i)s2.insert(find(i+n));
	cout<<min(s1.size(),s2.size())-1<<'\n';
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn = 2e5+5;
#define mod 1000000009

void solve(){

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

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

  sort(a.begin(),a.end());
  vector<int> widths;
  for(int i=0;i<m-1;i++){
  	if(a[i+1]-a[i]-1 > 0)
  	widths.push_back(a[i+1]-a[i]-1);
  }

  if(a[0] > 1)
   widths.push_back(a[0]-1);
  if( n-a[m-1] > 0)
  widths.push_back(n-a[m-1]);

  // for(auto i: widths) cout << i <<" ";
  // 	cout << endl;
  if( widths.empty() || m == 0) {
  	cout << 0 <<endl;
  	return;
  }
  int mn = *min_element(widths.begin(),widths.end());
  int ans =0;

  for(auto i : widths){
  	ans += (i+mn-1)/mn;
  }

  if(widths.empty()) cout << 0 << endl;
  else cout << ans << endl;
}

int main(){
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   int tc = 1;
  // cin >> tc;
   while(tc--) solve();

 return 0;
}