#include <stdio.h>
#include <limits.h>

#define MAX 200000
#define INF INT_MAX
typedef long long ll;

void merge(int*, int, int, int);
void mergesort(int*, int, int);

ll ans;

int n, a[MAX];

int main(){
  int i;

  scanf("%d" ,&n);
  for(i = 0 ; i < n ; i++){
    scanf("%d" ,a + i);
  }
  mergesort(a,0,n);
  printf("%lld\n" ,ans);

  return 0;
}

void merge(int a[], int left, int mid, int right){
  int n1 = mid - left, n2 = right - mid;
  int L[n1+1], R[n2+1], i, j, k;

  for(i = 0 ; i < n1 ; i++){
    L[i] = a[left + i];
  }

  for(i = 0 ; i < n2 ; i++){
    R[i] = a[mid + i];
  }

  L[n1] = R[n2] = INF;

  for(i = 0, j = 0, k = left ; k < right ; k++){
    if(i < n1 && (j == n2 || L[i] <= R[j])){
      a[k] = L[i++];
    }else{
      ans += (n1 + n2) / 2 - i;
      a[k] = R[j++];
    }
  }
}

void mergesort(int a[], int left, int right){
  int mid;

  if(left + 1 < right){
    mid = (left + right) / 2;
    mergesort(a, left, mid);
    mergesort(a, mid, right);
    merge(a, left, mid, right);
  }
}

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
#define MAX_N 200010
#define int long long

int bit[MAX_N], n, a[MAX_N], ma, b[MAX_N];

int sum(int i){
	int s = 0;

	while(i > 0){
		s += bit[i];
		i -= i & -i;
	}
	return s;
}

void add(int i , int x){
	while(i <= n){
		bit[i] += x;
		i += i & -i;
	}
}

signed main(){
	int ans = 0;
	map<int,int> ma;
	cin>>n;

	for(int i = 0; i < n; i++){
		scanf("%d", &b[i]);
		b[i]++;
		a[i] = b[i];
	}
	sort(b,b+n);
	for(int i = 0; i < n; i++)
		ma[b[i]] = i+1;
	for(int i = 0; i < n; i++)
		a[i] = ma[a[i]];

	for(int i = 0; i < n; i++){
		ans += i - sum(a[i]);
		add(a[i],1);
	}
	cout<<ans<<endl;
	return 0;
}


#include<cstdio>
#include<algorithm>

using namespace std;

int main(void) {
    char list[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    while(true) {
        int n,k[200];
        char s[150]={};

        scanf("%d", &n);
        if(!n) break;

        for(int i=0; i<n; i++)
            scanf("%d", &k[i]);

        scanf("%s",s);

        for(int i=0; s[i]; i++) {
            printf("%c", list[((lower_bound(list, list+52, s[i])-list)-k[i%n]+52)%52]);
        }
        printf("\n");
    }
    return 0;
}

#include <iostream>
using namespace std;

int main()
{
	int n,t;
	while(cin >> n, n)
	{
		int k[10] = {0};
		while(n--)
		{
			cin >> t;
			k[t]++;
		}

		for(int i=0; i<10; i++)
		{
			if(k[i])
			{
				for(int j=0; j<k[i]; j++)
				{
					cout << '*';
				}
				cout << endl;
			}
			else
			{
				cout << '-' << endl;
			}
		}
	}
}


// lcmとか__builtin_popcountとかはg++ -std=c++17 default.cppみたいなかんじで

#include <bits/stdc++.h>
#define mod 1000000007
#define INF LLONG_MAX
#define ll long long
#define ln cout<<endl
#define Yes cout<<"Yes"<<endl
#define NO cout<<"NO"<<endl
#define YES cout<<"YES"<<endl
#define No cout<<"No"<<endl
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
using namespace std;
ll dx[4]={1,0,-1,0};
ll dy[4]={0,1,0,-1};

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

    ll  a,b,c,d,e,n,k,maxi=0,f=0,mini=INF,sum=0,q;
	string str,stra,straa;
    ll x,y,w,z;
    char ca,cb,cc;
    cin>>a>>b>>c>>k;
    d=max(max(a,b),c);
    e=d;
    sum=a+b+c;
    rep(i,k){
        d*=2;
    }
    cout<<sum+d-e<<endl;
    return 0;
}

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

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

  int K;
  cin >> K;

  int multi;
  for (int i = 0; i < K; i++)
  {
    multi = max(A, max(B, C)) * 2;
    if (A * 2 == multi) A = multi;
    else if (B * 2 == multi)
    {
      B = multi;
    }
    else
    {
      C = multi;
    }

  }

  cout << A + B + C << endl;
}


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

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9+7;
const int maxn = 1e6 + 5;

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

    vector<ll> v(2); int k;
    cin>>v[0]>>v[1]>>k;

    for (int i=0; i<k; i++) {
	if (v[i%2]%2) {
	    v[i%2]--;
	} else {
	}
	int o = 1^(i%2);
	v[o] += (v[i%2]/2);
	v[i%2] /= 2;

    }

    cout<<v[0]<<" "<<v[1]<<endl;

    return 0;
}

// tsukasa_diary's programing contest code template
#include <bits/stdc++.h>
using namespace std;
// define
#define for_(i,a,b) for(int i=a;i<b;++i)
#define for_rev(i,a,b) for(int i=a;i>=b;--i)
#define allof(a) a.begin(),a.end()
#define minit(a,b) memset(a,b,sizeof(a))
#define size_of(a) (int)a.size()
// typedef
typedef long long lint;
typedef double Double;
typedef pair<int, int> pii;
//
typedef vector< int > Array;
typedef vector< Array > Matrix;
typedef vector< Double > DArray;
typedef vector< DArray > DMatrix;
typedef vector< string > SArray;
typedef vector< pii > PArray;
// popcount
inline int POPCNT(int _x) { return __builtin_popcount(_x); }
inline int POPCNT(lint _x) { return __builtin_popcountll(_x); }
// inf
const int iINF = 1L << 30;
const lint lINF = 1LL << 60;
// eps
Double EPS = 1e-9;
// in range
inline bool in_range(int _v, int _mx, int _mi) { return _mi <= _v && _v < _mx; }
inline bool in_range(Double _v, Double _mi, Double _mx) { return -EPS < _v - _mi && _v - _mx < EPS; }
inline bool in_range(int _x, int _y, int _W, int _H) { return 0 <= _x && _x < _W && 0 <= _y && _y < _H; }
// neighbor clockwise
const int DX[4] = {0,1,0,-1}, DY[4] = {-1,0,1,0};
const int DX_[8] = {0,1,1,1,0,-1,-1,-1}, DY_[8] = {-1,-1,0,1,1,1,0,-1};

int pointW[10], pointK[10];

void solve() {
	sort(pointW, pointW + 10, greater<int>());
	sort(pointK, pointK + 10, greater<int>());

	cout << pointW[0] + pointW[1] + pointW[2] << " " << pointK[0] + pointK[1] + pointK[2] << endl;
}

int main() {
	for_(i,0,10) cin >> pointW[i];
	for_(i,0,10) cin >> pointK[i];
	solve();
	return 0;
}


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

int read()
{
	int s=0;
	char c=getchar(),lc='+';
	while (c<'0'||'9'<c) lc=c,c=getchar();
	while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar();
	return lc=='-'?-s:s;
}
void write(int x)
{
	if (x<0)
	{
		putchar('-');
		x=-x;
	}
	if (x<10) putchar(x+'0');
	else
	{
		write(x/10);
		putchar(x%10+'0');
	}
}
void print(int x,char c='\n')
{
	write(x);
	putchar(c);
}

signed main()
{
	int n=read(),k=read();
	print(n-k+1);

	return 0;
}

#include<iostream>
#include<string>
using namespace std;
int main(){
  int k;
  cin>>k;
  string s,S;
  cin>>s;
  if(s.length()<=k)
    cout<<s;
  else{
    for(int i=0;i<k;i++){
    	cout<<s[i];
	}
  cout<<"...";
}
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define erep(i,a,n) for(int i = a;i<=n;i++)
typedef long long ll;
#define int long long
#define vint vector<int>
#define vvint vector<vector<int>>
#define vstring vector<string>
#define vdouble vector<double>
#define vll vector<ll>:
#define vbool vector<bool>
#define INF 1101010101010101010
#define MOD 1000000007
#define P = pair<int,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; }

signed main(){
  int n,m;
  cin >> n >> m;
  vint A(n-1+m),B(n-1+m);
  vvint to(n);
  vint in(n);
  vint dist(n,0);
  rep(i,0,n-1+m){
    cin >> A[i] >> B[i];
    A[i]--;B[i]--;
    to[A[i]].push_back(B[i]);
    in[B[i]]++;
  }
  queue<int> que;
  vint ans(0,0);
  rep(i,0,n){
    if(in[i] == 0) que.push(i);
  }
  while(!que.empty()){
    int now = que.front();
    que.pop();
    ans.push_back(now);
    for(auto p : to[now]){
      in[p]--;
      if(in[p] == 0) que.push(p);
      dist[p] = max(dist[now]+1,dist[p]);
    }
  }
  vint par(n,-1);
  rep(i,0,n-1+m){
    int a = A[i];
    int b = B[i];
    if(dist[b] - dist[a] == 1) par[b] = a;
  }
  rep(i,0,n) cout << par[i]+1 << endl;
}

#include <bits/stdc++.h>
#define N 100020
#define ll long long
using namespace std;
inline int read(){
  int x=0,f=1;char ch=getchar();
  while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
  while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
  return f?x:-x;
}
int a[N];
vector<int> sig;
int main(int argc, char const *argv[]) {
  int n = read(), m = read();
  for (int i = 1; i <= m; ++ i) {
    a[i] = read();
    if (a[i] & 1) {
      sig.push_back(i);
    }
  }
  size_t sigsize = sig.size();
  if (sigsize > 2) {
    return puts("Impossible"), 0;
  }
  if (sigsize > 0) {
    swap(a[1], a[sig[0]]);
  }
  if (sigsize > 1) {
    swap(a[m], a[sig[1]]);
  }

  if (m == 1) {
    if (a[1] == 1) {
      puts("1\n1\n1");
    } else {
      printf("%d\n2\n%d 1\n", a[1], a[1] - 1);
    }
    return 0;
  }

  for (int i = 1; i <= m; ++ i) {
    printf("%d ", a[i]);
  }
  puts("");

  printf("%d\n", a[m] == 1 ? m - 1 : m);
  printf("%d", a[1] + 1);
  for (int i = 2; i < m; ++ i) {
    printf(" %d", a[i]);
  }
  if (a[m] != 1) {
    printf(" %d\n", a[m] - 1);
  } else {
    puts("");
  }
  return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vec = vector<ll>;
using vecp = vector<P>;
using mat = vector<vec>;
using matp = vector<vecp>;
const ll MOD = 1000000007;
const ll INF = 10000000000000;
#define all(v) v.begin(), v.end()

int main(){
  ll N,ans=0;
  cin >> N;
  vecp A(N),B(N);
  rep(i,N){
    cin >> B.at(i).first >> B.at(i).second;
  }
  rep(i,N){
    cin >> A.at(i).first >> A.at(i).second;
  }
  sort(all(A));
  sort(all(B));
  set<ll> C;
  ll j=0;
  rep(i,N){
    while(j<N&&B.at(j).first<A.at(i).first){
      C.insert(B.at(j).second);
      j++;
    }
    if(!C.empty()){
      auto a=C.lower_bound(A.at(i).second);
      if(a!=C.begin()){
        a--;
        ans++;
        C.erase(a);
      }
    }
  }
  cout << ans << endl;

}

#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int> > RHEAP;

const int N = 100010, M = 1010;

int n;
int h[N], e[N], ne[N], idx;
int match[N];
bool st[N];
PII point[N];

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

bool find(int u)
{
	for (int i = h[u]; ~i; i = ne[i])
	{
		int j = e[i];
		if (!st[j])
		{
			st[j] = true;
			if (match[j] == 0 || find(match[j]))
			{
				match[j] = u;
				return true;
			}
		}
	}

	return false;
}

int main()
{
	memset(h, -1, sizeof h);

	cin >> n;
	for (int i = 1; i <= n; i ++ ) cin >> point[i].F >> point[i].S;
	for (int i = n + 1; i <= 2 * n; i ++ ) cin >> point[i].F >> point[i].S;

	for (int i = 1; i <= n; i ++ )
		for (int j = n + 1; j <= 2 * n; j ++ )
			if (point[i].F < point[j].F && point[i].S < point[j].S)
				add(i, j);

	int res = 0;
	for (int i = 1; i <= n; i ++ )
	{
		memset(st, 0, sizeof st);
		if (find(i)) res ++ ;
	}
	cout << res << endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const long long oo=1e15;
struct Node
{
	Node *lch,*rch;
	int l,r;
	long long v1,v2,lz;
}*rt;
void update(Node *x)
{
	x->v1=min(x->lch->v1,x->rch->v1);
	x->v2=min(x->lch->v2,x->rch->v2);
}
void add(Node *x,long long v)
{
	x->lz+=v;
	x->v1+=v;
	x->v2+=v;
}
void down(Node *x)
{
	if(x->lz)
	{
		add(x->lch,x->lz);
		add(x->rch,x->lz);
		x->lz=0;
	}
}
int a,b;
Node *Build(int l,int r)
{
	Node *ret=new Node;
	ret->l=l;
	ret->r=r;
	ret->lz=0;
	if(l==r)
	{
		ret->v1=abs(l-a)-l;
		ret->v2=abs(l-a)+l;
		ret->lch=ret->rch=NULL;
		return ret;
	}
	int mid=(l+r)>>1;
	ret->lch=Build(l,mid);
	ret->rch=Build(mid+1,r);
	update(ret);
	return ret;
}
long long query(Node *x,int l,int r,int dir)
{
	if(l<=x->l&&r>=x->r)
	{
		if(dir==1)
			return x->v1;
		else
			return x->v2;
	}
	down(x);
	int mid=(x->l+x->r)>>1;
	long long ret=oo;
	if(l<=mid)
		ret=min(ret,query(x->lch,l,r,dir));
	if(mid<r)
		ret=min(ret,query(x->rch,l,r,dir));
	return ret;
}
void modify(Node *x,int p,long long v)
{
	if(x->l==x->r)
	{
		x->v1=min(x->v1,v-p);
		x->v2=min(x->v2,v+p);
		return ;
	}
	down(x);
	int mid=(x->l+x->r)>>1;
	if(p<=mid)
		modify(x->lch,p,v);
	else
		modify(x->rch,p,v);
	update(x);
}
long long findans(Node *x)
{
	if(x->l==x->r)
		return x->v1+x->l;
	down(x);
	return min(findans(x->lch),findans(x->rch));
}
int main()
{
	int n,q,la,x;
	long long mi;
	scanf("%d%d%d%d",&n,&q,&a,&b);
	la=b;
	rt=Build(1,n);
	for(int i=1;i<=q;i++)
	{
		scanf("%d",&x);
		mi=min(query(rt,1,x,1)+x,query(rt,x,n,2)-x);
		add(rt,abs(x-la));
		modify(rt,la,mi);
		la=x;
	}
	printf("%lld",findans(rt));
	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define getbit(n, i) (((n) & (1LL << (i))) != 0)
#define setbit0(n, i) ((n) & (~(1LL << (i))))
#define setbit1(n, i) ((n) | (1LL << (i)))
#define togglebit(n, i) ((n) ^ (1LL << (i)))
#define lastone(n) ((n) & (-(n)))
char gap = 32;
template<typename T>
ostream& operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for (const auto &x : v) os << gap << x;
        return os << '}';
}
template<typename A, typename B>
ostream& operator<<(ostream &os, const pair<A, B> &p) {
        return os << '(' << p.first << gap << p.second << ')';
}

template <class T>
void read(T &x) {
    char c;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        ;
    for (x = 0; c <= '9' && c >= '0'; c = getchar()) x = x * 10 + (c & 15);
}
#define ll long long
#define lll __int128_t
#define pb push_back
#define mp make_pair
typedef pair < int,int> ii;
typedef vector<ii> vii;
typedef vector<ll>vl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll hashPrime = 1610612741;
ll count1(string s){
    ll mini = 0;
    ll cnt = 0;
    ll sz = s.size();
    for (ll i = 0; i < sz;i++){
        if(s[i]==')')
            cnt--;
        else
        {
            cnt++;
        }
        mini = min(mini, cnt);
    }
    return abs(mini);
}
ll count2(string s){
    ll mini = 0;
    ll cnt = 0;
    ll sz = s.size();
    for (ll i = sz - 1; i >= 0; i--)
    {
        if(s[i]=='(')
            cnt--;
        else
        {
            cnt++;
        }
        mini = min(mini, cnt);
    }
    return abs(mini);
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll i, j, k, l, n, m;
    cin >> n;
    vector<pair<ll, ll>> type1, type2, type3;
    ll bal = 0;
    ll bal1 = 0;
    for (i = 1; i <= n;i++){
        string s;
        cin >> s;
        for(auto x:s){
            if(x=='(')
                bal++;
            else
            {
                bal--;
            }
        }
        ll cnt1 = count1(s);
        bal1 += cnt1;
        ll cnt2 = count2(s);
        bal1 -= cnt2;
        if(cnt1+cnt2==0)
            continue;
        if(cnt2-cnt1>=0){
            type1.pb({cnt1, cnt2});
        }
        else{
            type2.pb({cnt2, cnt1});
        }
    }
    sort(type1.begin(), type1.end());
    sort(type2.begin(), type2.end());
    if (bal!=0||bal1!=0){
        cout << "No\n";
        return 0;
    }
    ll prev = 0;
    ll sz1 = type1.size();
    ll sz2 = type2.size();
    for (i = 0; i < sz1;i++){
        if(type1[i].first>prev){
            cout << "No\n";
            return 0;
        }
        else{
            prev -= type1[i].first;
            prev += type1[i].second;
        }
    }
    for (i = sz2-1; i >=0;i--){
        if(type2[i].second>prev){
            cout << "No\n";
            return 0;
        }
        else{
            prev += type2[i].first;
            prev -= type2[i].second;
        }
    }
    cout << "Yes\n";
    return 0;
}


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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define what_is(x) cerr << #x << " is " << x << endl;
#define MT make_tuple
#define eb emplace_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define FOR(i,n) for (int i=0;i < n ; i++)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

int main(){
	ll n;
	ll i;
	cin >> n;
	i=0;
	while((i+1)*(i+1)<=n) i++;
	cout << i*i;

	return 0;
}

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

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
  return string(1, val);
}
int char_to_int(char val) {
  return val - '0';
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int vector_finder(std::vector<ll> vec, int number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

map< ll, int > prime_factor(ll n) {
  map< ll, int > ret;
  for(ll i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}

int main() {
  int N; cin >> N;
  map<ll, int> m;
  for(int i = 1; i <= N; ++i) {
    auto a = prime_factor(i);
    for(auto x : a) {
      m[x.first] += x.second;
    }
  }
  ll ans = 1;
  for(auto x : m) {
    ans = ans * (x.second + 1) % 1000000007;
  }
  cout << ans << endl;
}


//cwystc <--> 陈威宇是天才  --by cwy
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
const double PI=acos(-1);
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for (int i=(a);i<=(b);i++)
#define per(i,a,b) for (int i=(a);i>=(b);i--)
#define Rep(i,a,b) for (int i=(a);i<(b);i++)
#define Per(i,a,b) for (int i=(a);i>(b);i--)
#define travel_set(it,a) for (set<int>::iterator (it)=(a).begin();(it)!=(a).end();(it)++)
#define travel_map(it,a) for (map<int>::iterator (it)=(a).begin();(it)!=(a).end();(it)++)
#define travel_vec(it,a) for (vector<int>::iterator (it)=(a).begin();(it)!=(a).end();(it)++)
#define p(x) cout << x << endl;
#define def cout << "find" << endl;
#define what_is(x) cout << #x << " is " << x << endl;
template <class T>T sqr(T x){return x*x;}
template <class T>T alex(T x){return (x>0)?x:-x;}
template <class T>void read(T&x){
	x=0;T f=1;char ch=getchar();
	while ((ch<48||ch>57)&&ch!=45)ch=getchar();
	if (ch==45)f=-1,ch=getchar();
	while (ch>=48&&ch<=57)x=x*10+ch-48,ch=getchar();
	x*=f;
}

//-------------------------------------------------head-------------------------------------------------

#define maxn 305
int f[maxn][maxn][maxn];
char s[maxn],_s[maxn];int k,len;
int main(){
	scanf("%s",s+1);len=strlen(s+1);read(k);
	rep(i,1,len)_s[i]=s[len-i+1];
	// rep(i,0,len)f[i][0][0]=f[0][i][0]=0;
	rep(i,1,len)rep(j,1,len)if (i!=0&&j!=0){
		f[i][j][0]=max(max(f[i-1][j][0],f[i][j-1][0]),f[i-1][j-1][0]+(s[i]==_s[j]));
		rep(kk,1,k){
			f[i][j][kk]=max(max(f[i-1][j][kk],f[i][j-1][kk]),f[i-1][j-1][kk-(s[i]!=_s[j])]+1);
		}
	}
	int res=0;
	rep(i,1,len)rep(kk,0,k)res=max(f[i][len-i][k]*2,res);
	rep(i,1,len)rep(kk,0,k)res=max(f[i-1][len-i][kk]*2+1,res);printf("%d\n",res);return 0;
}

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);

#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	a = (a<MOD)?a:(a-MOD);
	b = (b<MOD)?b:(b-MOD);
	a += b;
	a = (a<MOD)?a:(a-MOD);
}

string s;
bool done[334][334][334];
int memo[334][334][334];

int solve(int i, int j, int k){
	int l;
	if(done[i][j][k]){
		return memo[i][j][k];
	}
	done[i][j][k] = true;
	if(i==j){
		return memo[i][j][k] = 1;
	}else if(i>j){
		return memo[i][j][k] = 0;
	}
	memo[i][j][k] = max(solve(i+1,j,k),solve(i,j-1,k));
	if(s[i]==s[j]){
		memo[i][j][k] = max(memo[i][j][k],solve(i+1,j-1,k)+2);
	}else if(k){
		memo[i][j][k] = max(memo[i][j][k],solve(i+1,j-1,k-1)+2);
	}
	return memo[i][j][k];
}

int main(void){
	int k;
	cin >> s >> k;
	cout << solve(0,s.l_ength()-1,k) << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define Hello ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define ll long long
int main(){
    Hello
    int n, m;
    cin >> n >> m;
    int mx = 0, mn = 1e9;
    for(int i = 0, l, r; i < m; i++){
        cin >> l >> r;
        mx = max(mx, l);
        mn = min(mn, r);
    }
    cout << max(0, mn - mx + 1);
    return 0;
}

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

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

    int K, T;
    cin >> K >> T;
    vector<int> a(T);
    priority_queue<P> pq;
    REP(i, T) {
        cin >> a[i];
        pq.emplace(a[i], i);
    }
    while (pq.size() >= 2) {
        int b, c, i, j;
        tie(b, i) = pq.top();
        pq.pop();
        tie(c, j) = pq.top();
        pq.pop();
        if (b > 1) {
            pq.emplace(b - 1, i);
        }
        if (c > 1) {
            pq.emplace(c - 1, j);
        }
    }
    cout << pq.top().first - 1 << endl;

    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n); for(int i = 0;i < (n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
long double pi = acos(-1);
const int INF = 1001001001;

// 最小公倍数を求める
int euqlid (int a, int b){
  int temp;
  temp = a % b;
  if(temp == 0)return b;
  return euqlid(b,temp);
}
ll conbination(ll a,ll b){
  ll u = 1;
  ll d = 1;
  while(b != 0){
    u *= a;
    d *= b;
    a--;
    b--;
  }
  return u / d;
}
int strtoint(char s){
  return (int(s-'0'));
}
int fact(int n){
  if(n == 1)return 1;
  return n * fact(n-1);
  }

int main(){
  int a[4];
  rep(i,4)cin >> a[i];
  cout << min(a[0],a[1])+min(a[2],a[3]) << endl;
}

#include <iostream>
using namespace std;

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

    if(a <= b) {
        if(c < d){
            cout << a + c << endl;
        }else{
            cout << a + d << endl;
        }
    }else{
        if(c < d){
            cout << b + c << endl;
        }else{
            cout << b + d << endl;
        }
    }

    return 0;
}


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

using namespace std;

int findNum(vector<pair<int, long long> > comm, int num) {
    for(int i=0; i<comm.size(); i++)  {
      if(comm[i].first == num) {
	return i;
      }
    }
    return comm.size();
  }

int main() {
  int n;
  cin>>n;
  while(n != 0) {
    int m = n;
    vector<pair<int, long long> > comm;
    while(m--) {
      int num;
      long long price, unit;
      cin>>num>>price>>unit;
      int dataNum = findNum(comm, num);
      if(dataNum == comm.size()) {
        comm.push_back(make_pair(num, price*unit));
      }
      else {
        comm[dataNum].second += price*unit;
      }
    }

    bool flag = true;
    for(int i=0; i<comm.size(); i++) {
      if(comm[i].second >= 1000000) {
        cout<<comm[i].first<<endl;
        flag = false;
      }
    }
    if(flag == true) {
      cout<<"NA"<<endl;
    }
    cin>>n;
  }
}

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

using namespace std;

vector<int> divisor(int n){

  vector<int>res;
  for(int i=1; i*i<=n; i++){
    if(n%i==0){
      res.push_back(i);
      if(i!=n/i)res.push_back(n/i);
    }
  }
  return res;
}

int main(void){

  int p,l,n,m;
  vector<int>L,N,M;

  cin >> p;

  if(p==2){

    cin >> n >> m;

    N.clear();
    M.clear();

    N=divisor(n);
    M=divisor(m);

    sort(N.begin(),N.end());
    sort(M.begin(),M.end());

    for(int i=0;i<N.size();i++)
      if(binary_search(M.begin(),M.end(),N[i]))cout << N[i] << endl;

  }
  else if(p==3){

    cin >> l >> n >> m;

    L.clear();
    N.clear();
    M.clear();

    L=divisor(l);
    N=divisor(n);
    M=divisor(m);

    sort(L.begin(),L.end());
    sort(N.begin(),N.end());
    sort(M.begin(),M.end());

    for(int i=0;i<N.size();i++)
      if(binary_search(M.begin(),M.end(),N[i]) &&
	 binary_search(L.begin(),L.end(),N[i]))cout << N[i] << endl;
  }
  return 0;
}


#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 mp make_pair
#define pb(x) push_back(x)
#define vll vector<long long>
#define pll pair<long long, long long>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1e18

typedef long long ll;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> o_tree;

bool comp(array<ll,2> a,array<ll,2> b)
{
  if(a[0]==b[0])
  {
    return a[1]>b[1];
  }
  return a[0]<b[0];
}

void solve()
{
  ll n,t;
  cin>>n>>t;
  ll i,j;
  ll mx=5000+t;
  ll dp[n][mx];
  clr(dp);

  array<ll,2>a[n];
  for(i=0;i<n;i++)
  {
    cin>>a[i][0]>>a[i][1];
  }
  sort(a,a+n,comp);
  // reverse(a,a+n);

  for(i=0;i<n;i++)
  {
    for(j=0;j<mx;j++)
    {
      dp[i][j]=max(dp[i][j],(i>0)?dp[i-1][j]:0ll);
    }
    for(j=0;j<t;j++)
    {
      if(j+a[i][0]<mx)
      {
        dp[i][a[i][0]+j]=max(dp[i][a[i][0]+j],(i>0?dp[i-1][j]:0ll)+a[i][1]);
      }
    }
  }
  ll res = *max_element(dp[n-1],dp[n-1]+mx);
  cout<<res<<"\n";
}

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

  if (fopen("input.txt", "r"))
  {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  cout << setprecision(20);

  ll t = 1;

  // cin>>t;

  while (t--)
  {
    solve();
  }

  return 0;
}

#include <bits/stdc++.h>

int main(){
    int N, T;
    std::cin >> N >> T;
    std::vector< std::pair< int, int > > AB(N);
    for(int i=0; i<N; i++){
        std::cin >> AB[i].first >> AB[i].second;
    }

    std::sort(AB.begin(), AB.end());

    std::vector< std::vector< int > > dp(N + 1, std::vector< int >(T));
    dp[0].assign(T, 0);
    int max = 0;
    for(int i=1; i<N+1; i++){
        if(max < dp[i-1][T-1] + AB[i-1].second){
            max = dp[i-1][T-1] + AB[i-1].second;
        }
        for(int j=0; j<T; j++){
            if(j < AB[i-1].first){
                dp[i][j] = dp[i-1][j];
            }else{
                dp[i][j] = std::max(dp[i-1][j], dp[i-1][j-AB[i-1].first] + AB[i-1].second);
            }
        }
    }

    std::cout << max << std::endl;

    return 0;
}


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

int main() {
  int N,M;
  cin >> N;
  vector<string> blue(N);
  int MAX = 0;

  for(int i = 0; i < N; i++){
     cin >> blue.at(i);
  }

  cin >> M;
  vector<string> red(M);

  for(int i = 0; i < M; i++){
    cin >> red.at(i);
  }

  int count;
  string S;

  for(int i = 0; i < N; i++){
    count = 0;
    S = blue.at(i);
    for(int j = 0; j < N; j++){
      if(S == blue.at(j)){
        count++;
      }
    }
   for(int k = 0; k < M; k++){
     if(S == red.at(k)){
       count--;
     }
   }
   if(MAX < count){
     MAX = count;
   }

  }

  if(MAX < 1){
    cout << 0 << endl;
  }else{
  cout << MAX << endl;
  }

}

#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <set>
#include <array>
#include <stdio.h>
#include <string.h>
#include <random>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v,a) (reverse((v), (v)+(a)))
#define REP(i, n) for (int (i)=0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
#define CONTAINS_SET(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }

int N;
int M;
map<string, int> S;
map<string, int> T;

signed main()
{
	cin >> N;
	REP(i, N)
	{
		string s;
		cin >> s;
		if (CONTAINS_MAP(S, s))
		{
			S[s]++;
		}
		else
		{
			S[s] = 1;
		}
	}
	cin >> M;
	REP(i, M)
	{
		string s;
		cin >> s;
		if (CONTAINS_MAP(T, s))
		{
			T[s]++;
		}
		else
		{
			T[s] = 1;
		}
	}

	int ma = 0;
	for (auto pair : S)
	{
		int n = pair.second;
		if (CONTAINS_MAP(T, pair.first))
		{
			n -= T[pair.first];
		}
		ma = max(ma, n);
	}
	cout << ma << endl;
}


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

int main()
{
    int n;
    cin >> n;
    string res = "";
    while(n != 0)
    {
        if(n % 2 == 0) res = "0" + res;
        else
        {
            res = "1" + res;
            n--;
        }
        n /= -2;
    }
    if(res == "") res = "0";
    cout << res << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll n;
  cin >> n;
  if(n == 0) {
    cout << 0 << endl;
    return 0;
  }
  vl v(62, 0);
  rep(i, 62) v[i] = pow(2, i);
  ll now = 0;
  vl ans(62, 0);
  ll i = 0;
  if(n < 0) {
    n = abs(n);
    i = 1;
  }
  for(; now < n; i += 2) {
    now += v[i];
    ans[i] = 1;
  }
  for(; i >= 0; i--) {
    if(now - v[i] >= n) {
      now -= v[i];
      ans[i] = 1 - ans[i];
    }
  }
  reverse(all(ans));
  bool ok = false;
  rep(i, 62) {
    if(ans[i] == 1) ok = true;
    if(ok) cout << ans[i];
  }
  cout << endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
const double PI = acos(-1);
const double EPS = 1e-8;
int n;
double x[102], y[102];
vector<double> v;

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++) {
        scanf("%lf %lf", &x[i], &y[i]);
    }

    for (int i = 1; i <= n; i ++) {
        v.clear();
        for (int j = 1; j <= n; j ++) if (i != j) {
            double a1 = atan2(y[j]-y[i], x[j]-x[i]);
            v.push_back(a1);
        }

        sort(v.begin(), v.end());
        v.push_back(v[0] + 2*PI);

        int gg = 0; double gap = 0;
        for (int j = 0; j < (int)v.size() - 1; j ++) {
            //printf("%.3f\n", v[j]);
            if (v[j+1] - v[j] + EPS > PI) {
                gg ++; gap = v[j+1] - v[j];
            }
        }
        if (gg >= 2 || gap < PI) {
            printf("%.7f\n", 0.0);
            continue;
        }
        //printf("%.6f\n", gap);

        printf("%.7f\n", (gap-PI) / 2 / PI);

    }
}

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

#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(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

int main() {
    int N; cin >> N;
    vector<int> X(N);
    vector<int> Y(N);
    vector<char> U(N);
    REP(i,N) cin >> X[i] >> Y[i] >> U[i];

    int ans = INF;

    vector<vector<pair<int,char>>> H(1e6);//縦方向

    REP(i,N){
        if(U[i]=='U' || U[i]=='D'){
            H[X[i]].push_back({Y[i],U[i]});
        }
    }
    REP(i,H.size()){
        if(H[i].size()==0) continue;
        sort(ALL(H[i]));
        REP(j,H[i].size()-1){
            if(H[i][j].second=='U' && H[i][j+1].second=='D'){
                ans = min(ans, 5*(H[i][j+1].first - H[i][j].first));
            }
        }
    }

    vector<vector<pair<int,char>>> V(1e6);//横方向
    REP(i,N){
        if(U[i]=='R' || U[i]=='L'){
            V[Y[i]].push_back({X[i],U[i]});
        }
    }
    REP(i,V.size()){
        if(V[i].size()==0) continue;
        sort(ALL(V[i]));
        REP(j,V[i].size()-1){
            if(V[i][j].second=='R' && V[i][j+1].second=='L'){
                ans = min(ans, 5*(V[i][j+1].first - V[i][j].first));
            }
        }
    }

    vector<int> XX(N);
    vector<int> YY(N);

    REP(i,N){//45度回転
        XX[i] = X[i]-Y[i]+200000;
        YY[i] = X[i]+Y[i]+200000;
    }

    vector<vector<pair<int,char>>> V1(1e6);//横方向
    REP(i,N){
        if(U[i]=='R' || U[i]=='U'){
            V1[YY[i]].push_back({XX[i],U[i]});
        }
    }
    REP(i,V1.size()){
        if(V1[i].size()==0) continue;
        sort(ALL(V1[i]));
        REP(j,V1[i].size()-1){
            if(V1[i][j].second=='R' && V1[i][j+1].second=='U'){
                ans = min(ans, 5*(V1[i][j+1].first - V1[i][j].first));
            }
        }
    }

    vector<vector<pair<int,char>>> V2(1e6);//横方向
    REP(i,N){
        if(U[i]=='D' || U[i]=='L'){
            V2[YY[i]].push_back({XX[i],U[i]});
        }
    }
    REP(i,V2.size()){
        if(V2[i].size()==0) continue;
        sort(ALL(V2[i]));
        REP(j,V2[i].size()-1){
            if(V2[i][j].second=='D' && V2[i][j+1].second=='L'){
                ans = min(ans, 5*(V2[i][j+1].first - V2[i][j].first));
            }
        }
    }

    vector<vector<pair<int,char>>> H1(1e6);//横方向
    REP(i,N){
        if(U[i]=='R' || U[i]=='D'){
            H1[XX[i]].push_back({YY[i],U[i]});
        }
    }
    REP(i,H1.size()){
        if(H1[i].size()==0) continue;
        sort(ALL(H1[i]));
        REP(j,H1[i].size()-1){
            if(H1[i][j].second=='R' && H1[i][j+1].second=='D'){
                ans = min(ans, 5*(H1[i][j+1].first - H1[i][j].first));
            }
        }
    }

    vector<vector<pair<int,char>>> H2(1e6);//横方向
    REP(i,N){
        if(U[i]=='U' || U[i]=='L'){
            H2[XX[i]].push_back({YY[i],U[i]});
        }
    }
    REP(i,H2.size()){
        if(H2[i].size()==0) continue;
        sort(ALL(H2[i]));
        REP(j,H2[i].size()-1){
            if(H2[i][j].second=='U' && H2[i][j+1].second=='L'){
                ans = min(ans, 5*(H2[i][j+1].first - H2[i][j].first));
            }
        }
    }

    if(ans == INF) cout << "SAFE" << endl;
    else cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2) \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                            \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                        \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " \
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...) \
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#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()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

ll N;
std::vector<ll> a;
vector<vector<vector<double>>> dp;

double f(ll c1, ll c2, ll c3) {
  if (dp[c1][c2][c3] != -1.0) return dp[c1][c2][c3];

  if (c1 == 0 && c2 == 0 && c3 == 0) return 0.0;

  double ret = 0.0;
  if (c1 > 0) ret += f(c1 - 1, c2, c3) * c1;
  if (c2 > 0) ret += f(c1 + 1, c2 - 1, c3) * c2;
  if (c3 > 0) ret += f(c1, c2 + 1, c3 - 1) * c3;
  ret += N;
  ret *= 1.0 / (c1 + c2 + c3);
  return dp[c1][c2][c3] = ret;
}

void solve() {
  dp.resize(310, vector<vector<double>>(310, vector<double>(310, -1.0)));

  ll c1 = 0, c2 = 0, c3 = 0;
  rep(i, N) {
    if (a.at(i) == 1) c1++;
    if (a.at(i) == 2) c2++;
    if (a.at(i) == 3) c3++;
  }
  cout << f(c1, c2, c3) << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  scanf("%lld", &N);
  a.resize(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &a[i]);
  }
  solve();
  return 0;
}

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

typedef long long ll;
typedef long double ld;
//#define int ll
//#define endl '\n'

// use unique(x) - removec consecutive items, returns vec.begin() + number of items
// for vector: sort(all(vec)); vec.erase(unique(all(vec)), vec.end());
// use iota(all(vec), 0) for filling a vector with 0,1,2...
// use fill(all(vec), 1) for filling a vector with 1,1,1...
// use rotate(vec.begin(), vec.begin() + 1, vec.end()) to rotate a vector | middle arg becomes first
// print number in binary -> cout << bitset<20>(n);

const int N=303,mod=1e9+7;
int n;
int cnt[4];
double dp[N][N][N],ev[N][N][N];

main(){
	cin.tie(0);cin.sync_with_stdio(0);
	cin>>n;
	for(int i=0;i<n;++i){
		int a;cin>>a;++cnt[a];
	}
	dp[cnt[1]][cnt[2]][cnt[3]]=1;
	for(int k=n;k>=0;--k)
		for(int j=n;j>=0;--j)
			for(int i=n;i>=0;--i)
				if(i||j||k){
					double pwaste=double(n-(i+j+k))/n;
					double evwaste=pwaste/(1-pwaste)+1;
					ev[i][j][k]+=evwaste*dp[i][j][k];
					if(i)dp[i-1][j][k]+=dp[i][j][k]*i/(i+j+k);
					if(j)dp[i+1][j-1][k]+=dp[i][j][k]*j/(i+j+k);
					if(k)dp[i][j+1][k-1]+=dp[i][j][k]*k/(i+j+k);
					if(i)ev[i-1][j][k]+=ev[i][j][k]*i/(i+j+k);
					if(j)ev[i+1][j-1][k]+=ev[i][j][k]*j/(i+j+k);
					if(k)ev[i][j+1][k-1]+=ev[i][j][k]*k/(i+j+k);
				}
	cout<<setprecision(10)<<fixed<<ev[0][0][0]<<endl;
}


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

void solve() {
    int h, w;
    cin >> h >> w;
    vector<vector<char>> a(h+2, vector<char>(w+2));
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            cin >> a[i][j];
        }
    }
    int di[] = {1, 0, -1, 0};
    int dj[] = {0, 1, 0, -1};
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            if(a[i][j] == '.') {
                continue;
            }
            int x = 0;
            for (int k = 0; k < 4; k++) {
                x += (a[i + di[k]][j + dj[k]] == '#');
            }
           if(!x) {
               cout << "No";
               return;
           }

        }
    }
    cout << "Yes";
}

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

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
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; }
//name[i][j]-> vvl name(i,vl(j))
//map<int,int>mp
//余りを切り上げる =>  (a + b - 1) / b
vi vx={-1,0,1,0};
vi vy={0,1,0,-1};
using pi = pair<int,int>;

signed main(){
	int a,b; cin>>a>>b;
	vvi v(a,vi(b));
	queue<pi> q;
	rep(i,0,a){
		rep(j,0,b){
			char c; cin>>c;
			if(c=='#'){
				v[i][j]=1;
				q.push(make_pair(i,j));
			}
			else v[i][j]=0;
		}
	}
	while(!q.empty()){
		pi p=q.front(); q.pop();
		int k,l; tie(k,l)=p;
		bool ok=false;
		rep(i,0,4){
			k+=vx[i]; l+=vy[i];
			if(k>=0 && k<a){
				if(l>=0 && l<b){
					if(v[k][l]==1)ok=true;
				}
			}
			k-=vx[i]; l-=vy[i];
		}
		if(!ok){
			cout<<"No"<<endl;
			return 0;
		}
	}
	cout<<"Yes"<<endl;

	return 0;
}


#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define pi std::pair<int, int>
#define mkp std::make_pair
#define ll long long
#define pb push_back
#define fi first
#define se second

//{{{IO
namespace IO{
	template <class I> void read(I &x){
		x = 0; char ch = getchar(); bool f = false;
		for(; !isdigit(ch); ch = getchar()) f |= (ch == '-');
		for(; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + ch - '0';
		return ;
	}

	int read_int(){ int x; return read(x), x;}
	ll read_ll(){ ll x; return read(x), x;}
	template <class I>
		void chmax(I &x, I y){x = x > y ? x : y;}
	template <class I>
		void chmin(I &x, I y){x = x < y ? x : y;}
#define ri read_int()
#define rl read_ll()
}
using IO::read_int;
using IO::read_ll;
using IO::chmax;
using IO::chmin;
//}}}

//{{{Mod
namespace Mod{
//	const int mod = 1e9 + 7;
//	const int mod = 1004535809;
	const int mod = 998244353;

	inline void MOD(int &x){ x -= x >= mod ? mod : 0;}
	inline void add(int &x, int y){ x = x + y >= mod ? x + y - mod : x + y;}
	inline int Add(int x, int y){ return x + y >= mod ? x + y - mod : x + y;}
	inline void mul(int &x,  int y){ x = 1ll * x * y % mod;}
	inline int Mul(int x, int y){ return 1ll * x * y % mod;}
}
//}}}

#define N 100005

int n, s, cnt;
int x[N], num[N];
ll a[N];

void solve(int l, int r){
	if(l > r) return ;
	if(l == r) return num[++cnt] = l, void();
	if(x[l] > s){
		while(r >= l) num[++cnt] = r--;
		return ;
	}
	if(x[r] < s){
		while(r >= l) num[++cnt] = l++;
		return ;
	}
	if(a[l] >= a[r]){
		num[++cnt] = r; a[l] += a[r]; solve(l, r - 1);
	}
	else{
		num[++cnt] = l; a[r] += a[l]; solve(l + 1, r);
	}
	return ;
}

int main(){
	n = ri; s = ri;
	for(int i = 1; i <= n; ++i)
		x[i] = ri, a[i] = ri;
	solve(1, n); ll res = 0;
//	for(int i = cnt; i >= 1; --i)
//		debug("%d ", num[i]);
	for(int i = cnt; i >= 1; --i)
		res += abs(x[num[i]] - s), s = x[num[i]];
	printf("%lld\n", res);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vin=vector<int>;
using vll=vector<long long>;
using vvin=vector<vector<int>>;
using vvll=vector<vector<long long>>;
using vstr=vector<string>;
using vvstr=vector<vector<string>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using vbo=vector<bool>;
using vvbo=vector<vector<bool>>;
using vpii=vector<pair<int,int>>;
using pqsin=priority_queue<int,vector<int>,greater<int>>;
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define decp(n) cout<<fixed<<setprecision((int)n)
const ll inf=1e9+7;
const ll INF=1e18;

int main(){
    int n;cin>>n;
    vector<queue<int>> tennis(n);
    int a;
    rep(i,n)rep(j,n-1){
        cin>>a;
        tennis[i].push(a-1);
    }
    vector<queue<int>> match(2);
    rep(i,n)match[0].push(i);
    int ans=0,cnt=0,tmp,res=0;;bool can;
    while(1){
        ans++;
        vbo already(n,false);
        can=false;
        while(match[res].size()){
            int i=match[res].front();match[res].pop();
            if(tennis[i].size()==0)continue;
            if(!already[i]){
                tmp=tennis[i].front();
                if(i==tennis[tmp].front()&&!already[tmp]){
                    cnt++;
                    tennis[i].pop(); tennis[tmp].pop();
                    match[(res+1)%2].push(i);match[(res+1)%2].push(tmp);
                    already[i]=true;already[tmp]=true;
                    can=true;
                }
            }
        }
        if(cnt==n*(n-1)/2){
            cout<<ans<<endl;
            return 0;
        }
        if(!can){
            cout<<-1<<endl;
            return 0;
        }
        res=(res+1)%2;
    }
}

#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef priority_queue<int> HEAP;
typedef priority_queue<int, vector<int>, greater<int> > RHEAP;

const int N = 1000010, M = 1010;

int n, m;
int h[N], e[N], ne[N], idx;
int q[N], d[N], st[N];
int g[M][M], match[M][M];

void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

int top_sort()
{
	int hh = 0, tt = -1;
	for (int i = 1; i <= m; i ++ )
		if (!d[i])
		{
			q[ ++ tt] = i;
			st[i] = 1;
		}

	while (hh <= tt)
	{
		int t = q[hh ++ ];

		for (int i = h[t]; ~i; i = ne[i])
		{
			int j = e[i];
			if ( -- d[j] == 0)
			{
				q[ ++ tt] = j;
				st[j] = st[t] + 1;
			}
		}
	}

	int res = 0;
	for (int i = 1; i <= m; i ++ )
		if (!st[i]) return -1;
		else res = max(res, st[i]);

	return res;
}

int main()
{
	memset(h, -1, sizeof h);

	scanf("%d", &n);

	for (int i = 1; i <= n; i ++ )
		for (int j = 0; j < n - 1; j ++ )
		{
			int a = i, b;
			scanf("%d", &b);
			g[i][j] = b;

			if (match[a][b]) continue;
			match[a][b] = match[b][a] = ++ m;
		}

	for (int i = 1; i <= n; i ++ )
		for (int j = 0; j < n - 2; j ++ )
		{
			int a = match[i][g[i][j]], b = match[i][g[i][j + 1]];
			d[b] ++ ;
			add(a, b);
		}

	printf("%d\n", top_sort());
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main()
{
    ll n, x;
    cin >> n >> x;
    vector<ll> b(n);
    vector<ll> l(n);
    vector<ll> u(n);
    vector<ll> score(n);
    ll ascore = 0;
    ll bsum = 0;
    for(int i = 0; i < n; i++)
    {
        cin >> b[i] >> l[i] >> u[i];
        score[i] = l[i] * b[i] + (u[i] * (x-b[i]));
        ascore += b[i] * l[i];
        bsum += b[i];
    }
    vector<int> ord(n);
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&](int lhs, int rhs)
    {
        return score[lhs] > score[rhs];
    });
    vector<ll> sum(n+1);
    for(int i = 0; i < n; i++) sum[i+1] = sum[i] + score[ord[i]];
    ll h = bsum;
    ll le = -1;
    while(h - le > 1)
    {
        ll mid = (h + le) >> 1;
        ll num = mid / x;
        ll y = mid % x;
        ll tmax = 0;
        for(int i = 0; i < n; i++)
        {
            ll tmp;
            if(num > i) tmp = sum[num+1] - score[ord[i]];
            else tmp = sum[num];
            int nn = ord[i];
            if(y > b[nn]) tmp += b[nn] * l[nn] + (y - b[nn]) * u[nn];
            else tmp += y * l[nn];
            tmax = max(tmax, tmp);
        }
        if(tmax - ascore >= 0)
        {
            h = mid;
        }
        else
        {
            le = mid;
        }
    }
    cout << h << endl;
}

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;
typedef long long ll;
//typedef pair<int,int> Pint;
typedef pair<ll, ll> P;
//typedef pair<int, pair<int, int>> P;
//typedef tuple<int,int,int> T;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define revrep(i, n) for(ll i = n-1; i >= 0; i--)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}

ll INFL = 1000000000000000010;//10^18 = 2^60
int INF = 1000000000;//10^9
ll MOD  = 1000000007;
//vector<int> dy = {0,0,1,-1};
//vector<int> dx = {1,-1,0,0};

ll N, X;
vector<tuple<ll, ll, ll, ll>> test(100010);
ll A;
vector<ll> sum(100010);

bool can(ll point){
  if(point == N * X) return 1;
  ll rest = point % X;
  ll perfect = point / X;
  rep(i, N){
    ll T = 0;
    if(perfect <= i) T += sum[perfect];
    else T += sum[perfect+1] - get<0>(test[i]);
    if(rest >= get<1>(test[i])) T += rest * get<3>(test[i]) - get<1>(test[i]) * (get<3>(test[i]) - get<2>(test[i]));
    else T += rest * get<2>(test[i]);
    if(T >= A) return 1;
  }
  return 0;
}

//　貢献度, b, l, u 0 1 2 3
int main(void){
  cin >> N >> X;
  test.resize(N);
  sum.resize(N+1);
  rep(i, N){
    cin >> get<1>(test[i]) >> get<2>(test[i]) >> get<3>(test[i]);
    get<0>(test[i]) = X * get<3>(test[i]) - get<1>(test[i]) * (get<3>(test[i]) - get<2>(test[i]));
    A += get<1>(test[i]) * get<2>(test[i]);
  }
  sort(test.begin(), test.end());
  reverse(test.begin(), test.end());
  rep(i, N) sum[i+1] = sum[i] + get<0>(test[i]);
  ll lose = -1, win = N * X;
  ll mid;
  while(win - lose > 1){
    mid = (lose + win) / 2;
    if(can(mid)){
      win = mid;
    }else{
      lose = mid;
    }
  }
  cout << win << endl;
}


/**********************************************
 * author : deepak nayan
 * file   : e:/Solutions/atcoder/abc089_a.cpp
 * time   : Thu Apr 30 18:03:36 2020
 * quest  : A - Grouping 2
 *********************************************/
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  cout << ((n < 3) ? 0 : (n / 3)) << "\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<int, int>;

int main() {
  string N;
  cin >> N;
  rep(i, 3) {
    if (N[i] == '7') {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}


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

#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}

const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
const double pi = 3.14159265358979;

vector<pair<ll, ll>> pf(ll n){
    vector<pair<ll, ll>> res;
    for (ll i = 2; i*i <= n; i++){
        if (n%i != 0){
            continue;
        }
        else{
            ll ex = 0;
            while (n%i == 0){
                ex++;
                n /= i;
            }
            res.push_back({i, ex});
        }
    }
    if (n != 1){
        res.push_back({n, 1});
    }
    return res;

    //素因数分解
    //入力:ll
    //出力:vector<pair<ll, ll>>
    //res[i].first == i番目に小さい素因数
    //res[i].second == res[i].firstの指数
}

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

    ll a,b;
    cin >> a >> b;

    ll g = gcd(a,b);

    vector<pair<ll, ll>> x = pf(g);

    cout << x.size()+1 << endl;

}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define repe(i,n) for(int i=0;i<n;i++)
#define repa(i,j,n) for(int i=j;i<n;i++)
//#define v.all() v.begin(),v.end()
#define repr(i,n) for(int i=n;i>=0;i--)
#define int long long int
#define vi vector<int>
#define vpi vector<pair<int,int>>
int gcd(int a,int b){
	if(b==0) return a;
	else return gcd(b,a%b);
}
vi primef(int n){
	vi a,ref;
	a.push_back(1);
	while(n%2==0) {
		a.push_back(2);
		n/=2;
	}
	for(int i=3;i<=sqrt(n);i+=2){
		while(n%i==0){
			a.push_back(i);

			n/=i;
		}
	}
	if(n>2) a.push_back(n);
	sort(a.begin(), a.end());
	ref.push_back(a[0]);
	for(int i=1;i<a.size();i++){
		if(a[i]!=a[i-1]) {
			ref.push_back(a[i]);
		}
	}
	return ref;
}

signed  main(){
	int t=1;
	//cin>>t;
	while(t--) {
		int ai,bi;
		cin>>ai>>bi;
		vi a,b;
		a=primef(ai);
		b=primef(bi);
		// for(int i=0;i<a.size();i++){
		// 	cout<<a[i]<<" ";
		// }
		// cout<<endl;
		// for(int i=0;i<b.size();i++){
		// 	cout<<b[i]<<" ";
		// }
		// cout<<endl;
		int ct=0;
		map<int,int> m;
		rep(i,a.size()) m[a[i]]=0;
		rep(i,b.size()) m[b[i]]=0;
		rep(i,a.size()) m[a[i]]=1;
		rep(i,b.size()){
			if(m[b[i]]==1) {ct++;}
		}
		cout<<ct<<endl;
	}
	return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

#define endl '\n'

typedef long long ll;
typedef pair<int, int> pii;
typedef tree<int, null_type,less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename T>
static T randint(T lo, T hi){
	return uniform_int_distribution<T>(lo, hi)(rng);
}

pii get(string s){
	stack<char> st;
	pii ans = {0, 0};
	for(auto &c : s){
		if(c == '(')
			st.push(c);
		else{
			if(st.empty())
				ans.first++;
			else{
				st.pop();
			}
		}
	}

	ans.second = (int)st.size();

	return ans;
}

bool balanced(string &s){
	int bal = 0;
	for(auto &c : s){
		if(c == '(')
			bal++;
		else
			bal--;
		if(bal < 0)
			return false;
	}
	return bal == 0;
}

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

	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	int n;
	cin >> n;

	vector<pii> v1, v2;

	for(int i = 0; i < n; i++){
		string s;
		cin >> s;
		pii x = get(s);

		if(x == pii(0, 0))
			continue;

		if(x.second > x.first)
			v1.push_back(x);
		else
			v2.push_back(pii(x.second, x.first));
	}

	sort(v1.begin(), v1.end());
	sort(v2.rbegin(), v2.rend());

	string s = "";

	for(auto &p : v1){
		for(int i = 0; i < p.first; i++)
			s += ")";
		for(int i = 0; i < p.second; i++)
			s += "(";
	}

	for(auto &p : v2){
		for(int i = 0; i < p.second; i++)
			s += ")";
		for(int i = 0; i < p.first; i++)
			s += "(";
	}

	if(balanced(s))
		cout << "Yes" << endl;
	else
		cout << "No" << endl;

	return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;
typedef complex<ld> com;
constexpr int inf = 1000000000;
constexpr ll INF = 1000000000000000000;
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

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

	int n;
	string s;
	cin >> n >> s;
	rep(i, 4) {
		string t = "";
		rep(j, 2) {
			if ((i >> j) & 1) t += 'S';
			else t += 'W';
		}
		for (int i = 1; i < n - 1; i++) {
			int cnt = 0;
			if (t[i - 1] == 'S') cnt ^= 1;
			if (t[i] == 'S') cnt ^= 1;
			if (s[i] == 'o') cnt ^= 1;
			if (cnt) t += 'S';
			else t += 'W';
		}
		int check = 0;
		if (t[n - 2] == 'S') check ^= 1;
		if (t[n - 1] == 'S') check ^= 1;
		if (t[0] == 'S') check ^= 1;
		if (s[n - 1] == 'o') check ^= 1;
		if (check) continue;
		if (t[n - 1] == 'S') check ^= 1;
		if (t[0] == 'S') check ^= 1;
		if (t[1] == 'S') check ^= 1;
		if (s[0] == 'o') check ^= 1;
		if (check) continue;
		cout << t << '\n';
		return 0;
	}
	cout << -1 << '\n';
}


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

int main() {
  int H, W; cin >> H >> W;
  int N = H*W;
  int A[N]; string str;
  int white_num = 0;
  for (int i = 0; i < H; i++) {
    cin >> str;
    for (int j = 0; j < W; j++) {
      white_num += A[i*W+j] = (str[j] == '.');
    }
  }
  queue<pair<int, int>> Q; Q.push(make_pair(0, 1));
  bool visited[N]; for (int i = 0; i < N; i++) visited[i] = false;
  visited[0] = true;
  int score = -1;
  while (!Q.empty()) {
    auto front = Q.front(); Q.pop();
    int u = front.first, d = front.second;
    if (u == H*W-1) { score = white_num-d; break; }
    vector<int> e;
    if (u >= W) e.push_back(u-W);
    if (u % W > 0) e.push_back(u-1);
    if (u % W < W-1) e.push_back(u+1);
    if (u / W < H-1) e.push_back(u+W);
    for (int v : e) {
      if (A[v] && !visited[v]) {
        visited[v] = true;
        Q.push(make_pair(v, d+1));
      }
    }
  }
  cout << score << endl;
}

#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = 1; i <= (int)(n); i++)
#define drep(i, n) for(int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define dup(x,y) (((x)+(y)-1)/(y))
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
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; }

// Mod int
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x;
  mint():x(0){}
  mint(ll x):x((x%mod+mod)%mod){}
  // mint(ll x):x(x){}
  mint& fix() { x = (x%mod+mod)%mod; return *this;}
  mint operator-() const { return mint(0) - *this;}
  mint& operator+=(const mint& a){ if((x+=a.x)>=mod) x-=mod; return *this;}
  mint& operator-=(const mint& a){ if((x+=mod-a.x)>=mod) x-=mod; return *this;}
  mint& operator*=(const mint& a){ (x*=a.x)%=mod; return *this;}
  mint operator+(const mint& a)const{ return mint(*this) += a;}
  mint operator-(const mint& a)const{ return mint(*this) -= a;}
  mint operator*(const mint& a)const{ return mint(*this) *= a;}
  bool operator<(const mint& a)const{ return x < a.x;}
  bool operator==(const mint& a)const{ return x == a.x;}
};
istream& operator>>(istream&i,mint&a){i>>a.x;return i;}
ostream& operator<<(ostream&o,const mint&a){o<<a.x;return o;}
//

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

int main() {
  int h, w;
  cin >> h >> w;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vvi dist(h, vi(w, INF));
  if (s[0][0] == '#') {cout<<-1<<endl; return 0;};
  vvi used(h, vi(w, 0));
  dist[0][0] = 0;
  used[0][0] = 1;
  queue<P> q;
  q.emplace(0, 0);
  while (!q.empty()) {
    int i = q.front().first, j = q.front().second;
    q.pop();
    int d = dist[i][j];
    rep(dir, 4) {
      int ni = i + di[dir], nj = j + dj[dir];
      if (ni<0||nj<0||ni>=h||nj>=w) continue;
      if (s[ni][nj] == '#') continue;
      if (used[ni][nj]) continue;
      dist[ni][nj] = d + 1;
      used[ni][nj]++;
      q.emplace(ni, nj);
    }
  }
  if (dist[h-1][w-1] == INF) {cout << -1 << endl; return 0;};
  int ans = h*w;
  rep(i, h) rep(j, w) if (s[i][j] == '#') ans--;
  ans -= dist[h-1][w-1] + 1;
  cout << ans << endl;
}


#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int, int> P;

const int INF = (int)1e9;

int board[128][128];

int main()
{
    int w, h;

    while (scanf("%d %d", &w, &h), w){
        int xs, ys;
        int xg, yg;
        scanf("%d %d", &xs, &ys);
        scanf("%d %d", &xg, &yg);

        int n;
        scanf("%d", &n);
        memset(board, 0, 128 * 128);
        while (n-- > 0){
            int c, d, x, y;
            scanf("%d %d %d %d", &c, &d, &x, &y);

            board[y][x] = board[y][x + 1] = c;
            board[y + 1][x] = board[y + 1][x + 1] = c;
            if (d == 0){
                board[y][x + 2] = board[y][x + 3] = c;
                board[y + 1][x + 2] = board[y + 1][x + 3] = c;
            }
            else {
                board[y + 2][x] = board[y + 3][x] = c;
                board[y + 2][x + 1] = board[y + 3][x + 1] = c;
            }
        }

        queue<P> que;
        bool flag = false;
        int color = board[ys][xs];

        if (!color){
            puts("NG");
            continue;
        }

        for (que.push(P(xs, ys)); que.size(); que.pop()){
            P p = que.front();
            int x = p.first;
            int y = p.second;

            if (board[y][x] != color) continue;

            if (x == xg && y == yg){
                flag = true;
                break;
            }
            board[y][x] = !color;
            que.push(P(x + 1, y));
            que.push(P(x - 1, y));
            que.push(P(x, y + 1));
            que.push(P(x, y - 1));
        }

        puts(flag ? "OK" : "NG");
    }

    return 0;
}

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

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

int board[100][100];

void init_board() {
    int i, j;
    for (i=0; i<100; i++) {
        for (j=0; j<100; j++) {
            board[i][j] = 0;
        }
    }
}
void put_block(int c, int d, int x, int y) {
    int i, j;
    int i_stop = d? 2+x: 4+x;
    int j_stop = d? 4+y: 2+y;
    for (i=x; i<i_stop; i++) {
        for (j=y; j<j_stop; j++) {
            board[i][j] = c;
        }
    }
}

string trace(int xs, int ys, int xg, int yg) {
    int x, y, nx, ny, i, c;
    queue<pair<int, int> > q;
    q.push(make_pair(xs, ys));
    c = board[xs][ys];
    if (c == 0) {
        return "NG";
    }
    board[xs][ys] = 0;
    while (q.size() > 0) {
        x = q.front().first;
        y = q.front().second;
        q.pop();
        if (x==xg && y==yg) {
            return "OK";
        }
        for (i=0; i<4; i++) {
            nx = x+dx[i];
            ny = y+dy[i];
            if (nx<0 || ny<0 || nx>=100 || ny>=100) {
                continue;
            }
            if (board[nx][ny] == c) {
                q.push(make_pair(nx, ny));
                board[nx][ny] = 0;
            }
        }
    }
    return "NG";
}

int main() {
    int w, h, xs, ys, xg, yg, n, c, d, x, y, i;
    while (true) {
        cin >> w >> h;
        if (w==0 && h==0) {
            break;
        }
        cin >> xs >> ys;
        cin >> xg >> yg;
        cin >> n;
        init_board();
        for (i=0; i<n; i++) {
            cin >> c >> d >> x >> y;
            put_block(c, d, x, y);
        }
        cout << trace(xs, ys, xg, yg) << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
    while(1){
        int n;
        cin>>n;
        if(n==0){
            return 0;
        }
        vector<int> a(n);
        for(int i=0;i<n;i++){
            cin>>a.at(i);
        }
        vector<int> b(n+1);
        b.at(0)=0;
        for(int i=1;i<=n;i++){
            b.at(i)=b.at(i-1)+a.at(i-1);
        }
        int ans=b.at(1);
        for(int i=1;i<=n;i++){
            for(int j=0;j<i;j++){
                ans=max(ans,b.at(i)-b.at(j));
            }
        }
        cout<<ans<<endl;
    }
}

#define _USE_MATH_DEFINES
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<list>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<map>
#include<queue>
#include<vector>
using namespace std;

string a[7] = {
	"Monday",
	"Tuesday",
	"Wednesday",
	"Thursday",
	"Friday",
	"Saturday",
	"Sunday"
};

int b[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };

int main() {
	for (int m, d; cin >> m >> d&&m;) {
		while (m)
			d += b[--m];
		cout << a[(d - 1 + 3) % 7] << endl;
	}
	return 0;
}


#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<queue>
#include<bitset>
#include<cstdlib>
#include<set>
#include<unordered_map>
#define l(x) (x<<1)
#define r(x) ((x<<1)|1)
#define low(x) (x&(-x))
#define IL inline
#define reg register
#define LL long long
#define N 500010
#define MOD 998244353
#define MO 1000000007
#define INF 0x3f3f3f3f
using namespace std;

int T,n,m,i,j,k,y,z,pos,l,r;
LL Ans,x;
int a[N],b[N];
char c[N];

IL LL Min(LL a,LL b){return (a<b)?a:b;}
IL LL Max(LL a,LL b){return (a>b)?a:b;}

IL int read(){
	int p=0,f=1;	char	c=getchar();
	while (c<48||c>57)	{if (c=='-')	f=-1;	c=getchar();}
	while (c>=48&&c<=57)	p=(p<<1)+(p<<3)+c-48,c=getchar();
	return p*f;
}

struct Segtree{
	struct Tree{
		int l,r;
		LL d,lz;
	}t[N*4];

	IL void Maketree(int x,int low,int high){
		int mid=(low+high)>>1;
		t[x].l=low;	t[x].r=high;
		if (low==high){t[x].d=a[low];	return;}
		Maketree(l(x),low,mid);	Maketree(r(x),mid+1,high);
		t[x].d=Min(t[l(x)].d,t[r(x)].d);
	}

	IL void Push(int x){
		LL p=t[x].lz;
		t[l(x)].d+=p;	t[r(x)].d+=p;
		t[l(x)].lz+=p;	t[r(x)].lz+=p;
		t[x].lz=0;
	}

	IL void Add(int x,int low,int high,LL val){
		reg int mid=(t[x].l+t[x].r)>>1;
		if (t[x].l==low && t[x].r==high){
			t[x].d+=val;	t[x].lz+=val;
			return;
		}
		if (t[x].lz) Push(x);
		if (high<=mid)	Add(l(x),low,high,val);
		else if (low>mid)	Add(r(x),low,high,val);
		else Add(l(x),low,mid,val),Add(r(x),mid+1,high,val);
		t[x].d=Min(t[l(x)].d,t[r(x)].d);
	}

	IL LL Query(int x,int low,int high){
		reg int mid=(t[x].l+t[x].r)>>1;
		if (t[x].l==low && t[x].r==high)	return t[x].d;
		if (t[x].lz) Push(x);
		if (high<=mid)	return Query(l(x),low,high);
		else if (low>mid)	return Query(r(x),low,high);
		else return Min(Query(l(x),low,mid),Query(r(x),mid+1,high));
	}
}T1,T2;

struct Tree{
	int l,r;
	LL d,lz;
}t[N*4];

int main(){
	#ifdef __Marvolo
	freopen("zht.in","r",stdin);
	freopen("zht.out","w",stdout);
	#endif
	n=read();	m=read();
	T1.Maketree(1,1,m);
	for (i=1;i<=m;i++)	a[i]=-i;
	T2.Maketree(1,1,m);
	for (i=1;i<=n;i++){
		l=read();	r=read();

		if (r<m){
			Ans=T2.Query(1,1,r);	x=T1.Query(1,r+1,r+1);
			if (Ans+r+1<x)	T1.Add(1,r+1,r+1,Ans+r+1-x),T2.Add(1,r+1,r+1,Ans+r+1-x);
		}

		T1.Add(1,l,r,INF);	T2.Add(1,l,r,INF);
		T1.Add(1,1,m,1);	T2.Add(1,1,m,1);

		Ans=T1.Query(1,1,m);
		if (Ans>=INF)	puts("-1");
		else printf("%lld\n",Ans);
	}
	return 0;
}

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

int main(void){
    int h, w;
    cin >> h >> w;
    map<int, int> cur2start;
    multiset<int> values;

    for (int i = 0; i < w; i++) {
        cur2start[i] = i;
        values.insert(0);
    }

    for (int i = 0; i < h; i++) {
        int left, right;
        cin >> left >> right;
        auto it = cur2start.lower_bound(left - 1);
        int right_bound_of_start = -1;
        while (it != cur2start.end() && it->first <= right) {
            right_bound_of_start = max(right_bound_of_start, it->second);
            values.erase(values.find(it->first - it->second));
            cur2start.erase(it++);
        }
        if (right_bound_of_start != -1 && right < w) {
            values.insert(right - right_bound_of_start);
            cur2start[right] = right_bound_of_start;
        }
        int ans = -1;
        if (values.size() > 0) {
            ans = *values.begin() + i + 1;
        }
        cout << ans << endl;
    }
}


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
const double PI = 3.141592653589793238;
const double EPS = 1e-10;

int main() {
	ll K;
	cin >> K;
	cout << 50 << endl;
	vector<ll> a(50);
	for (int i = 0; i < 50; i++) {
		a[i] = i + K / 50 - K % 50;
		if (i < K % 50) a[i] += 51;
	}
	for (int i = 0; i < 50; i++) cout << a[i] << " ";
	cout << endl;
 }

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

using ll=long long;

int main(){
    vector<vector<ll>> a(500,vector<ll>(500,0));
    vector<int> isPrime(5000,true);
    vector<ll> primes;
    for(int i=2;i<isPrime.size();i++){
        if(isPrime[i]){
            primes.push_back(i);
            for(int j=2;j<isPrime.size();j+=i){
                isPrime[j]=false;
            }
        }
    }
    set<ll> vs;
    swap(primes[0],primes[213]);
    swap(primes[1],primes[224]);
    swap(primes[2],primes[171]);
    for(int i=499;i>=0;i--){
        for(int j=i%2;j<500;j+=2){
            a[i][j]=primes[i/2]*primes[j/2];
            int tmp=2;
            while(vs.count(a[i][j])){
                a[i][j]=a[i][j]/(tmp-1)*tmp;
                tmp++;
            }
            vs.insert(a[i][j]);
        }
    }
    auto isRange=[](int x,int y){
        return 0<=x && x<500 && 0<=y && y<500;
    };
    auto lcm=[](ll x,ll y){
        return x/__gcd(x,y)*y;
    };
    int dx[]={0,1,0,-1};
    int dy[]={1,0,-1,0};

    for(int i=0;i<500;i++){
        for(int j=(i+1)%2;j<500;j+=2){
            ll v=1;
            for(int k=0;k<4;k++){
                if(isRange(i+dx[k],j+dy[k])){
                    v=lcm(v,a[i+dx[k]][j+dy[k]]);
                }
            }
            a[i][j]=v;
            int tmp=2;
            while(vs.count(a[i][j]+1)){
                a[i][j]=a[i][j]/(tmp-1)*tmp;
                tmp++;
            }
            a[i][j]++;
            if(a[i][j]<=1e15) vs.insert(a[i][j]);
        }
    }
    ios_base::sync_with_stdio(false);

    int n;
    cin>>n;
    int cnt=0;
    for(int i=0;i<n;i++){
        for(int j=0;j+1<n;j++){
            if(a[i][j]>1e15){
                cnt++;
                cerr<<i<<" "<<j<<endl;
                vs.insert(a[i][j]);
            }
            cout<<a[i][j]<<" ";
        }
        cout<<a[i][n-1]<<"\n";
    }
    cerr<<cnt<<endl;
    return 0;
}


#include <iostream>
using namespace std;

bool div_ok(int n) {
  if (n % 3 == 0) {
    return true;
  } else {
    return false;
  }
}

bool num_dispear(int n) {
  while (n > 0) {
    if (n % 10 == 3) {
      return true;
    }
    n /= 10;
  }
  return false;
}

int main () {
  bool loop = true;
  int i = 1;
  int n;
  cin >> n;
  bool div_check = false;
  while (i <= n) {
    if (div_ok(i)) {
      cout << " " << i;
      div_check = true;
    }
    if (!div_check && num_dispear(i)) {
      cout << " " << i;
    }
    i++;
    div_check = false;
  }
  cout << endl;
  return 0;
}

#include<bits/stdc++.h>
#include<sstream>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

void call(int n){
  for(int i=1;i<=n;i++){
    stringstream ss;
    ss<<i;
    char c[8];
    ss>>c;
    if(i%3==0||strstr(c,"3"))
      cout<<" "<<i;
  }
  cout<<endl;
}

int main(){ _;
  int n;cin>>n;
  call(n);
}


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

#define all(s) s.begin(), s.end()
#define pb push_back
#define ii pair<int, int>
#define x first
#define y second
#define bit(x, y) ((x >> y) & 1)
#define sz(s) ((int) (s).size())

const int MOD = 998244353;

int dp[305][100000];
int dp2[305][100000];
int n, a[305];

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cout.tie(0);
    cin >> n;
    int sum  = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        sum += a[i];
    }
    bool flag = (sum % 2 == 0);
    sum = (sum - 1) / 2 + 1;
    dp[0][0] = 1;
    dp2[0][0] = 1;
    int ans = 1;
    for (int i = 1; i <= n; i++) {
        ans = 1LL * ans * 3 % MOD;
        for (int j = 0; j <= 90000; j++) {
            dp2[i][j] = dp2[i - 1][j];
            dp[i][j] = dp[i - 1][j] * 2 % MOD;
            if (j >= a[i]) {
                dp[i][j] = (dp[i][j] + dp[i - 1][j - a[i]]) % MOD;
                dp2[i][j] = (dp2[i][j] + dp2[i - 1][j - a[i]]) % MOD;
            }
        }
    }
    int tmpAns = 0;
    for (int i = sum; i <= 90000; i++) {
        tmpAns = (tmpAns + dp[n][i]) % MOD;
    }
    tmpAns = 1LL * tmpAns * 3 % MOD;
    if (flag) tmpAns = (tmpAns - 3LL * dp2[n][sum]) % MOD;
    if (tmpAns < 0) tmpAns += MOD;
    ans = (ans - tmpAns) % MOD;
    if (ans < 0) ans += MOD;
    cout << ans;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
static const int64_t mod=1000000007;
int main(){
int64_t N;
  cin>>N;
  map<int64_t,int64_t>M;
  M[-1]=3;
  for(int64_t i=0;i<N;i++)
    M[i]=0;
  int64_t ans=1;
  for(int64_t i=0;i<N;i++){
  int64_t a;
    cin>>a;
    M.at(a)++;
    if(M.at(a-1)<M.at(a)){
    ans=0; break;
    }else{
    ans=((1+M.at(a-1)-M.at(a))*ans)%mod;
    }
       }cout<<ans<<endl;
   return 0;
}


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

#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (ll i = m; i < (n); i++)

using ll = long long;
using pii = pair<int, int>;
using Vi = vector<int>;

int main() {
  string s;
  cin >> s;
  cout << 2018;
  rep(i, s.size()-4){
    cout << s[i+4];
  }
}

#include <iostream>
#include <string>
int main(){
  char s[10];
  for (int i = 0; i < 10; i++){
    std::cin >> s[i];
    if (i == 3){
      s[i] = '8';
    }
  }

  for (int i = 0; i < 10; i++){
    std::cout << s[i];
  }
  std::cout << std::endl;
  return 0;
}


#include<iostream>
using namespace std;
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#define int long long int
int function2(int memo[],int turn[],int n,map<int,int> &sticks)
{
	if(n<0)
	{
		return -1000000000;
	}
	if(n==0)
	{
		return 0;
	}
	if(memo[n]!=-1)
	{
		return memo[n];
	}
	else
	{
		map<int,set<int>> mp;
		int here=-1000000000;
		mp[here].insert(-1);
		for(auto &p:sticks)
		{
			int val=1+function2(memo,turn,n-p.second,sticks);
			mp[val].insert(p.first);
		}
		auto it=mp.end();
		it--;
		int answer=*it->second.rbegin();
		turn[n]=answer;
		memo[n]=it->first;
		return it->first;
	}
}
int32_t main()
{
	int n,m;
	cin>>n>>m;
	int value[10]={0,2,5,5,4,5,6,3,7,6};
	map<int,int> sticks;
	int j;
	for(j=0;j<m;j++)
	{
		int x;
		cin>>x;
	    sticks.insert({x,value[x]});
	}
	int memo[n+1],turn[n+1];
	for(j=0;j<=n;j++)
	{
		memo[j]=-1;
		turn[j]=-1;
	}
	int val=function2(memo,turn,n,sticks);
	while(n!=0)
	{
		int here=turn[n];
		cout<<here;
		n-=value[here];
	}
	cout<<endl;
}

#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 lcm(a, b) (a)/__gcd((a),(b))*(b)
#define CEIL(a, b) (a)/(b)+(((a)%(b))?1:0)
#define endl '\n'

using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> LP;

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 = 1000000007;
static const int SIZE = 200005;

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

vector<int> Div(int n) {
    vector<int> ret;
    for(int 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;
}

const int match[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
string str[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};

string dp[10][10005];

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

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

    }
    sort(all(v), greater<int>());

    for(int i = 0; i < m; ++i) {
        int mvi = match[v[i]];
        for(int j = 1; j <= n; ++j) {
            if(i == 0) {
                if(j % mvi == 0) {
                    dp[i][j] = dp[i][j - mvi];
                    dp[i][j] += str[v[i]];

                }
            } else {
                string s1 = dp[i - 1][j];
                string s2;
                if(j % mvi == 0 || (j >= mvi && dp[i][j - mvi].size() != 0)) {
                    s2 = dp[i][j - mvi];
                    s2 += str[v[i]];

                }
                if(s1 == s2 && s1.size() == 0) {
                    continue;

                }
                if(s2.size() == 0) dp[i][j] = s1;
                else {
                    if(s1.size() < s2.size()) {
                        dp[i][j] = s2;

                    } else if(s1.size() > s2.size()){
                        dp[i][j] = s1;

                    } else {
                        if(s1 < s2) {
                            dp[i][j] = s2;

                        } else {
                            dp[i][j] = s1;

                        }
                    }
                }
            }
        }
    }
    cout << dp[m - 1][n] << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,s, n) for(int i = s; i < (int)(n); i++)
#define co(x) cout<<x<<endl
#define SQvec(i,j) vector<vector<int>> data(i, vector<int>(j))
#define mod 1000000007
typedef long long int lint;

int main() {
  string S;
  cin >> S;
  int ans=0;
  bool fin = false;
  rep(i,0,S.size() - 1){
    if(S[i] == S[i + 1]){
      cout << i + 1 << " " << i + 2 << endl;
      fin = true;
      break;
    }
  }
  if(fin == false){
    rep(i,0,S.size() - 2){
      if(S[i] == S[i + 2]){
        cout << i + 1 << " " << i + 3 << endl;
        fin = true;
        break;
      }
    }
    if(fin == false){cout << -1 << " " << -1 << endl;}
  }
}


#include <cstdio>
#include <cstring>
#include <algorithm>
const int MOD = 998244353;
const int MAXN = 5e5 + 10;

int n, m;
namespace {
  inline int add(int x) { return x >= MOD ? x - MOD : x; }
  inline int sub(int x) { return x < 0 ? x + MOD : x; }
  inline int mul(int x, int y) { return 1ll * x * y % MOD; }
  inline int pwr(int x, int y) {
    int ans = 1;
    for (; y; y >>= 1, x = mul(x, x)) {
      if (y & 1) ans = mul(ans, x);
    }
    return ans;
  }
}
int fact[MAXN << 1], invf[MAXN << 1];
int choose(int x, int y) {
  return x < y ? 0 : mul(fact[x], mul(invf[y], invf[x - y]));
}
int main() {
  scanf("%d%d", &n, &m);
  fact[0] = 1, invf[0] = 1;
  if (n < m) std::swap(n, m);
  for (int i = 1; i <= 2 * n; i++) {
    fact[i] = mul(fact[i - 1], i);
    invf[i] = pwr(fact[i], MOD - 2);
  }

  int ans = 0;
  for (int i = 1; i <= m; i++) {
    ans = add(ans + mul(choose(2 * i - 1, i), choose(n + m - 2 * i, n - i)));
  }
  printf("%d\n", add(n + mul(ans, pwr(choose(n + m, n), MOD - 2))));
}

#include <stdio.h>

using ll = long long;
const int MX = 1000005, MM = 998244353;

ll F[MX], I[MX], IF[MX];

ll C(int x,int y){return F[x+y]*IF[x]%MM*IF[y]% MM;}

int main()
{
	int N, M, i;
	I[1]=F[0]=IF[0]=F[1]=IF[1]=1;
	for(i = 2; i < MX; i++){
		I[i] = (MM - MM/i) * I[MM%i] % MM;
		F[i] = F[i-1] * i % MM;
		IF[i] = IF[i-1] * I[i] % MM;
	}

	scanf("%d%d", &N, &M);
	if( N > M ) N^=M^=N^=M;
	ll ans = M, tot = IF[N+M] * F[N] % MM * F[M] % MM;
	for(i = 1; i <= N; i++){
		ans = (ans + tot * C(N-i, M-i) % MM * C(i, i) % MM * I[2]) % MM;
	}
	printf("%lld\n", ans);
}


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

ll solve() {
    ll N;
    cin >> N;
    vector<ll> A(N);
    ll s = 0;
    for ( int i = 0; i < N; i++ ) {
        cin >> A[i];
        s += A[i];
    }
    ll m = 1000000, ix = 0;
    for ( int i = 0; i < N; i++ ) {
        ll x = abs(A[i]*N-s);
        if ( m > x ) {
            m = x;
            ix = i;
        }
    }
    return ix;
}

int main() {
    auto ans = solve();
    cout << ans << "\n";
    return 0;
}

//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

//Tariquzzaman Faisal
//_Faisal101_
//IUT_CSE'19
//190041101

#include<iostream>
#include<stdio.h>
#include<vector>
#include<deque>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<cstring>
#include<climits>
#include<cmath>
#include<iterator>
#include<set>
#define fileOut freopen("out.txt","w",stdout)
#define fileIn freopen("in.txt","r",stdout)
#define testcases ll TC,T; cin >> TC; for(T=1; T<=TC; T++)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define INFLL 9223372036854775807
#define INF 2147483647
#define pi acos(-1.0)
#define ll long long
#define endll printf("\n")
#define CASE printf("Case %lld: ",T)
#define ff first
#define ss second
#define mem(ara,n) memset(ara,n,sizeof ara)
#define ull unsigned long long
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define mid3(a,b,c) min(max(a,b),max(b,c),max(a,b))
#define max4(a,b,c,d) max(max(a,b),max(c,d))
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define endl '\n'
#define ld long double

using namespace std;

int main(){
	ll n;
	cin >> n;
	ll x;
	set <ll> st;
	while(n--){
		cin >> x;
		if(!st.count(x)){
			st.insert(x);
		}
		else{
			st.erase(st.find(x));
		}
	}
	cout<< st.size() <<endl;
	return 0;
}


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

int main(void){
  string A, B, C;
  int cnt = 0;
  cin >> A;
  cin >> B;
  cin >> C;
  if(A[A.size() - 1] == B[0])
    cnt++;
  if(B[B.size() - 1] == C[0])
    cnt++;
  if(cnt == 2) cout << "YES" << "\n";
  else cout << "NO" << "\n";
  return 0;
}

#include<iostream>
#include<string>

using namespace std;

int main(){
    string a, b, c;
    cin >> a >> b >> c;

    if(*(--a.end()) == *(b.begin()) && *(--b.end()) == *(c.begin())){
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }
}


//%std
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
	int out = 0, fh = 1;
	char jp = getchar();
	while ((jp > '9' || jp < '0') && jp != '-')
		jp = getchar();
	if (jp == '-')
		fh = -1, jp = getchar();
	while (jp >= '0' && jp <= '9')
		out = out * 10 + jp - '0', jp = getchar();
	return out * fh;
}
char s[12];
int main()
{
	scanf("%s", s);
	int n = strlen(s);
	if (n & 1)
		puts("No");
	else
	{
		for (int i = 0; i < n; i += 2)
			if (s[i] != 'h' || s[i + 1] != 'i')
			{
				puts("No");
				return 0;
			}
		puts("Yes");
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
int main() {
    ll n;
    cin >> n;
    ll a=1,b;
    for(ll i=2;i*i<=n;i++){
        if(n%i==0){
            a=i;
        }
    }
    b=n/a;
    ll ans=0;
    while(b>0){
        ans++;
        b/=10;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod= int(1e9)+7;
using P = pair<ll,ll>;
using ld=long double;

int main(){
   ll r;cin>>r;
   if(r<1200)puts("ABC");
   else if(r<2800)puts("ARC");
   else puts("AGC");
   return 0;
}

#include "iostream"
#include "string"
using namespace std;
int main(){
  string a;
  cin>>a;
  a[3]='8';
  cout<<a<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define MAX 200001
#define BMAX 100002
#define INFI 2100000000
int S[MAX];
int n;
int L[BMAX], R[BMAX];

long Merge(int left, int mid, int right){
    int n1 = mid-left;
    int n2 = right-mid;
    long cnt = 0;
    for(int i=0;i<n1;i++){
        L[i] = S[left+i];
    }
    for(int i=0;i<n2;i++){
        R[i] = S[mid+i];
    }
    L[n1] = INFI;
    R[n2] = INFI;
    int i=0,j=0;
    for(int k=0;k<n1+n2;++k){
        if(L[i]<=R[j]){
            S[left+k] = L[i];
            i++;
        }
        else{
            S[left+k] = R[j];
            cnt += (n1-i);
            j++;
        }
    }
    return cnt;
}

long countInversion(int left, int right){
    if(left+1<right){
        long v1,v2,v3;
        int mid = (left+right)/2;
        v1 = countInversion(left,mid);
        v2 = countInversion(mid,right);
        v3 = Merge(left,mid,right);
        return v1+v2+v3;
    }
    return 0;
}

int main(){
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%d",&S[i]);
    }
    long cnt = countInversion(0,n);
    printf("%ld\n",cnt);
    return 0;
}

#include <iostream>
#include <vector>
#include <iomanip>

int main(int argc, char const* argv[])
{
	int n{};
	std::cin >> n;
	while (n) {
		std::vector<int> table{};
		for (int i = 0; i < n * n; i++) {
			int d;
			std::cin >> d;
			table.push_back(d);
		}
		for (int i = 0; i < n; i++) {
			auto hsum = 0;
			for (int j = 0; j < n; j++) {
				auto current = table[i * n + j];
				hsum += current;
				std::cout << std::setw(5) << std::right << current ;
			}
			std::cout << std::setw(5) << std::right << hsum;
			std::cout << std::endl;
		}
		int sum{};
		for (int i = 0; i < n; i++) {
			auto vsum = 0;
			for (int j = 0; j < n; j++) {
				auto current = table[j * n + i];
				vsum += current;
			}
			std::cout << std::setw(5) << std::right << vsum;
			sum += vsum;
		}
		std::cout << std::setw(5) << std::right << sum;
		std::cout << std::endl;
		std::cin >> n;
	}

	return 0;
}


#include <iostream>
#include <cmath>

int main()
{
  double R;
  std::cin >> R;
  std::cout << 2.0 * M_PI * R << std::endl;
  return 0;
}

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
#include <string>

using namespace std;

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

	string s;
	if (r < 1200) {
		s = "ABC";
	} else if (r < 2800) {
		s = "ARC";
	} else {
		s = "AGC";
	}

	cout << s << endl;
	return 0;
}


#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(LL i=0;i<N;++i)
typedef long long int LL;

const LL mod = 1000000007;
LL N, X;
std::vector<LL>S;
std::vector<std::vector<LL>>memo;

LL dp(LL i, LL j)
{
	if (j == N) return i;
	if (memo[i][j] != -1) return memo[i][j];
	LL res1 = dp(i%S[j], j + 1);
	LL res2 = (dp(i, j + 1)*(N - j - 1)) % mod;
	return memo[i][j] = (res1 + res2) % mod;
}

int main()
{
	in >> N >> X;
	S.resize(N);
	rep(i, N) in >> S[i];

	memo.resize(X + 1, std::vector<LL>(N + 1, -1));
	std::sort(S.begin(), S.end(), std::greater<LL>());
	out << dp(X, 0) << std::endl;
}

#include<bits/stdc++.h>
#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
#define ms(a,b) memset(a,b,sizeof(a));
#define mp make_pair
#define pb push_back
typedef  unsigned long long ull;
typedef long long ll;
typedef long double ld;
const ll MAXN = 1e6+1;
using namespace std;
const ll mod = 1e9+7;
char _;
ll N, D, dp[10100][110][2][2]; string s;
// dp holds position, previous sum % MOD, lmt,
ll solve (ll pos,ll prevsum,  bool lmt, bool zero){
    ll ret = dp[pos][prevsum][lmt][zero];
    if (ret!= -1) return ret;
    if(pos == s.size()) return (prevsum%D==0);
    int v = s[pos] - '0', bound = lmt? v : 9; ret = 0;
    for(int i = (zero && pos == s.size()-1); i <= bound; ++i){
        ret += solve(pos+1, (prevsum+i)%D, lmt && i == v, zero && i == 0);
        ret %= mod;
    }
    return dp[pos][prevsum][lmt][zero] = ret;
}
int main()
{
    ms(dp, -1);
    cin>>s;
    cin>>D;
    cout<<solve(0,0,1,1)<<endl;
    return 0;
}


#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
template<typename T>
struct VoronoiMST{
  int n;
  using E = pair<int, T>;
  using P = pair<T, int>;
  vector<vector<E> > G;
  VoronoiMST(vector<T> &x){
    n=x.size();
    G.assign(n<<1,vector<E>());
    for(int i=0;i<n;i++) G[i].emplace_back(n+i,x[i]);
  }
  void add_edge(int u,int v,T c){
    G[u+n].emplace_back(v+n,c);
    G[v+n].emplace_back(u+n,c);
  }
  vector<vector<E> > build(){
    priority_queue<P, vector<P>, greater<P> > pq;
    const T INF = numeric_limits<T>::max();
    vector<T> dist(n<<1,INF);
    vector<int> from(n<<1,-1);
    for(int i=0;i<n;i++){
      dist[i]=0;from[i]=i;
      pq.emplace(dist[i],i);
    }
    while(!pq.empty()){
      T d;
      int v;
      tie(d,v)=pq.top();pq.pop();
      if(dist[v]<d) continue;
      for(E e:G[v]){
        int u=e.first;
        T c=e.second;
        if(dist[u]<=dist[v]+c) continue;
        dist[u]=dist[v]+c;
        from[u]=from[v];
        pq.emplace(dist[u],u);
      }
    }
    vector<vector<E> > H(n);
    for(int v=0;v<(n<<1);v++){
      for(E e:G[v]){
        int u=e.first;
        T c=e.second;
        if(from[v]==from[u]) continue;
        int x=from[v],y=from[u];
        T z=dist[v]+dist[u]+c;
        H[x].emplace_back(y,z);
        H[y].emplace_back(x,z);
      }
    }
    return H;
  }
};
//END CUT HERE
#ifndef call_from_test

#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
#endif
//BEGIN CUT HERE
struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
#endif
//BEGIN CUT HERE
template<typename T>
struct Kruskal{

  struct edge{
    int from,to;
    T cost;
    int used;
    edge(int from,int to,T cost):
      from(from),to(to),cost(cost),used(0){}
    bool operator<(const edge& e) const{
      return cost<e.cost;
    }
  };

  int n;
  vector<int> r,p;
  vector<edge> es;

  Kruskal(){}
  Kruskal(int n):n(n),r(n,1),p(n){
    iota(p.begin(),p.end(),0);
  }

  int find(int x){
    return (x==p[x]?x:p[x]=find(p[x]));
  }

  bool same(int x,int y){
    return find(x)==find(y);
  }

  void unite(int x,int y){
    x=find(x);y=find(y);
    if(x==y) return;
    if(r[x]<r[y]) swap(x,y);
    r[x]+=r[y];
    p[y]=x;
  }

  void add_edge(int u,int v,T c){
    es.emplace_back(u,v,c);
  }

  T build(){
    sort(es.begin(),es.end());
    T res=0;
    for(auto &e:es){
      if(!same(e.from,e.to)){
        res+=e.cost;
        unite(e.from,e.to);
        e.used=1;
      }
    }
    return res;
  }
};
//END CUT HERE
#ifndef call_from_test
int main(){
  return 0;
}
#endif

#undef call_from_test

//INSERT ABOVE HERE
signed CF17FINAL_J(){
  using ll = long long;
  int n;
  cin>>n;
  vector<ll> x(n);
  for(int i=0;i<n;i++) cin>>x[i];
  VoronoiMST<ll> vo(x);
  for(int i=1;i<n;i++){
    int a,b,c;
    cin>>a>>b>>c;
    a--;b--;
    vo.add_edge(a,b,c);
  }
  auto G=vo.build();
  Kruskal<ll> ks(n);
  for(int v=0;v<n;v++){
    for(auto e:G[v]){
      int u=e.first;
      ll c=e.second;
      if(v<u) ks.add_edge(v,u,c);
    }
  }
  cout<<ks.build()<<endl;
  return 0;
}
/*
  verified on 2019/01/16
  https://atcoder.jp/contests/cf17-final/tasks/cf17_final_j
*/

signed KEYENCE2019_E(){
  using ll = long long;

  int n,d;
  cin>>n>>d;
  vector<ll> a(n);
  for(int i=0;i<n;i++) cin>>a[i];
  VoronoiMST<ll> vo(a);
  for(int i=1;i<n;i++){
    vo.add_edge(i-1,i,d);
  }
  auto G=vo.build();
  Kruskal<ll> ks(n);
  for(int v=0;v<n;v++){
    for(auto e:G[v]){
      int u=e.first;
      ll c=e.second;
      if(v<u) ks.add_edge(v,u,c);
    }
  }
  cout<<ks.build()<<endl;
  return 0;
}
/*
  verified on 2019/01/16
  https://atcoder.jp/contests/keyence2019/tasks/keyence2019_e
*/

signed main(){
  //CF17FINAL_J();
  KEYENCE2019_E();
  return 0;
}
#endif

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, int>;
using E = pair<ll, pair<int, int> >;

const ll INF = 1LL << 60;

template <typename T>
struct SegmentTree {
    int n;
    vector<T> data;
    T INITIAL_VALUE;

    //使うときは、この2つを適宜変更する
    static T merge(T x, T y);
    void updateNode(int k, T x);

    SegmentTree(int size, T initial_value) {
        n = 1;
        INITIAL_VALUE = initial_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_VALUE);
    }

    T getLeaf(int k) {
        return data[k + n - 1];
    }

    void update(int k, T x) {
        k += n - 1; //葉の節点
        updateNode(k, x);
        while (k > 0) {
            k = (k - 1) / 2;
            data[k] = merge(data[k * 2 + 1], data[k * 2 + 2]);
        }
    }

    //区間[a, b)に対するクエリに答える
    //k:節点番号, [l, r):節点に対応する区間
    T query(int a, int b) {
        T vl = INITIAL_VALUE, vr = INITIAL_VALUE;
        for (int l = a + n, r = b + n; l != r; l >>= 1, r >>= 1) {
            if (l & 1) vl = merge(vl, data[l++ - 1]);
            if (r & 1) vr = merge(vr, data[--r - 1]);
        }
        return merge(vl, vr);
    }
};

//使うときは以下2つを変更
template <typename T>
T SegmentTree<T>::merge(T x, T y) {
    return min(x, y);
}

template <typename T>
void SegmentTree<T>::updateNode(int k, T x) {
    data[k] = x;
}

int n;
ll D;
ll a[200005];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> D;
    vector< vector<int> > vs(n);
    vector<int> root(n), idxs(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        vs[i].push_back(i);
        root[i] = i;
        idxs[i] = i;
    }

    SegmentTree<P> st1(n, P(INF, -1)), st2(n, P(INF, -1));
    for (int i = 0; i < n; i++) {
        st1.update(i, P(a[i] - D * i, i));
        st2.update(i, P(a[i] + D * i, i));
    }

    int max_size = 1;
    ll ans = 0;

    while (max_size < n) {
        vector< E > es;
        for (int i : idxs) {
            if (vs[i].empty()) continue;
            for (int j : vs[i]) {
                st1.update(j, P(INF, -1));
                st2.update(j, P(INF, -1));
            }

            P p(INF, -1);
            for (int j : vs[i]) {
                P q1 = st1.query(0, j);
                q1.first += a[j] + D * j;
                P q2 = st2.query(j + 1, n);
                q2.first += a[j] - D * j;
                P p2 = min(q1, q2);
                if (p > p2) p = p2;
            }

            for (int j : vs[i]) {
                st1.update(j, P(a[j] - D * j, j));
                st2.update(j, P(a[j] + D * j, j));
            }

            if (p.second == -1) continue;
            es.emplace_back(p.first, make_pair(i, p.second));
        }

        for (E& e : es) {
            int ri = root[e.second.first];
            int r = root[e.second.second];
            if (ri == r) continue;

            ans += e.first;
            if (vs[ri].size() >= vs[r].size()) {
                for (int j : vs[r]) {
                    vs[ri].push_back(j);
                    root[j] = ri;
                }
                vs[r].clear();
                max_size = max(max_size, (int)vs[ri].size());
            } else {
                for (int j : vs[ri]) {
                    vs[r].push_back(j);
                    root[j] = r;
                }
                vs[ri].clear();
                max_size = max(max_size, (int)vs[r].size());
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool comp(pair<long long int,long long int>a,pair<long long int,long long int>b)
{
    if(a.first==b.first)
    {
        if(a.second<=b.second)
        return true;
        else
        return false;
    }
    if(a.first>b.first)
    return true;
    else return false;
}
long long int gcd(long long int bdda,long long int chota)
{
    if(chota==0)
        return bdda;
    return gcd(chota,bdda%chota);
}
void solve()
{
    int a,b;
    cin>>a>>b;
    string s;
    cin>>s;
    if(s[a]!='-')
    {
        cout<<"No";return;
    }
    for(int i=0;i<a+b+1;i++)
    {
        if(i!=a && s[i]=='-')
        {
            cout<<"No";return;
        }
    }
    cout<<"Yes";
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    t=1;

    //cin>>t;
    while(t--)
    {
        solve();
    }
}

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

int main() {
    int a,b;cin>>a>>b;
    int c[3];
    c[0]=a+b;
    c[1]=a-b;
    c[2]=a*b;
    sort(c,c+3);
    cout<<c[2]<<endl;
	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
const int N = 2e5 + 5;
typedef long long ll;
using namespace std;

int n, A, B, x[N];
ll ans;

template < typename T >
inline T read()
{
	T x = 0, w = 1; char c = getchar();
	while(c < '0' || c > '9') { if(c == '-') w = -1; c = getchar(); }
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * w;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("cpp.in", "r", stdin);
#endif
	n = read <int> (), A = read <int> (), B = read <int> ();
	for(int i = 1; i <= n; i++) x[i] = read <int> ();
	for(int i = 1; i < n; i++) ans += min(1ll * B, 1ll * A * (x[i + 1] - x[i]));
	printf("%lld\n", ans);
	return 0;
}

#include <cstdint>
#include <utility>
#include <vector>

using namespace std;

size_t partition(uint32_t array[], size_t start, size_t end)
{
  int32_t mid = array[end];
  int32_t less_than = 0;
  for (uint32_t i = start; i < end; i++) {
    if (array[i] <= mid) {
      swap(array[less_than], array[i]);
      less_than++;
    }
  }
  swap(array[end], array[less_than]);
  return less_than;
}

#if !defined(TEST)
#include <iostream>
#include <vector>

static vector<uint32_t> vec;
int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    uint32_t val;
    cin >> val;
    vec.push_back(val);
  }
  size_t mid = partition(&vec[0], 0, vec.size() - 1);

  for (uint32_t i = 0; i < vec.size(); i++) {
    if (i != 0) cout << " ";
    if (i == mid) cout << "[";
    cout << vec[i];
    if (i == mid) cout << "]";
  }

  cout << endl;

  return 0;
}
#endif