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

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it =(c).begin(); it!=(c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

int main(void){
    for(int n, m, s; s = 0, cin >> n >> m, n; cout << s << endl){
        priority_queue<int> p;
        rep(i, n){
            int in; cin >> in;
            p.push(in);
        }
        COUNT(i, n){
            if(i % m) s += p.top();
            p.pop();
        }
    }

    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <iterator>

int main(){
    int n,m,x;

    while(std::cin >> n >> m){

        if(n == 0 && m == 0)
            break;

        int flag = 0;
        int num = 0;
        std::vector<int> v;
        std::string line;

        std::getline(std::cin, line);

        if(std::getline(std::cin, line)){
            std::istringstream ss(line);
            while(ss >> x){
                v.push_back(x);
            }
        }

        sort(v.rbegin(),v.rend());

        for(int y = 0;y < n;++y){
            ++flag;
            if(flag != m){
                num += v[y];
            }
            else
                flag = 0;
        }
        std::cout << num << std::endl;
    }
}


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

int main(){
  int n;
  cin >> n;
  string a,b;
  cin >> a >> b;
  int len=0;
  for(int i=0;i<n;i++){
    bool ok=true;
    for(int j=0;j<n-i;j++)
      if(a.at(i+j)!=b.at(j))
        ok=false;
    if(ok)
      len=max(n-i,len);
  }
  cout << n*2-len << endl;
  return 0;
}


#include<map>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define mp make_pair
using namespace std;

map<pair<int,int>,int> m;

struct dsu
{
    int fa[200010],rank[200010];

    void init(int n)
    {
        for(int i=1;i<=n;i++)
        {
            fa[i]=i;
        }
    }

    int find(int x)
    {
        if(fa[x]==x)
        {
            return x;
        }
        return fa[x]=find(fa[x]);
    }

    void union_(int x,int y)
    {
        int fx=find(x);
        int fy=find(y);
        if(fx==fy)
        {
            return ;
        }
        if(rank[fx]<rank[fy])
        {
            fa[fx]=fy;
        }
        else
        {
            fa[fy]=fx;
            if(rank[fx]==rank[fy])
            {
                rank[x]++;
            }
        }
    }

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

int main()
{
    //give the fucking vjudge a flying fucking
    int n,x,y;
    scanf("%d%d%d",&n,&x,&y);
    a.init(n);
    b.init(n);
    int from,to;
    for(int i=1;i<=x;i++)
    {
        scanf("%d%d",&from,&to);
        a.union_(from,to);
    }
    for(int i=1;i<=y;i++)
    {
        scanf("%d%d",&from,&to);
        b.union_(from,to);
    }
    for(int i=1;i<=n;i++)
    {
        m[mp(a.find(i),b.find(i))]++;
    }
    for(int i=1;i<=n;i++)
    {
        printf("%d ",m[mp(a.find(i),b.find(i))]);
    }
}

#include <iostream>
using namespace std;

void pri(string s,int a,int b){
  cout << s.substr(a,b-a+1) << endl;
}

string rep(string s,int a, int b, string r){
  for(int i=a; i<=b; i++){
    s[i] = r[i-a];
  }
  return s;
}

string rev(string s, int a, int b){
  string tmp = s;
  for(int i=a; i<=b; i++){
    s[i] = tmp[b+a-i];
  }
  return s;
}

int main(){

  int n;
  string s;
  cin >> s >> n;
  for(int i=0; i<n; i++){
    string ope,tar; cin >> ope;
    int a,b;
    //    cout << ope <<endl;
    if(ope == "replace"){
      cin >> a >> b >> tar;
      s = rep(s,a,b,tar);
    }
    if(ope == "print"){
      cin >> a >> b;
      pri(s,a,b);
    }
    if(ope == "reverse"){
      cin >> a >> b;
      s = rev(s,a,b);
    }
  }
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

int n;
char a[3][3];
char b[4][4];

string c[5];
int main(){
	a[0][0] = 'a'; a[0][1] = 'a'; a[0][2] = 'c';
	a[1][0] = 'b'; a[1][1] = '.'; a[1][2] = 'c';
	a[2][0] = 'b'; a[2][1] = 'd'; a[2][2] = 'd';

	b[0][0] = 'a'; b[0][1] = 'a'; b[0][2] = 'c'; b[0][3] = 'd';
	b[1][0] = 'b'; b[1][1] = 'b'; b[1][2] = 'c'; b[1][3] = 'd';
	b[2][0] = 'c'; b[2][1] = 'd'; b[2][2] = 'a'; b[2][3] = 'a';
	b[3][0] = 'c'; b[3][1] = 'd'; b[3][2] = 'b'; b[3][3] = 'b';

	c[0] = "a.eef";
	c[1] = "ab..f";
	c[2] = ".bggi";
	c[3] = "cch.i";
	c[4] = "ddhjj";

	int n;
	cin >> n;
	if(n<=2) puts("-1");
	else if(n == 7){
		cout <<
		"aa...bc" << endl <<
		"....dbc" << endl <<
		"eeffd.." << endl <<
		".abbcc." << endl <<
		".a..edd" << endl <<
		"g...e.f" << endl <<
		"g.hh..f" << endl;
	}
	else if(n == 11){
		cout <<
		"aa.......bc" << endl <<
		"........dbc" << endl <<
		"aabb....d.." << endl <<
		"c...ddee..." << endl <<
		"c......aabb" << endl <<
		".cde......." << endl <<
		".cde......." << endl <<
		"....abc...." << endl <<
		"....abc...." << endl <<
		"..dd..ee..c" << endl <<
		"....aa..bbc" << endl;

	}
	else if(n%3 == 0){
			string ans[1005];
			rep(i,n) rep(j,n) ans[i].pb('.');
			rep(i,n/3){
				int x = i*3, y = i*3;
				rep(aa,3)rep(b,3){
					ans[x+aa][y+b] = a[aa][b];
				}
			}
			rep(i,n) cout << ans[i] << endl;

	}
	else{
			string ans[1005];
			rep(i,n) rep(j,n) ans[i].pb('.');
			rep(aaa,n) rep(bbb,n){
				if(4*aaa+5*bbb == n){
					int cur = 0;
					rep(i,aaa){
						rep(aa,4)rep(bb,4) ans[cur+aa][cur+bb] = b[aa][bb];
						cur += 4;
					}
					rep(i,bbb){
						rep(aa,5)rep(bb,5) ans[cur+aa][cur+bb] = c[aa][bb];
						cur += 5;
					}
					rep(i,n) cout << ans[i] << endl;return 0;
				}
			}

	}
}

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

int main() {
  int num;
  int tmpCnt1,tmpCnt2;
  int Max1 = 0, Max2 = 0;
  string str1, str2;
  cin >> num >> str1 >> str2;

  if(str1 == str2){
    cout << num;
    return 0;
  }

  for(int i = 0; i < num; i++){
    tmpCnt1 = 0;

    if(str1.at(i) == str2.at(0)){
      for(int j = 0; j < num - i; j++){
        if(str1.at(i+j) == str2.at(j)) tmpCnt1++;
        else break;
      }
      Max1 = max(Max1, tmpCnt1);
    }
  }

  for(int i = 0; i < num; i++){
    tmpCnt2 = 0;

    if(str2.at(i) == str1.at(0)){
      for(int j = 0; j < num - i; j++){
        if(str2.at(i+j) == str1.at(j)) tmpCnt2++;
        else break;
      }
      Max2 = max(Max2, tmpCnt2);
    }
  }

  if(Max1 == 0 && Max2 == 0){
    cout << num * 2;
  }else if(Max1 >= Max2){
    cout << num + (num - Max1);
  }else{
    cout << num + (num - Max2);
  }
}


#include<cstdio>

long double sqrt(double);

int main(void){
long double d;
double x1,y1,x2,y2,D;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
D=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
sqrt(D);
d=sqrt(D);
printf("% 8.8Lf\n",d);
return 0;
}

long double sqrt(double a){
int i;
long double b=3;
for(i=0;i<256;i++){
b=(b/2)+(a/(2*b));

}
return b;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = 1e9;

int main()
{
  double x1, y1, x2, y2;
  fscanf(stdin, "%lf %lf %lf %lf", &x1, &y1, &x2, &y2);
  fprintf(stdout, "%.9f\n", hypot(x1 - x2, y1 - y2));
  return 0;
}


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

int main(){
  ll N, X;
  cin >> N >> X;

  vector<ll> A(N);
  for (int i = 0; i < N; i++){
    cin >> A.at(i);
  }
  sort(A.begin(), A.end());

  ll ans = 0;
  while (true){
    if (X >= A.at(ans)){
      X -= A.at(ans);
      ans++;
      if (ans == N){
        if (X > 0){
          ans--;
        }
        break;
      }
    }
    else{
      break;
    }
  }

  cout << ans << endl;
}

#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
#define ALL(a)  (a).begin(),(a).end()
#define REP(i,n) for(int i=0;i<(n);++i)
const double EPS = 1e-10;
const double PI  = acos(-1.0);
#define dump(x) cerr << "  (L" << __LINE__ << ") " << #x << " = " << (x) << endl;
#define dumpv(x) cerr << "  (L" << __LINE__ << ") " << #x << " = "; REP(q,(x).size()) cerr << (x)[q] << " "; cerr << endl;
template<typename T1, typename T2>
ostream& operator<<(ostream& s, const pair<T1, T2>& d) {return s << "(" << d.first << "," << d.second << ")";}

int main() {
  string s;
  while (getline(cin, s)) {
    string orig;
    for (int i = 0; i <= 'z'-'a'; i++) {
      orig = s;
      for (int j = 0; j < orig.size(); j++) {
        if (orig[j] >= 'a' && orig[j] <= 'z') {
          orig[j] = 'a' + ((orig[j]-'a') + i) % ('z'-'a'+1);
        }
      }
      if (orig.find("the") != string::npos ||
          orig.find("this") != string::npos ||
          orig.find("that") != string::npos) {
        break;
      }
    }
    cout << orig << endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n;i++)
using pii = pair<int,int>;
using piii = pair<pii,int>;
using ll = long long;
#define int ll
signed main(){
    int v,e; cin>>v>>e;
    vector<piii>edge;
    rep(i,e){
        int a,b,c; cin>>a>>b>>c;
        edge.push_back(piii(pii(a,b),c));
    }
    vector<string>ans;
    rep(s,v){
        vector<int>d(v,LLONG_MAX/1000);
        d[s]=0;
        rep(i,v){
            bool update=false;
            rep(j,e){
                int to,from,cost;
                to=edge[j].first.second;
                from=edge[j].first.first;
                cost=edge[j].second;
                if(d[from]==LLONG_MAX/1000)continue;
                if(d[to]>d[from]+cost){
                    d[to]=d[from]+cost;
                    update=true;
                }
            }

            if(i==v-1&&update==true) return puts("NEGATIVE CYCLE")*0;
        }
        string temp;
        rep(i,v){
            if(i)temp+=" ";
            if(d[i]==LLONG_MAX/1000)temp+="INF";
            else temp+=(to_string(d[i]));
        }
        ans.push_back(temp);
    }
    rep(i,v)cout<<ans[i]<<endl;
}

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pint;

int d[101][101];
int inf=INT_MAX;
int v;
int warshall_floyd(){
    rep(k,v)rep(i,v)rep(j,v){
        if(d[i][k]==inf||d[k][j]==inf)continue;
        d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    }
}
int main(){
    int e,r,s,t,ds;
    cin>>v>>e;
    rep(i,v)rep(j,v) d[i][j]=inf;
    rep(i,v) d[i][i]=0;
    rep(i,e){
        cin>>s>>t>>ds;
        d[s][t]=ds;
    }
    warshall_floyd();
    rep(i,v){
        if(d[i][i]<0){
            cout<<"NEGATIVE CYCLE"<<endl;
            break;
        }
        else if(i==v-1){
            rep(j,v){
                rep(k,v){
                    if(d[j][k]<inf) cout<<d[j][k];
                    else cout<<"INF";
                    if(k==v-1) cout<<endl;
                    else cout<<" ";
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define ref goto refrain

class dice{
	public:
	int o,u,s,e,n,w;//over,under,south,east,north,west
	void change(char a);
};
void dice::change(char a){
	int tem=o;
	if(a=='S'){
		o=n;	n=u;	u=s;	s=tem;
	}
	else if(a=='E'){
		o=w;	w=u;	u=e;	e=tem;
	}
	else if(a=='N'){
		o=s;	s=u;	u=n;	n=tem;
	}
	else if(a=='W'){
		o=e;	e=u;	u=w;	w=tem;
	}
	else{
		cout<<"Error!"<<endl;
		exit(0);
	}
}

int main(){
	dice a;
	cin>>a.o>>a.s>>a.e>>a.w>>a.n>>a.u;
	char com[100];
	cin>>com;
	int m=strlen(com);
	for(int i=0;i<m;i++){
		a.change(com[i]);
	}
	cout<<a.o<<endl;
	return 0;
}

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

const int N = 2e5+5, MOD = 998244353;
int n, m, k, p[N];

int mul(int x, int y) {
    ll tmp = 1LL * x * y;
    return tmp % MOD;
}
int add(int x, int y) {
    ll tmp = 1LL*x + 1LL*y;
    return tmp % MOD;
}
int pow(int x, int e) {
    int res = 1;
    for(; e; e>>=1) {
        if(e&1) res = mul(res, x);
        x = mul(x, x);
    }
    return res;
}
int inv(int x) {
    return pow(x, MOD-2);
}
int C(int x, int y) {
    int det = mul(p[x], p[y-x]);
    return mul(p[y], inv(det));
}
void preprocess() {
    p[0] = 1;
    for(int i=1; i<N; i++)
        p[i] = mul(p[i-1], i);
}
int main() {
    preprocess();
    cin >> n >> m >> k;
    int ans = 0;
    for(int t=n-k; t<=n; t++) {
        int tmp = mul(m, pow(m-1, t-1));
        tmp = mul(tmp, C(n-t, n-1));
        ans = add(ans, tmp);
    }
    cout << ans;
}


#include <bits/stdc++.h>
#define sz(c)      int(c.size())
#define rep(i,a,b) for (int i=a; i<(b); ++i)
#define per(i,a,b) for (int i=(b)-1; i>=(a); --i)
using namespace std;
using ll = long long;

int const MAXN=2.2e6;
int const MD=998244353;

int madd(int x,int y) {
	if (x+y<MD) return x+y; else return x+y-MD;
}
int msub(int x,int y) {
	if (x-y>=0) return x-y; else return x-y+MD;
}
int mmul(int x,int y) {
	return int(ll(x)*y % MD);
}
int mpow(int x,int p) {
	int res=1;
	while (p>0) {
		if (p%2) res=mmul(res,x);
		x=mmul(x,x);
		p/=2;
	}
	return res;
}
int minv(int x) {
	return mpow(x,MD-2);
}
int mdiv(int x,int y) {
	return mmul(x,mpow(y,MD-2));
}

struct mod_fact {
	vector<int> f,rf;
	mod_fact(int n):f(n),rf(n) {
		f[0]=rf[0]=1;
		rep(i,1,n) {
			f[i]=mmul(f[i-1],i);
			rf[i]=minv(f[i]);
		}
	}
} mfact(MAXN);

int comb(int n,int k) {
	if (n<k) return 0;
	return mmul(mfact.f[n],mmul(mfact.rf[n-k],mfact.rf[k]));
}
int distr(int n,int k) {
	return comb(n+k-1,k-1);
}

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	cout<<fixed<<setprecision(10);

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

	int res=0;
	rep(i,0,min(N,M)+1) if ((3*M-i)%2==0) {
		int add=mmul(comb(N,i),distr((3*M-i)/2,N));
		res=madd(res,add);
	}
	rep(i,0,min(N,M)+1) if ((M-i)%2==0) {
		int sub=mmul(comb(N,i),distr((M-i)/2,N));
		res=msub(res,mmul(sub,N));
	}
	rep(i,0,min(N,M)+1) if ((M-i)%2==0) {
		int add=mmul(comb(N-1,i),distr((M-i)/2,N-1));
		res=madd(res,mmul(add,N));
	}
	cout<<res<<'\n';
}

#include <bits/stdc++.h>
#include <unistd.h>

using namespace std;

#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
#define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
//const ll MOD = 1000000007;
const ll MOD = 998244353;
const double pi = 3.14159265358979323846;
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { -1,0, 1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
// #define mp make_pair
//#define endl '\n'

const int MAXN = 2522222;

ll fact[2522222];
ll rfact[2522222];

ll mod_pow(ll x, ll p, ll M = MOD) {
  ll a = 1;
  while (p) {
    if (p % 2)
      a = a*x%M;
    x = x*x%M;
    p /= 2;
  }
  return a;
}

ll mod_inverse(ll a, ll M = MOD) {
  return mod_pow(a, M - 2, M);
}

void set_fact(ll n, ll M = MOD) {
  fact[0] = fact[1] = rfact[0] = rfact[1] = 1;
  for (ll i = 2; i <= n; i++) {
    fact[i] = i * fact[i - 1] % M;
    rfact[i] = mod_inverse(fact[i], M);
  }
}

//http://drken1215.hatenablog.com/entry/2018/06/08/210000
//nが大きくfactが計算できないときのほかの計算方法について書いてある
ll nCr(ll n, ll r, ll M = MOD) {
  if (r > n) return 0;
  ll ret = fact[n];
  ret = (ret*rfact[r]) % M;
  ret = (ret*rfact[n - r]) % M;
  return ret;
}

ll nHr(ll n, ll r) {
  return nCr(n+r-1, r);
}

ll guchoku(ll n, ll m) {
  vll dp(n + 1, vl(3*m + 1));

  dp[0][0] = 1;
  rep (i, n) {
    rep (j, 3*m + 1) {
      for (int add = 0; add <= 2*m; add++) {
        if (j + add > 3*m) break;
        (dp[i + 1][j + add] += dp[i][j]) %= MOD;
      }
    }
  }
  return dp[n][3*m];

}

int main() {
  ll n, m;
  cin >> n >> m;
  set_fact(2522221);

  ll zen = nHr(3*m + 1, n - 1);

  for (int x = 0; x < m; x++) {
    ll sub = nHr(x + 1, n - 1);
    if (3*m - 2*x > n) {
      sub = 0;
    }
    else {
      sub *= nCr(n, 3*m - 2*x);
      sub %= MOD;
    }
    zen -= sub;
  }

  for (int x = 2*m + 1; x <= 3*m; x++) {
    ll sub = n;
    ll m2 = 3*m - x;
    sub = sub * nHr(m2 + 1, (n - 1) - 1) % MOD;
    (zen -= sub) %= MOD;
  }

  zen = (zen + MOD) % MOD;
  cout << zen << endl;

  //DEBUG(guchoku(n, m));
}


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

int main(void){
  while(true){
    int n;
    cin >> n;
    if(n == 0){ break; }

    vector<pair<double, string> > moonlight;
    string l;
    int p, a, b, c, d, e, f, s, m;
    for(int i = 0; i < n; i++){
      cin >> l >> p >> a >> b >> c >> d >> e >> f >> s >> m;
      int income = f * m * s - p;
      int time = a + b + c + (d + e) * m;
      moonlight.push_back(make_pair(-income/(double)time, l));
    }
    sort(moonlight.begin(), moonlight.end());
    for(int i = 0; i < n; i++){ cout << moonlight[i].second << endl; }
    cout << "#" << endl;
  }

  return 0;
}

#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<iomanip>
using namespace std;
struct Data{

  long double x;
  long double y;
};
int main(){

  int N;
  cin>>N;
  vector<Data>data(N);
  double countmax=1;
  for(double i=0;i<=N-1;i++){

    cin>>data[i].x>>data[i].y;
    countmax*=i+1;
  }
  double x;
  double x2;
  double y;
  double y2;
  vector<double>P(N);
  for(int i=0;i<=N-1;i++)P[i]=i+1;
  double ave=0;
  do{
    for(int i=0;i<=N-2;i++){

      x=data[P[i]-1].x;
      x2=data[P[i+1]-1].x;
      y=data[P[i]-1].y;
      y2=data[P[i+1]-1].y;
      ave+=sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2));
    }
  }while(next_permutation(P.begin(),P.end()));
  ave/=countmax;
  cout<< fixed << setprecision(15)<<ave;
  return 0;
}


#include <iostream>
using namespace std;
int main(void){
    string s;
    cin >> s;

    cout << "2018/";

    for (int i=0; i<s.size(); i++) {
        if (i > 4) cout << s.at(i);
    }
    cout << endl;
}

#include<iostream>
using namespace std;

int main()
{
	int n;
	int i;

	cin >> n;

	for(i=1;i<=n;i++){
		if(i%3==0)
			cout << ' ' << i;
		else if(i%10==3)
			cout << ' ' << i;
		else if((i/10)%10==3)
			cout << ' ' << i;
		else if((i/100)%10==3)
			cout << ' ' << i;
		else if((i/1000)%10==3)
			cout << ' ' << i;
	}
	cout << endl;

	return 0;
}


#include <iostream>
using namespace std;

int main()
{
    string s;
    cin>>s;
    int sum=0;
    int count=0;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='R')
        sum++;
        else
        sum=0;
        if(sum>count)
        count=sum;
    }
    cout<<count<<endl;
    return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REP(i,n) for(int (i)=0;(i)<=(int)(n);(i)++)
#define rep1(i,x,n) for(int (i)=(x);(i)<(int)(n);(i)++)
#define REP1(i,x,n) for(int (i)=(x);(i)<=(int)(n);(i)++)
#define rrep(i,x) for(int i=((int)(x)-1);i>=0;i--)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using Int = long long;

//INT_MAX = 2147483647;
//LLONG_MAX = 9223372036854775807;
//ULLONG_MAX = 18446744073709551615;
const int MOD = 1000000007;
const ll INF = numeric_limits<ll>::max();
const int inf = 1e8;
typedef pair<int,int> P;
typedef std::priority_queue<int> IntPrioQueue;
typedef std::priority_queue<int, std::vector<int>, std::greater<int> > IntReversePrioQueue;

//少数点表示
//cout << std::fixed << std::setprecision(14) << 値

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

    string s;
    cin >> s;
    int cnt;
    if(s[0]=='R' && s[1]=='R' && s[2]=='R') cnt = 3;
    else if((s[0]=='R' && s[1]=='R' ) || (s[1]=='R' && s[2]=='R')) cnt = 2;
    else if(s[0]=='R' || s[1]=='R' || s[2]=='R') cnt = 1;
    else cnt = 0;

    cout << cnt <<endl;

    return 0;

}


#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<string>
#include<utility>
#include<numeric>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;

using ll = long long;

class Stream{
public:
    Stream(){

    }
    //配列以外の入力を受け取る
    void read(){

    }
    template<typename First, typename... Rest>
    void read(First& first, Rest&... rest){
        cin >> first;
        read(rest...);
    }
    //配列を区切って出力する
    template<typename T>
    void write_vec(vector<T>& v, char divider){
        for(size_t i = 0; i < v.size(); i++){
            cout << v[i] << divider;
        }
    }
};

int main(){
    Stream io;

    string S;
    io.read(S);
    string ans = "AC";
    if(S[0] != 'A') ans = "WA";
    int c = 0;
    for(int i = 1; i < S.size(); i++){
        if(!(S[i] <= 'z' && S[i] >= 'a')){
            if(i <= S.size() - 2 && i >= 2){
                c++;
            }
            else ans = "WA";
        }
    }
    if(c != 1) ans = "WA";
    cout << ans;
    return 0;
}

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

int main(){
	string s;
	cin >> s;
	bool OK_A = false;
	bool OK_C = false;
	bool OK_lower = true;
	int cnum;

	if(s[0] == 'A') OK_A = true;
	rep(i, s.size()-1){
		if(i != 1){
			if(s[i] == 'C'){
				cnum = i;
				if(OK_C == false) OK_C = true;
				else OK_C = false;
			}
		}
	}

	rep(i, s.size()){
		if(i != 0 && i != cnum){
			if(isupper(s[i])) OK_lower = false;
		}
	}

	if(OK_A && OK_C && OK_lower) cout << "AC" << endl;
	else cout << "WA" << endl;
    return 0;
}


#include<bits/stdc++.h>

int main(){
    using namespace std;
    constexpr unsigned long MOD = 1000000007;
    auto f = [&MOD](auto& a, auto k){(a.second += (a.first += a.second * k) %= MOD) %= MOD; return a.first;};
    unsigned long N;
    cin >> N;
    vector<pair<pair<unsigned long, unsigned long>, unsigned long>> tapi(1048576, {{1, 0}, 0});
    unsigned long ans{0}, t{500000004}, a{0}, z{0};
    for(unsigned long i = 1, A; i <= N; ++i){
        cin >> A;
        if(a ^= A){
            ans += f(tapi[a].first, z - tapi[a].second);
            tapi[a].second = z;
        }else ++z, (t *= 2) %= MOD;
    }
    cout << (a ? tapi[a].first.first : (ans + t) % MOD) << endl;
    return 0;
}

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

#define int long long
#define ull unsigned long long
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define print0(a) cout << (a) << ' '
#define print1(a) cout << (a) << '\n'
#define print2(a,b) cout << (a) << ' ',print1(b)
#define print3(a,b,c) cout << (a) << ' ',print2(b,c)
#define print4(a,b,c,d) cout << (a) << ' ',print3(b,c,d)
#ifdef i_am_noob
#define debug(a) print2(#a,a)
#else
#define debug(a)
#endif
#define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>

using namespace std;
using namespace __gnu_pbds;

const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod;

inline int add(int a, int b){return (a+b>=MOD?a+b-MOD:a+b);}
inline int sub(int a, int b){return (a+MOD-b>=MOD?a-b:a+MOD-b);}
int Pow(int a, int b){
	if(!b) return 1;
	int tmp=Pow(a*a%MOD,b>>1);
	return (b&1?tmp*a%MOD:tmp);
}
inline int inverse(int n){return Pow(n,MOD-2);}

const int maxn=100005;
//i_am_noob
int n,goal,a[10][2],cnt,x,ans=4e18;
bool flag;

signed main(){
	ios_base::sync_with_stdio(0),cin.tie(0);
    cin >> n >> goal;
    rep(n) rep1(j,2) cin >> a[i][j];
    rep(pow2(n)){
        cnt=x=0;
        rep1(j,n) if(i&pow2(j)){
            x+=a[j][0];
            cnt+=a[j][0]*(j+1)*100+a[j][1];
        }
        if(cnt>=goal){
            ans=min(ans,x);
            continue;
        }
        flag=false;
        rep3(j,n-1,0) if((i&pow2(j))==0){
            rep1(k,a[j][0]){
                x++;
                cnt+=(j+1)*100;
                if(cnt>=goal){
                    ans=min(ans,x);
                    flag=true;
                    break;
                }
            }
            if(flag) break;
        }
    }
    print1(ans);
	return 0;
}


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

using namespace std;

int main() {
	int hour[3];
	cin >> hour[0] >> hour[1] >> hour[2];
	int ans = 1000;
	for (int i = 0; i < 2; i++) {
		for (int j = i + 1; j < 3; j++) {
			ans = min(ans, hour[i] + hour[j]);
		}
	}
	cout << ans << endl;
	return 0;
}

/*
    @uthor: Amit Kumar
    user -->GitHub: drviruses ; CodeChef: dr_virus_ ; Codeforces,AtCoder,Hackerearth,Hakerrank: dr_virus;
*/
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;
//#define ln mp::cpp_int;
#define ll    long long
#define ld    long double
#define ull   unsigned long long
#define endl "\n"
ll google_itr = 1;
#define google(x) cout<<"Case #"<<x<<":"
#define pi 3.14159265358979323846264338327950L

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

ll popcount(ll num){
    num = num - ((num >> 1) & 0x55555555);
    num = (num & 0x33333333) + ((num >> 2) & 0x33333333);
    return ((num + (num >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}

vector<pair<ll,ll>> factor_arr;

void myfactor(ll num){
    ll count = 0;
    while (!(num % 2)) {
        num >>= 1; count++;
    }
    if (count) factor_arr.push_back({2,count});
    for (auto i = 3; i <= sqrt(num); i += 2) {
        count = 0;
        while (num % i == 0) {
            count++;
            num /= i;
        }
        if (count) factor_arr.push_back({i,count});
    }
    if (num > 2) factor_arr.push_back({num,1});
}

void virus(){
        ll a,b,c;
        cin>>a>>b>>c;
        cout<<min({a+b,b+c,a+c});
}

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

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

    ll t=1;
    //cin>>t;
    while(t--){
           auto start = high_resolution_clock::now();
           virus();
           auto stop = high_resolution_clock::now();
           auto duration = duration_cast<seconds>(stop - start);
         //cout << "Time: "<<duration.count()<<endl;
        //your code goes here
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define f(a, b) for (ll i = a; i <= b; i++)
#define pb push_back
#define pf push_front
#define mp make_pair
using namespace std;
#define N 200005
const ll INF = 1000000000000000001;
const ll MOD = 1000000000;

//vector< vector<ll> > g(N);
//vector< pair<ll, pair< ll, ll > > > g(N);
//ll vis[N] = {0}, par[N] = {0}, child[N] = {0},cnt = 0;

void solve(){
    ll n; cin >> n;
    ll ans =0;
    ll a[n] ;
    f(0, n-1) cin >> a[i];
    f(1, n-1){
        if(a[i] < a[i-1]) ans += a[i-1]-a[i], a[i] += (a[i-1]-a[i]);
    }
    cout << ans << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    //ll t;cin >> t;for(ll index=1; index<=t; index++){
        //cout << "Case #" << index << ": " ;
        solve();
    //}
    return 0;
}

#include<bits/stdc++.h>
#define int long long int
#define mod 998244353
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int32_t main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output1.txt","w", stdout);
    #endif
    int n;
    cin>>n;
    int a[n];
    for (int i = 0; i < n; ++i)
    {
        cin>>a[i];
    }
    int ans=0,s;
    s=a[0];
    for (int i = 1; i < n; ++i)
    {
        s=max(s,a[i]);
        ans+=(s-a[i]);
    }
    cout<<ans;
    #ifndef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
    return 0;
}


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

// http://www.prefield.com/algorithm/misc/dice.html
enum FACE { TOP, BOTTOM, FRONT, BACK, LEFT, RIGHT };
class dice {
public:
	dice(int top,int front,int right,int left,int back,int bottom) {
		id[TOP] = top; id[FRONT] = front; id[RIGHT] = right;
		id[LEFT] = left; id[BACK] = back; id[BOTTOM] = bottom;
	}
	int& operator[] (FACE f) { return id[f]; }
	const int& operator[] (FACE f) const { return id[f]; }
	bool operator==(const dice &b) const {
		const dice &a = *this;
		return a[TOP] == b[TOP] && a[BOTTOM] == b[BOTTOM] &&
			a[FRONT] == b[FRONT] && a[BACK] == b[BACK] &&
			 a[LEFT] == b[LEFT] && a[RIGHT] == b[RIGHT];
	}
	void roll_north() { roll(TOP, FRONT, BOTTOM, BACK); }
	void roll_south() { roll(TOP, BACK, BOTTOM, FRONT); }
	void roll_east() { roll(TOP, LEFT, BOTTOM, RIGHT); }
	void roll_west() { roll(TOP, RIGHT, BOTTOM, LEFT); }
	void roll_right() { roll(FRONT, LEFT, BACK, RIGHT); }
	void roll_left() { roll(FRONT, RIGHT, BACK, LEFT); }
	void all_rolls(set<long long> &s){
		for (int k = 0; k < 6; (k&1?roll_east():roll_north()),++k)
			for (int i = 0; i < 4; roll_right(), ++i)
				s.insert(((long long)id[TOP]<<40) | ((long long)id[FRONT]<<32) | (id[RIGHT]<<24) | (id[LEFT]<<16) | (id[BACK]<<8) | (id[BOTTOM]<<0));
	}
private:
	void roll(FACE a, FACE b, FACE c, FACE d){
		int tmp = id[a];
		id[a] = id[b]; id[b] = id[c];
		id[c] = id[d]; id[d] = tmp;
	}
	int id[6];
};

int main(){
	int n,d[6];
	cin>>n;
	int i,j;
	set<long long>s;
	for(i=0;i<n;i++){
		for(j=0;j<6;j++)cin>>d[j];
		long long x=((long long)d[0]<<40) | ((long long)d[1]<<32) | (d[2]<<24) | (d[3]<<16) | (d[4]<<8) | (d[5]<<0);
		if(s.find(x)!=s.end()){
			cout<<"No"<<endl;
			return 0;
		}else{
			dice di(d[0],d[1],d[2],d[3],d[4],d[5]);
			di.all_rolls(s);
		}
	}
	cout<<"Yes"<<endl;
}

#include <bits/stdc++.h>

using namespace std;

inline char shift(char c, int i) {
  return char(((c + i) - 'a') % 26 + 'a');
}

int main() {
  string str;
  while (getline(cin, str)) {
    int n;
    for (int i = 0; i < 26; ++i) {
      string target;
      for (char c : str) {
        if ('a' <= c && c <= 'z')
          target += shift(c, i);
        else
          target += c;
      }
      for (string key : {"the", "this", "that"}) {
        size_t pos = target.find(key);
        if (pos != string::npos) {
          n = i;
          str = target;
          goto found;
        }
      }
    }

found:
    cout << str << endl;
  }
}


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

int main(){
	string s; cin >> s;
  	map<char,vector<int>> dist;
  	for(int i=0; i< s.size(); i++){
    	dist[s.at(i)].push_back(i);

    }
  	/*ここからアンバランス探しmapにはアルファベットと出現箇所が紐づけ
    各アルファベットについて、隣り合ってたらok
  	*/
  	bool seikai = false;
  	for(auto& a: dist ){
    	if(a.second.size()>1){
        	for(int i=0; i< a.second.size() - 1; i++ ){
            	if((a.second[i+1] - a.second[i] ==1) ||
                   (a.second[i+1] - a.second[i]==2)){
                	cout << a.second[i] +1<< " " << a.second[i+1] +1;
                  	seikai = true;
                  	goto Finish;

                }
            }
        }
    }
  	Finish:
  	if(!seikai){cout << -1 << " " << -1;}
  	cout << endl;

}

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

int main(){
	string s, strCopy;
	while (getline(cin, s)) {
		for(int j=0 ; j<26 ; j++){
			for(int i=0 ; i<s.size() ; i++){
				if(s[i]>='a' && s[i]<='z'){
					s[i] = (s[i]=='z')? 'a' : s[i] + 1;
				}
			}
			if( s.find( "the" ) != string::npos || s.find( "this" ) != string::npos || s.find( "that" ) != string::npos){
				cout << s << endl;
			}
		}
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct node{
	long long num,p;
}a[100010];
int s,n;
long long ans;
bool cmp(node a,node b)
{
	return a.p<b.p;
}
int main()
{
	scanf("%d%d",&n,&s);
	for(int i=1;i<=n;i++)
	scanf("%d%d",&a[i].p,&a[i].num);
	sort(a+1,a+n+1,cmp);
	int L=1,R=n;
	int bef=-1;

	while(a[L].p<=s && a[R].p>=s)
	{
		int now;
		if(a[L].num>=a[R].num)
		{
			now=false;
			a[L].num+=a[R].num;
		}
		else
		{
			now=true;
			a[R].num+=a[L].num;
		}
		if(now!=bef)
		{
			ans+=a[R].p-a[L].p;
			bef=now;
		}
		if(a[L].num>=a[R].num) R--;
		else L++;
	}
	if(a[L].p<s) ans+=s-a[L].p;
	else ans+=a[R].p-s;
	printf("%lld",ans);
	return 0;
}

//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#ifndef LOCAL
#define cerr if(0)cout
#endif
typedef long long ll;
const int mod=1e9+7;
//My i/o stream
struct fastio
{
	char s[100000];
	int it, len;
	fastio() {it=len=0;}
	inline char get()
	{
		if (it<len) return s[it++]; it=0;
		len=fread(s, 1, 100000, stdin);
		if (len==0) return EOF; else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while (c==' '||c=='\n') c=get();
		if (it>0) it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0; bool ng=0; char c; c=_buff.get();
	while (c!='-'&&(c<'0'||c>'9')) c=_buff.get();
	if (c=='-') ng=1, c=_buff.get();
	while (c>='0'&&c<='9') r=r*10+c-'0', c=_buff.get();
	return ng?-r:r;
}
template <class T> inline void putnum(T x)
{
	if (x<0) putchar('-'), x=-x;
	register short a[20]={}, sz=0;
	while (x) a[sz++]=x%10, x/=10;
	if(sz==0) putchar('0');
	for (int i=sz-1; i>=0; i--) putchar('0'+a[i]);
}
inline char getreal() {char c=_buff.get(); while (c<=32) c=_buff.get(); return c;}
ll qpow(ll x, ll k) {return k==0? 1: 1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
const int maxn = 100111;
int n;
ll X[maxn], P[maxn], S;
struct movement
{
	ll pos, ans;
	movement move(ll x)
	{
		movement ret;
		ret.ans = ans+abs(x-pos);
		ret.pos = x;
		return ret;
	}
};
movement solve(int l, int r, ll dl, ll dr)
{
	if (S<=X[l]) return movement({X[r], X[r]-S});
	if (X[r]<=S) return movement({X[l], S-X[l]});
	if (P[l]+dl<P[r]+dr)
	{
		return solve(l+1, r, 0, dl+dr+P[l]).move(X[l]);
	}
	else
	{
		return solve(l, r-1, dl+dr+P[r], 0).move(X[r]);
	}
}
int main()
{
	getii(n, S);
	for (int i=1; i<=n; i++) getii(X[i], P[i]);
	cout<<solve(1, n, 0, 0).ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,N) for(ll (i)=0;(i)<(N);(i)++)
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod = 1000000007;

int main() {
  int h, w;
  cin >> h >> w;
  int a, b;
  cin >> a >> b;
  int one = 1;
  int zero = 0;
  bool yes = true;
  rep(i, h) {
    if (i >= b && yes) {
      swap(zero, one);
      yes = false;
    }
    rep(j, w) {
      if(j < a) cout << one;
      else cout << zero;
    }
    cout << endl;
  }
}

#include <stdio.h>
typedef long long ll;

int main(void) {
  ll i, j, k, h, w, a, b;
  scanf("%lld%lld%lld%lld", &h, &w, &a, &b);
  for(i = 0; i < h; ++i) {
    for(j = 0; j < w; ++j) printf("%d", (i < b) ^ (j < a));
    printf("\n");
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%lld",&x)
#define ss(s)	scanf("%s",s)
#define pi(x)	printf("%d\n",x)
#define pl(x)	printf("%lld\n",x)
#define ps(s)	printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define wi(t) int t;cin>>t;while(t--)
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpii;
typedef vector<pl>		vpl;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);

const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================

vi g[N];
int a[N];

void solve()
    {
     string s;
     cin>>s;
     string ans;
      int diff=INT_MAX;
      for(int i=0;i<3;i++)
      ans+=s[i];
    diff=min(diff,abs(stoi(ans)-753));
     for(int i=3;i<s.length();i++)
     {
        ans.erase(ans.begin());
        ans.pb(s[i]);

           diff=min(diff,abs(stoi(ans)-753));

     }
     cout<<diff<<endl;

    }

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());

   // wi(t)
    {
      solve();
    }

    return 0;
}

int mpow(int base, int exp) {
  base %= mod;
  int result = 1;
  while (exp > 0) {
    if (exp & 1) result = ((ll)result * base) % mod;
    base = ((ll)base * base) % mod;
    exp >>= 1;
  }
  return result;
}

void ipgraph(int n, int m){
	int i, u, v;
	while(m--){
		cin>>u>>v;
    u--, v--;
		g[u].pb(v);
		g[v].pb(u);
	}
}

void dfs(int u, int par){
	for(int v:g[u]){
		if (v == par) continue;
		dfs(v, u);
	}
}

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

int main() {
    string s;
    cin >> s;

    int mini = 10000;
    for (int i = 0; i < s.length()-2; i++) {
        int x = atoi(s.substr(i, 3).c_str());
        mini = min(abs(x - 753), mini);
    }

    cout << mini << endl;
}


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

int main(){
  long long n;
  cin >> n;
  bool ok=false;
  long long i=1;
  while(!ok){
    if(i*i>n){
      cout << (i-1)*(i-1) << endl;
      ok=true;
    }
    i++;
  }
  return 0;
}

#include<iostream>
//#include<CastingInterop.h>
#include<string>
#include<array>
#include<math.h>
#include<sstream>
using namespace std;

int main()
{

	int num1,num3;
	cin >> num1;

		num3 = sqrt(num1);
			if (num3 % 10 != 0)
			{

				num3 = sqrt(num1);
				cout << num3 * num3;

			}
			else
			{
				cout << num3 * num3;
			}
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using LP = pair<ll, ll>;
const double PI = 3.1415926535897932;

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

	cout << T + S << endl;
	return 0;
}

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <functional>
#include <limits.h>
#include <set>
#include <map>
#include <tuple>
using namespace std;

#define ll long long
#define ull unsigned long long
#define rep(i,N) for (ll i=0;i<N;i++)
#define loop(i,N,M) for(ll i=N;i<M;i++)
#define MAX(v) *max_element(v.begin(),v.end())
#define MIN(v) *min_element(v.begin(),v.end())
#define SORTL2S(type,v) sort(v.begin(),v.end(),greater<type>())
#define SORTS2L(type,v) sort(v.begin(),v.end())
#define SORTMF(v,func) sort(v.begin(),v.end(),func)
#define MP(a,b) make_pair(a,b)

ll debugFlag = false;
template<typename T> void debug(T&);
template<typename T> void debug(T&, bool);
template<typename T> void debug(vector<T>&);
template<typename T> void debug(vector<T>&, bool);
template<typename T, typename U> void debug(pair<T, U>&);
template<typename T, typename U> void debug(pair<T, U>&, bool);

template<typename T>
void debug(T& obj) {
	debug(obj, true);
}

template<typename T>
void debug(T& obj, bool lnFlag) {
	if (!debugFlag) return;
	cout << obj;
	if (lnFlag) cout << endl;
}

template<typename T>
void debug(vector<T>& obj) {
	debug(obj, true);
}

template<typename T>
void debug(vector<T>& obj, bool lnFlag) {
	if (!debugFlag) return;
	cout << "[";
	rep(i, obj.size()) {
		if (i != 0) cout << ",";
		debug(obj[i], false);
	}
	cout << "]";
	if (lnFlag) cout << endl;
}

template<typename T, typename U>
void debug(pair<T, U>& obj) {
	debug(obj, true);
}

template<typename T, typename U>
void debug(pair<T, U>& obj, bool lnFlag) {
	if (!debugFlag) return;
	cout << "(";
	debug(obj.first, false);
	cout << ",";
	debug(obj.second, false);
	cout << ")";
	if (lnFlag) cout << endl;
}

void solve() {

	string S,T;
	cin >> S >> T;
	cout << T << S;

}

int main(int argc, char* argv[]) {
	if (argc == 2 && string(argv[1]) == "debug") debugFlag = true;
	solve();
	return 0;
}


#pragma GCC optimize ("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
#define sz(x) (int)(x).size();
#define all(x) x.begin(), x.end()
#define trav(i,a) for(auto &i:a)
const int inf=1e18;
inline int in(){int x;scanf("%lld",&x);return x;}
#define x first
#define y second
int32_t main()
{
	int n=in();
	vector<vector<pair<int,int>>> g(n);
	int u,v,w;
	for(int i=1;i<n;i++)
	{
		u=in();v=in();w=in();
		u--;v--;
		g[u].push_back({v,w});
		g[v].push_back({u,w});
	}

	int Q=in();int K=in();K--;

	queue<int> q;
	q.push(K);
	vector<int> dis(n,inf);
	dis[K]=0;
	while(!q.empty())
	{
		u=q.front();
		q.pop();
		for(auto v:g[u])
		{
			if(dis[v.x]!=inf)continue;
			dis[v.x]=min(dis[v.x],dis[u]+v.y);
			q.push(v.x);
		}
	}
	while(Q--)
	{
		u=in();v=in();
		cout<<dis[u-1]+dis[v-1]<<"\n";
	}

}

#include <iostream>
#include <iomanip>
#include<math.h>
#include<list>
#include <algorithm>
#include<set>
#include<vector>
#include<math.h>
#include<map>
#include<string>
#include <numeric>
#include <queue>
#include <sstream>
#include <bitset>
#include<stack>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;

template<typename T>
T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }

template<typename T>
map<T, T> getPrimeFactor(T n) {
	map<T, T> res;
	for (T i = 2; i * i <= n; ++i) {
		while (n % i == 0) {
			res[i] ++;
			n /= i;
		}
	}
	if (n != 1) res[n] = 1;
	return res;
}

template<typename T>
bool IsPrimeNumber(T num)
{
	if (num <= 2) return true;
	else if (num % 2 == 0) return false;

	double sqrtNum = sqrt(num);
	for (int i = 3; i <= sqrtNum; i += 2)
	{
		if (num % i == 0)
		{
			return false;
		}
	}

	return true;
}

long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}

#define rep(i,s,e) for(ll i=s;i<e;i++)
#define repeq(i,s,e) for(ll i=s;i<=e;i++)

int main() {
	ll N;
	std::cin >> N;

	vector<map<ll, ll>> dic(N+1);
	rep(i, 1, N) {
		ll a, b, c;
		std::cin >> a >> b >> c;
		dic[a][b] = c;
		dic[b][a] = c;
	}

	ll Q, K;
	std::cin >> Q >> K;

	vll cost(N+1, 999999999999999);
	cost[K] = 0;
	vector<bool> isVisited(N + 1, false);
	priority_queue<pair<ll, ll>> pq;
	pq.push({ 0,K });
	while (!pq.empty()) {
		if (!isVisited[pq.top().second]) {
			isVisited[pq.top().second] = true;
			ll c = pq.top().first;
			ll pos = pq.top().second;
			pq.pop();

			for (auto itr = dic[pos].begin(); itr != dic[pos].end(); itr++) {
				if (c + itr->second < cost[itr->first]) {
					cost[itr->first] = c + itr->second;
					pq.push({ cost[itr->first],itr->first });
				}
			}
		}
	}

	rep(i, 0, Q) {
		ll x, y;
		std::cin >> x >> y;
		std::cout << cost[x] + cost[y] << endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll, ll> Pll;
#define rep(i,n) for(int (i) = 0; (i) < (int)(n); (i)++)
#define debug(var)  do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
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; }
void Yes(){cout << "Yes" << endl;}
void No(){cout << "No" << endl;}
void YES(){cout << "YES" << endl;}
void NO(){cout << "NO" << endl;}
void err() {cout << -1 << endl;}
#define all(x) (x.begin(), x.end())

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int ddx[8] = {1,1,1,-1,-1,-1,0,0};
int ddy[8] = {0,1,-1,0,1,-1,1,-1};

int main(){
    int a, b;
    cin >> a >> b;
    if(a%b==0) cout << 0 << endl;
    else cout << 1 << endl;
}

/// In The Name Of God

#include <bits/stdc++.h>

#define f first
#define s second

#define pb push_back
#define pp pop_back
#define mp make_pair

#define sz(x) (int)x.size()
#define sqr(x) ((x) * 1ll * (x))
#define all(x) x.begin(), x.end()

#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)

#define Kazakhstan ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define nl '\n'
#define ioi exit(0);

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

const int N = (int)2000 + 7;
const int inf = (int)1e9 + 7;
const int mod = (int)998244353;
const ll linf = (ll)1e18 + 7;

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

using namespace std;

void add(int &x, int y) {
	x += y;
	if (x >= mod) x -= mod;
}
int sum(int x, int y) {
	add(x, y);
	return x;
}
int mult(int x, int y) {
	return x * 1ll * y % mod;
}

int bp(int x, int y) {
	if (x < 0) return 0;
	int res = 1;
	while (y) {
		if (y & 1) res = res * 1ll * x;
		x = x * 1ll * x % mod, y >>= 1;
	}
	return res;
}

int n, k;

int dp[N][N][2];
int calc(int ost, int prs, int f) {
	if (ost < 0) return 0;
	if (!ost) return 1;
	if (~dp[ost][prs][f]) return dp[ost][prs][f];
	int res = 0;
	if (prs) add(res, calc(ost, prs - 1, 1));
	if (f) {
		if (prs) add(res, calc(ost - 1, prs, 0) * 2ll % mod);
	} else {
		add(res, calc(ost - 1, prs, 0));
	}
	return dp[ost][prs][f] = res;
}
int dp1[N][N];
int calc1(int ost, int cnt) {
	if (ost < 0) return 0;
	if (!ost || cnt <= 0) return !ost;
	if (~dp1[ost][cnt]) return dp1[ost][cnt];
	int res = calc1(ost, cnt - 1);
	if (cnt) add(res, calc1(ost - 1, cnt));
	return dp1[ost][cnt] = res;
}
int total(int K) {
	int prs = 0;
	rep(i, 1, K) {
		prs += (K - i <= k && i < K - i);
	}
	int res = 0;
	rep(i, 0, n) {
		int cnt1 = sum(calc(i, prs, 1), calc(i - 1, prs, 1) * (K % 2 == 0 && K / 2 <= k));
		/* cout << calc(i, prs, (K % 2 == 0 && K / 2 <= n), 1) << nl;
		cout << calc1(n - i, k - 2 * prs - (K % 2 == 0 && K / 2 <= n)) << nl;
		cout << nl; */
		add(res, mult(cnt1, calc1(n - i, k - 2 * prs - (K % 2 == 0 && K / 2 <= k))));
	}
	return res;
}
int main() {
	#ifdef wws
		freopen ("in.txt", "r", stdin);
	#endif
	cin >> k >> n;
	memset(dp, -1, sizeof(dp));
	memset(dp1, -1, sizeof(dp1));
	rep(i, 2, 2 * k) {
		cout << total(i) << nl;
	}
	ioi
}


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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    cin >> s;
    int res = 0;
    if (s[0] == 'R'||s[1] == 'R'||s[2] == 'R') res = 1;
    if (s[0] == 'R' && s[1] == 'R') res = 2;
    if (s[1] == 'R' && s[2] == 'R') res = 2;
    if (s[0] == 'R' && s[1] == 'R' && s[2] == 'R') res = 3;
    cout << res << endl;
    return 0;
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define REP(i,a,b) for (int i = a; i < (b); ++i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define YY cout << "Yes" << endl;
#define NN cout << "No" << endl;
#define ve vector
const int INF = 1000000007;
const long double EPS = 1e-15;
const long double PI = acos(-1);
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a,b); }
// *max_element(all(d))
class UnionFind{
  private:
    vector<int>par;
  public:
    UnionFind(int n):par(n,-1){}
    int root(int x){
      if(par[x] < 0)return x;
      return par[x] = root(par[x]);
    }
    bool unite(int x,int y){
      x = root(x); y = root(y);
      if (x == y) return false;
      if (par[x] > par[y]) swap(x, y);
      par[x] += par[y];
      par[y] = x;
      return true;
    }
    bool same(int x,int y){
      return root(x) == root(y);
    }
    int size(int x) {
      return -par[root(x)];
    }
};
int solve(){
  int n,k,l;
  cin >> n >> k >> l;
  UnionFind uf(n),uf2(n);
  rep(i,k){
    int a,b;
    cin >> a >> b;
    a--;
    b--;
    uf.unite(a,b);
  }
  rep(i,l){
    int a,b;
    cin >> a >> b;
    a--;
    b--;
    uf2.unite(a,b);
  }
  map<pair<int,int>,int>m;
  rep(i,n){
    pair<int,int>P;
    P.first = uf.root(i);
    P.second = uf2.root(i);
    m[P]++;
  }
  rep(i,n){
    pair<int,int>P;
    P.first = uf.root(i);
    P.second = uf2.root(i);
    cout << m[P] << " ";
  }
  cout << endl;
  return 0;
}
int main(){
  // ios::sync_with_stdio(false);cin.tie(nullptr);
  cout << fixed;cout << setprecision(16);
  solve();
  return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(a) begin(a),end(a)
#define FOR(x,val,to) for(int x=(val);x<int((to));++x)
#define FORE(x,val,to) for(auto x=(val);x<=(to);++x)
#define FORR(x,arr) for(auto &x: arr)
#define FORS(x,plus,arr) for(auto x = begin(arr)+(plus); x != end(arr); ++x)
#define FORREV(x,plus,arr) for(auto x = (arr).rbegin()+(plus); x !=(arr).rend(); ++x)
#define REE(s_) {cout<<s_<<'\n';exit(0);}
#define GET(arr) for(auto &i: (arr)) sc(i)
#define whatis(x) cerr << #x << " is " << (x) << endl;
#define e1 first
#define e2 second
#define INF 0x7f7f7f7f
typedef std::pair<int,int> pi;
typedef std::vector<int> vi;
typedef std::vector<std::string> vs;
typedef int64_t ll;
typedef uint64_t ull;
#define umap unordered_map
#define uset unordered_set
using namespace std;
using namespace __gnu_pbds;

#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
template<class L, class R> ostream& operator<<(ostream &os, map<L, R> P) { for(auto const &vv: P)os<<"("<<vv.first<<","<<vv.second<<")"; return os; }
template<class T> ostream& operator<<(ostream &os, set<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) { os<<"("<<P.first<<","<<P.second<<")"; return os; }
inline int fstoi(const string &str){auto it=str.begin();bool neg=0;int num=0;if(*it=='-')neg=1;else num=*it-'0';++it;while(it<str.end()) num=num*10+(*it++-'0');if(neg)num*=-1;return num;}
inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}}
inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}}
template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); }
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound
template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 1000001

vi adj[2][N];

int vis[N];
int vit;
int wh[2][N];

void d1(int v){
    vis[v] = vit;
    wh[0][v] = vit;
    FORR(i,adj[0][v]){
        if(vis[i] != vit)
            d1(i);
    }
}

void d2(int v){
    vis[v] = vit;
    wh[1][v] = vit;
    FORR(i,adj[1][v]){
        if(vis[i] != vit)
            d2(i);
    }
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n,k,l;
    sc(n,k,l);
    int f,s;
    while(k--){
        sc(f,s);
        --f,--s;
        adj[0][f].pb(s);
        adj[0][s].pb(f);
    }
    while(l--){
        sc(f,s);
        --f,--s;
        adj[1][f].pb(s);
        adj[1][s].pb(f);
    }
    FOR(i,0,n){
        if(!vis[i]){
            ++vit;
            d1(i);
        }
    }
    memset(vis,0,n<<2);
    FOR(i,0,n){
        if(!vis[i]){
            ++vit;
            d2(i);
        }
    }
    map<pi,int> st;
    FOR(i,0,n){
        ++st[mp(wh[0][i],wh[1][i])];
    }
    FOR(i,0,n){
        cout << st[mp(wh[0][i],wh[1][i])] << ' ';
    }
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
#include <deque>
#include <stdio.h>
using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())
#define LBOUND(Itr,val) lower_bound((Itr).begin(),(Itr).end(),(val))
#define UBOUND(Itr,val) upper_bound((Itr).begin(),(Itr).end(),(val))
typedef long long ll;

int main(){

    int t; cin>>t;
    REP(kai,t){
        int out=0;
        int point=0;
        bool rui[3]={false,false,false};
        while(true){
            string s; cin>>s;
            if(s=="OUT"){
                out++;
                if(out==3)goto nextf;
            }else if(s=="HOMERUN"){
                point++;
                REP(j,3)if(rui[j])point++;
                REP(j,3)rui[j]=false;
            }else{
                if(rui[2])point++;
                rui[2]=rui[1];
                rui[1]=rui[0];
                rui[0]=true;
            }
        }
    nextf:;
        cout<<point<<endl;
    }

    return 0;
}

#include <cstdio>
#include <algorithm>
#include <functional>
#include <stack>
#include <cmath>
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 llong long long

int main() {
  while(1){
    int n;
    scanf("%d",&n);
    if(n == 0) break;
    int a[n];
    REP(i,n) scanf("%d",a+i);
    int maxseq = a[0];
    int sum = 0;
    REP(i,n) {
      sum = max(sum+a[i], a[i]);
      if(maxseq < sum) maxseq = sum;
    }
    if(maxseq < sum) maxseq = sum;
    printf("%d\n",maxseq);
  }
  return 0;
}


#include <bits/stdc++.h>
#define LL long long
#define FOR(i,a,b) for(int i=a; i<=b; i++)
#define FOD(i,a,b) for(int i=a; i>=b; i--)
#define pa pair<int,int>
#define fi first
#define se second
#define MOD xxx
#define getbit(n,i) ((n>>(i-1))&1)
#define offbit(n,i) ((n^(1<<(i-1))
#define onbit(n,i) (n|(1<<(i-1)))
#define cntone(x) (__builtin_popcount(x))
using namespace std;
const int N = 80+5;
const int P = 12800;
int m,n;
int A[N][N],B[N][N];
int F[N][N][N*N*4];
int tmp,ans(0);
int main()
{
    cin >> m >> n;
    FOR(i,1,m)
    FOR(j,1,n) cin >> A[i][j];
    FOR(i,1,m)
    FOR(j,1,n) cin >> B[i][j];
    tmp = A[1][1] - B[1][1];
    F[1][1][tmp +P] = F[1][1][-tmp +P] = 1;
    FOR(i,1,m)
    FOR(j,1,n)
    if(i+j!=2)
    {
        tmp = A[i][j] - B[i][j];
        FOR(k,0,2*P)
        if(F[i-1][j][k] || F[i][j-1][k])
        {
            F[i][j][k+tmp] = F[i][j][k-tmp] = 1;
        }
    }
    FOR(i,0,P)
    {
        if(F[m][n][i+P]==1)
        {
            ans = i;
            break;
        }
        if(F[m][n][i])
        ans = i-P;
    }
    cout << abs(ans);
	return 0;
}

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

long long n, x, cur = 0;
vector<vector<long long>> tests;

long long check(long long k){

    long long best, complete = k / x, other = k % x, cursum = 0;

    if(complete >= n)
        return cur + 1;

    for(int i = 0; i < complete; ++i)
        cursum += tests[i][0];

    best = cursum;

    for(int i = 0; i < n; ++i){
        if(i < complete){
            best = max(best, tests[complete][0] + (cursum - tests[i][0]) + tests[i][3] * other - tests[i][1] * (tests[i][3] - tests[i][2]));
            best = max(best, tests[complete][0] + (cursum - tests[i][0]) + tests[i][2] * other);
        } else {
            best = max(best, cursum + tests[i][3] * other - tests[i][1] * (tests[i][3] - tests[i][2]));
            best = max(best, cursum + tests[i][2] * other);
        }
    }
    return best;
}

int main(){

    cin.sync_with_stdio(0); cin.tie(0); cout.tie(0);

    long long b, l ,u; cin >> n >> x;

    for(int i = 0; i < n; ++i){
        cin >> b >> l >> u;
        cur += b * l;
        tests.push_back({(x - b) * u + b * l, b, l, u});
    }

    sort(tests.begin(), tests.end(), greater<vector<long long>>());

    long long mid, lef = -1, rig = n * x;
    for(;lef < rig - 1;){
        mid = lef + rig >> 1;
        if(check(mid) >= cur)
            rig = mid;
        else
            lef = mid;
    }
    cout << rig << "\n";
    return 0;
}


#include <iostream>
#include <vector>
using i64 = long long;

template <class T = long long int>
class Bit {
    private:
    unsigned len;
    T init;
    std::vector<T> arr;

    public:
    Bit(unsigned length, T initialValue = 0): len(length), init(initialValue), arr(len + 1, init) {}
    void update(unsigned a, T newval) {
        for (unsigned x = a; x <= len; x += x & -x) arr[x] += newval;
    }
    T query(unsigned a) const {
        T ret = init;
        for (unsigned x = a; x > 0; x -= x & -x) ret += arr[x];
        return ret;
    }
};

int main() {
    int n, q;
    std::cin >> n >> q;

    Bit<> b(n + 1);
    for (int i = 0; i < n; i++) {
        i64 a;
        std::cin >> a;
        b.update(i + 1, a);
    }

    while (q--) {
        int t, x, y;
        std::cin >> t >> x >> y;
        if (t) std::cout << b.query(y) - b.query(x) << '\n';
        else b.update(x + 1, y);
    }

    return 0;
}

#include<bits/stdc++.h>
#define maxn 100005
#define endl '\n'
using namespace std;
int arr[maxn],brr[maxn],pre[maxn];
struct sqrtd {
	int next,cnt;
}table[maxn];
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n;
	cin >> n;
	for(int i=1;i<=n;i++)
		cin >> arr[i];
	for(int i=1;i<n;i++)
		brr[i] = arr[i+1] - arr[i] , pre[i] = brr[i] + pre[i-1];
	n--;

	int L;
	cin >> L;

	int box = sqrt(n);
	for(int i=1;i<=n;i++) {
		int tem_cnt = 0 , bound = min((i/box+1)*box,n);
		int j , sum = 0;
		for(j=i;j<=bound;) {
			sum = 0;
			do {
				sum += brr[j++];
			} while(j <= bound && sum + brr[j] <= L);
			tem_cnt++;
		}
		if(j > bound && j <= n && sum + brr[j] <= L) {
			int l = j , r = n , m;
			while(true) {
				if(r-l<3) {
					for(m=l;m<=r && pre[m]-pre[j-1]+sum <= L;m++);
					break;
				}
				m = (l+r)>>1;
				if(pre[m]-pre[j-1]+sum <= L)
					l = m;
				else
					r = m-1;
			}
			j = m;
		}
		table[i] = {j,tem_cnt};
	}

	int q;
	cin >> q;
	while(q--) {
		int l,r;
		cin >> l >> r;
		if(l > r)	swap(l,r);
		r--;
		int ans = 0;
		while(l <= r && l/box != r/box) {
			ans += table[l].cnt;
			l = table[l].next;
		}
		if(l <= r && l/box == r/box) {
			while(l <= r) {
				int sum = 0;
				do {
					sum += brr[l++];
				} while(l <= r && sum + brr[l] <= L);
				ans++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include <string>

using namespace std;

string revstr( string str ) {
	string ret;
	for(int i = str.length() - 1; i >= 0; i--)
		ret += str[i];
	return ret;
}

int main(void){
	string str;
	cin >> str;
	cout << revstr( str ) << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ld eps = 1e-10;

int toInt(char c) {
  return c-'0';
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s; cin >> s;
  reverse(all(s));
  int n = s.size();
  vector<vector<ll>> dp(n+1, vector<ll>(2, longinf));
  dp[0][0] = 0;
  rep(i, n) {
    if(toInt(s[i]) == 9) {
      dp[i+1][0] = min({dp[i][0]+toInt(s[i])});
      dp[i+1][1] = min({dp[i][0]+10-toInt(s[i]), dp[i][1]+10-(toInt(s[i])+1), dp[i][1]+1});
    } else {
      dp[i+1][0] = min({dp[i][0]+toInt(s[i]), dp[i][1]+toInt(s[i])+1});
      dp[i+1][1] = min({dp[i][0]+10-toInt(s[i]), dp[i][1]+10-(toInt(s[i])+1)});
    }
  }
  cout << min(dp[n][0], dp[n][1]+1) << "\n";
  return 0;
}


/*
ID: computerbox --> Huseyn Hajiyev
LANG: C++
TASK: target_mode_on
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define _CRT_SECURE_NO_WARNINGS
//#include <boost/multiprecision/cpp_int.hpp>
//using boost::multiprecision::cpp_int;
#define FAST_READ ios_base::sync_with_stdio(0);
#define in freopen("input.txt", "r", stdin);
#define out freopen("output.txt", "w", stdout);
#define ll long long
#define debt(x,y)cout<<"#x = "<<(x)<<" and "<<"#y = "<<(y)<<endl;
#define deb(x)cout<<"#x = "<<(x)<<endl;
#define COUT(n, a) cout<< fixed << setprecision(a) << n<<endl
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define flsh '\n'<<flush
#define arr(a,n) for(ll i=1;i<=n;i++) cout<<a[i]<<" "; cout << "\n";
#define vecc(a,n) for(ll i=0;i<n;i++) cout<<a[i]<<" "; cout << "\n";
#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
#define DTIME(ccc) __begin = clock(); ccc; cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<endl;
#define MAX3 2010
#define MAX4 20010
#define MAX5 200010
#define MAX6 2000010

//cin.ignore (7777, '\n');

using namespace std;
using namespace __gnu_pbds;

const ll MOD1=1000000007LL;
const ll MOD2=998244353LL;

#define debug(args...)(Debugger()) , args
class Debugger
{
  public:
  Debugger(const std::string& _separator=", ") :
  first(true),separator(_separator){}
  template<typename ObjectType>
  Debugger& operator,(const ObjectType& v)
  {
        if(!first)cerr<<separator;
        cerr<<v;
        first=false;
        return *this;
  }
  ~Debugger() {cerr<<endl;}
  private:
    bool first;
    string separator;
};

template <typename T1, typename T2>
inline ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p)
{
   return os<<"("<<p.first<<", "<<p.second<<")";
}
template<typename T>
inline ostream &operator<<(ostream & os,const vector<T>& v)
{
    bool first=true;
    os<<"[";
    for(unsigned ll i=0;i<v.size();i++)
    {
        if(!first)os<<", ";
        os<<v[i];
        first=false;
    }
        return os<<"]";
}
template<typename T>
inline ostream &operator<<(ostream & os,const set<T>&v)
{
    bool first=true;
    os<<"[";
    for(typename set<T>::const_iterator ii=v.begin();ii!=v.end();++ii)
    {
        if(!first)os<<", ";
        os<<*ii;
        first=false;
    }
    return os<<"]";
}
template<typename T1, typename T2>
inline ostream &operator<<(ostream & os,const map<T1, T2>&v)
{
        bool first=true;
        os<<"[";
        for(typename map<T1,T2>::const_iterator ii=v.begin(); ii!=v.end();++ii)
        {
            if(!first)os<<", ";
            os<<*ii;
            first=false;
        }
        return os<<"]";
}

string stroka;
ll dp[200010];
ll cnt[60];

int main(){
FAST_READ;
cin>>stroka;
ll n=(ll)stroka.size();
cnt[stroka[0]-'a']++;
for(ll i=1;i<(ll)stroka.size();i++)
{
  dp[i]=dp[i-1];
  cnt[stroka[i]-'a']++;
  for(char j='a';j<='z';j++)if(stroka[i]!=j)dp[i]+=cnt[j-'a'];
}

cout<<dp[n-1]+1<<endl;

return 0;
}

//0039
//include-------------------------------------
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<stack>

using namespace std;

//typedef--------------------------------------
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

#define MP make_pair

//repetition-----------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

//constant-------------------------------------
const double PI = acos(-1.0);

//cout,cin-------------------------------------
#define COUT(x) cout<<(x)<<endl
#define CIN(x) cin>>(x)

int f(char c){
	if (c == 'I') return 1;
	else if (c == 'V') return 5;
	else if (c == 'X') return 10;
	else if (c == 'L') return 50;
	else if (c == 'C') return 100;
	else if (c == 'D') return 500;
	else if (c == 'M') return 1000;
}

int main(){
	int ans = 0;
	string st;
	while (CIN(st)){
		 int now = f(st[0]);
		 if (st.size() == 1) ans = now;
		 ans = now;
		 REP(i, st.size() - 1){
			 now >= f(st[i + 1]) ? ans += f(st[i + 1]) : ans += f(st[i + 1]) - now - now;
			 now = f(st[i + 1]);
		 }
		 COUT(ans);
	}

	return 0;

}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>
#define ll long long
#define itn int
#define endl '\n'
#define co(ans) cout<<ans<<endl
#define COYE cout<<"YES"<<endl
#define COYe cout<<"Yes"<<endl
#define COye cout<<"yes"<<endl
#define CONO cout<<"NO"<<endl
#define CONo cout<<"No"<<endl
#define COno cout<<"no"<<endl
#define FORE(i,a) for(auto &i:a)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RREP(i,n)  FFOR(i,1,n)
#define PB push_back
#define MP make_pair
#define ALL(V) (V).begin(),(V).end()
#define SORT(V) sort((V).begin(),(V).end())
#define REVERSE(V) reverse((V).begin(),(V).end())
#define EACH(V,i) for(typeof((V).begin()) i=(V).begin();i!=(V).end();++i)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define INF ((1LL<<62)-(1LL<<31))
#define EPS 1e-10
#define PI 3.141592653589793238
#define MOD 1000000007
#define MAX 5100000
using namespace std;
using Graph=vector<vector<int>>;
inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T>inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}
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;}
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
const int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int main(){
  ll N,ans=0;
  cin>>N;
  ll A[N],B[N];
  // A[i] S[i]の最小値
  // B[i] S[i]の値

  REP(i,N){
    A[i]=0;
    B[i]=0;
  }

  string S[N];
  REP(i,N) cin>>S[i];

  REP(i,N){
    ll cnt=0;
    REP(j,S[i].size()){
      if(S[i].at(j)=='(') cnt++;
      else cnt--;
      chmin(A[i],cnt);
    }
    B[i]=cnt;
  }

  vector<pair<int,int>> L,LL,LLL;
  bool CAN=true;

  REP(i,N){
    if(B[i]>0){
      if(A[i]>=0ll) L.PB(MP(A[i],B[i]));
      else LL.PB(MP(A[i],B[i]));
    }
    else LLL.PB(MP(B[i]-A[i],A[i]));
  }

  SORT(L);
  REVERSE(L);
  SORT(LL);
  REVERSE(LL);
  SORT(LLL);
  REVERSE(LLL);

  REP(i,L.size()){
    if(ans+L[i].first<0) CAN=false;
    ans+=L[i].second;
  }

  REP(i,LL.size()){
    if(ans+LL[i].first<0) CAN=false;
    ans+=LL[i].second;
  }

  REP(i,LLL.size()){
    if(ans+LLL[i].second<0) CAN=false;
    ans+=LLL[i].first+LLL[i].second;
  }

  if(ans!=0) CAN=false;
  if(CAN) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
  return 0;
}

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

bool check(vector<P> s){
	int h = 0;
	for(P p : s){
		int b = h + p.first;
		if(b < 0) return false;
		h += p.second;
	}
	return true;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vector<P> ls, rs;
	int total = 0;
	rep(i,n){
		string s;
		cin >> s;
		int h = 0, b = 0;
		for(char c : s){
			if(c == '(') ++h;
			else --h;
			chmin(b, h);
		}
		if(h > 0) ls.emplace_back(b, h);
		else rs.emplace_back(b-h, -h);
		total += h;
	}
	sort(ls.rbegin(), ls.rend());
	sort(rs.rbegin(), rs.rend());
	if(check(ls) && check(rs) && total == 0) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}


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

#define rep2(x,from,to) for(int x=(from);(x)<(to);(x)++)
#define rep(x,to) rep2(x,0,to)
int main() {
	int n;
	while(1) {
		if(scanf("%d", &n) == EOF) break;
		int total = 0;
		rep2(i,1,(600/n)) {
			total += n * n * i * n * i;
		}
		cout << total << endl;
	}
	return 0;
}

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

int main(int argc, char const *argv[]) {
    map<string, int> day;
    day["SUN"] = 7;
    day["MON"] = 6;
    day["TUE"] = 5;
    day["WED"] = 4;
    day["THU"] = 3;
    day["FRI"] = 2;
    day["SAT"] = 1;

    string s;
    cin >> s;
    cout << day[s] << endl;
    return 0;
}


#include<iostream>
#define INF 100000000000000
#define MAXN 1000+10
using namespace std;
long long dist[MAXN][MAXN];
int main(){
	int i,j,k,n,m,a,b;

	cin>>n>>m;
	for (i = 0;i < n;++i){
		for (j = 0;j < n;++j){
			if (i != j) {
				dist[i][j]=INF;
			}
		}
	}

	for (i = 0;i < m;++i){
		cin>>a>>b;
		cin>>dist[a][b];
	}

	for (k = 0;k < n;++k){
		for (i = 0;i < n;++i){
			for (j = 0;j < n;++j){
				if (dist[i][k] != INF && dist[k][j] != INF)dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
			}
		}
	}

	for (i = 0;i < n;++i){
		if (dist[i][i] < 0) {
			cout<<"NEGATIVE CYCLE"<<endl;
			return 0;
		}
	}

	for (i = 0;i < n;++i){
		for (j = 0;j < n;++j){
			if (j) cout<<" ";
			if (dist[i][j] == INF) cout<<"INF";
			else cout<<dist[i][j];
		}
		cout<<endl;
	}

	return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>

using namespace std;

template <class T>
using gp_queue=priority_queue<T, deque<T>, less<T>>;

using Weight=long long;
static const Weight INF=1LL<<57;

struct Edge {
    size_t src, dst;
    Weight weight;
    Edge() {}
    Edge(size_t src, size_t dst, Weight weight=1):
        src(src), dst(dst), weight(weight)
    {}
};

bool operator<(const Edge &e, const Edge &f) {
    if (e.weight != f.weight) {
        return e.weight > f.weight;
    } else {
        return e.src!=f.src? e.src<f.src : e.dst<f.dst;
    }
}

using Edges=vector<Edge>;
using Vertex=vector<Edge>;
using Graph=vector<Vertex>;

void join(Graph &g, size_t s, size_t d, Weight w=1) {
    // non-directed
    g[s].push_back(Edge(s, d, w));
    g[d].push_back(Edge(d, s, w));
}

void connect(Graph &g, size_t s, size_t d, Weight w=1) {
    // directed
    g[s].push_back(Edge(s, d, w));
}

vector<vector<Weight>> shortest_distance(Graph g) {
    /* Johnson */
    size_t V=g.size();
    vector<Weight> h(V);
    for (size_t k=0; k<V; ++k)
        for (size_t i=0; i<V; ++i)
            for (const Edge &e: g[i])
                if (h[e.dst] > h[e.src] + e.weight) {
                    h[e.dst] = h[e.src] + e.weight;
                    if (k == V-1)
                        return vector<vector<Weight>>();
                }

    for (size_t i=0; i<V; ++i)
        for (Edge &e: g[i])
            e.weight += h[e.src] - h[e.dst];

    vector<vector<Weight>> d(V, vector<Weight>(V, INF<<1));
    vector<vector<size_t>> prev(V, vector<size_t>(V, -1));
    for (size_t s=0; s<V; ++s) {
        gp_queue<Edge> q; q.push(Edge(s, s, 0));
        d[s][s] = 0;
        while (!q.empty()) {
            Edge e=q.top(); q.pop();
            if (~prev[s][e.dst]) continue;

            prev[s][e.dst] = e.src;
            for (const Edge &f: g[e.dst]) {
                if (d[s][f.dst] > e.weight + f.weight) {
                    d[s][f.dst] = e.weight + f.weight;
                    q.push(Edge(f.src, f.dst, e.weight+f.weight));
                }
            }
        }
        for (size_t u=0; u<V; ++u)
            d[s][u] += h[u] - h[s];
    }

    return d;
}

int main() {
    size_t V, E;
    scanf("%zu %zu", &V, &E);

    Graph g(V);
    for (size_t i=0; i<E; ++i) {
        size_t s, t;
        Weight d;
        scanf("%zu %zu %lld", &s, &t, &d);

        connect(g, s, t, d);
    }

    vector<vector<Weight>> d=shortest_distance(g);
    if (d.empty())
        return !printf("NEGATIVE CYCLE\n");

    for (vector<Weight> w: d)
        for (size_t i=0; i<V; ++i)
            if (w[i] < INF) {
                printf("%lld%c", w[i], i<V-1? ' ':'\n');
            } else {
                printf("INF%c", i<V-1? ' ':'\n');
            }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define pb push_back
using ll = long long;
using vin=vector<int>;
using vll=vector<ll>;
using vst=vector<string>;
using P = pair<int, int>;
const int inf=1e9+7;
const ll INF=1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
const long double pi=acos(-1);

int main(){cout<<fixed<<setprecision(10);
		   string s;
           cin>>s;
           ll n=s.size();
           //abjka 両端が同じなら数えない
           ll ans=1;
           map<char,ll> cnt;
           rep(i,0,n)cnt[s[i]]++;
           ans+=n*(n-1)/2;
           ll c;
           for(auto v:cnt){
              c=v.second;
            ans-=c*(c-1)/2;
           }
           cout<<ans<<endl;
}

#include <bits/stdc++.h>
#define REP(i, e) for(int (i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\n"
using namespace std;
using ll = long long; using pint = pair<int, int>; using pll = pair<ll, ll>;
const long long MOD = 1000000007;

ll N;
bool prime[55556];
vector<ll> v;

signed main(){
    cin >> N;
    for(ll i = 2; i <= 55555; i++) prime[i] = true;
    for(ll i = 2; i <= 55555; i++){
        if(prime[i] == false) continue;
        if(i % 5 == 1) v.push_back(i);
        for(ll j = 2; i * j <= 55555; j++) prime[i * j] = false;
    }
    REP(i, N){
        cout << v[i];
        if(i == N - 1) cout << endl;
        else cout << " ";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
int INF = 1e9+7;
int mod = 1234567;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int cnt[200005];
signed main() {
    int N;
    cin >> N;
    vector<int>a(N);
    for(int i = 0; i < N; i++) {
        cin >> a[i];
    }
    vector<int>ans;
    for(int i = N; i >= 1; i--) {
        if(cnt[i]%2 == a[i-1]) {
            continue;
        }
        ans.push_back(i);
        for(int j = 1; j*j <= i; j++) {
            if(i%j == 0) {
                if(j*j == i) {
                    cnt[j]++;
                }
                else {
                    cnt[j]++;
                    cnt[i/j]++;
                }
            }
        }
    }
    cout << ans.size() << endl;
    for(int i = 0; i < ans.size(); i++) {
        if(i) {
            cout << " ";
        }
        cout << ans[i];
    }
}

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

typedef long long int ll;
#define rep(i,n) for (int i = 0; i < (n); i++)
const ll mod = 1000000007;

int main() {
    int n;
    cin >> n;
    int a[n+1];
    rep(i,n) cin >> a[i+1];
    int ans[n+1];
    rep(i,n) ans[i+1] = 0;
    //rep(i,n) cout << a[i] << endl;
    for(int i = n; i >= 1; --i) {
        //printf("OK\n");
        for(int j = i; j <= n; j += i) {
            ans[i] = (ans[i]+ans[j])%2;
        }
        ans[i] = (ans[i]+a[i])%2;
        //printf("OK2\n");
    }
    //rep(i,n) cout << ans[i+1] << endl;
    int cnt = 0;
    rep(i,n) {
        if(ans[i+1]) cnt++;
    }
    if (cnt) {
        cout << cnt << endl;
        rep(i,n) {
            if (ans[i+1]) {
                printf("%d\n", i+1);
            }
        }
    } else cout << 0 << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int e, m;

int main(){
	while (cin >> e&&e){
		int m = 1000001;
		for (int i = 0; i <= 100; i++){
			for (int j = 0; j <= 1000; j++){
				int x = e - i*i*i - j*j;
				if (x >= 0){
					m = min(m, x + i + j);
				}
			}
		}
		cout << m << endl;
	}
}

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
int N;
int A[200000];
int main(){
  int i;
  long long res = 0;
  long long mx = -1;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  for(i=(0);i<(N);i++){
    res +=max_L(mx - A[i], 0);
    chmax(mx, A[i]);
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200911-1

// --- original code ---
// int N, A[2d5];
// {
//   ll res = 0, mx = -1;
//   rd(N,A(N));
//   rep(i,N){
//     res += max(mx - A[i], 0);
//     mx >?= A[i];
//   }
//   wt(res);
// }


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

int main() {
  string s;
  cin >> s;
  string ans = s.substr(0, s.size()-8);
  cout << ans << endl;
  return 0;
}

/*
このコード、と～おれ!
Be accepted!
∧＿∧
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
					*/

#include <stdio.h>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>

#pragma GCC optimize("Ofast")
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define rep1(i, n) for(int i = 1; i <= (n); ++i)
#define rep2(i, n) for(int i = 2; i < (n); ++i)
#define repr(i, n) for(int i = n; i >= 0; --i)
#define reprm(i, n) for(int i = n - 1; i >= 0; --i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printin(a) printf(a ? "possible\n" : "inposible\n")
#define printdb(a) printf("%.50lf\n", a)//少数出力
#define printdbd(a) printf("%.16lf\n", a)//少数出力(桁少なめ)
#define prints(s) printf("%s\n", s.c_str())//string出力
#define all(x) (x).begin(), (x).end()
#define allsum(a, b, c) ((a + b) * c / 2)//等差数列の和、初項,末項,項数
#define pb push_back
#define priq priority_queue
#define rpriq priq<int, vector<int>, greater<int>>
#define deg_to_rad(deg) (((deg)/360.0)*2.0*PI)
#define rad_to_deg(rad) (((rad)/2.0/PI)*360.0)
#define Please return
#define AC 0
#define ushi int
#define tapu main
#define nichia (
#define kunn void
#define wara )

using ll = long long;

const int INF = 1073741823;
const int MINF = -1073741823;
const ll LINF = ll(4661686018427387903);
const ll MOD = 1000000007;
const double PI = acos(-1);

using namespace std;

void scans(string& str) {
	char c;
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);//最初の改行対策
	while (c != '\n' && c != -1) {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

/*-----------------------------------------ここからコード-----------------------------------------*/

int main(){

	string s;
	scans(s);
	prints(s.substr(0, s.length() - 8));
	Please AC;
}


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

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

    int h;
    ll ans = 0;
    cin >> h;
    for(int i=0;i<n-1;i++){
        int nh;
        cin >> nh;
        if(nh>=h) h = nh;
        else ans += (h-nh);
    }
    cout << ans << endl;
    return 0;
}

//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("O3")
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdlib>
#include<set>
#include<vector>
#include<sstream>
#include<queue>
#include <iomanip>
#include<unordered_set>
#include <stack>
#include <unordered_map>
#include <map>
#include<numeric>
#include<chrono>
#include <fstream>
constexpr auto INF = 9223372036854775807;
typedef long long int ll;
typedef unsigned long long int ull;
typedef unsigned long int ul;
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i+=1)
#define rf(i,a,b) for(ll i=(ll)a;i>=(ll)b;i-=1)
#define endl '\n'
#define N 1000000007 // prime modulo value
#define M 998244353
#define all(x) x.begin(),x.end()
#define mkp(a,b) make_pair(a,b)
using namespace std;
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}

	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
};
inline int mex(unordered_set<int> st)
{
	int m = 0;
	while (st.find(m) != st.end())
	{
		m += 1;
	}
	return m;
}
inline int calculateGrundy(int n)
{
	if (n == 1)
		return 0;
	unordered_set<int> mexi;
	for (int i = 1; i <= sqrt(n); i += 1)
	{
		if (n % i == 0)
		{
			if (n / i != n)
				mexi.insert(calculateGrundy(n / i));
			mexi.insert(calculateGrundy(i));
		}
	}
	return mex(mexi);
}
inline ll gcd(ll a, ll b)
{
	// This is code taken from geeksforgeek
	if (b == 0)
		return a;
	return gcd(b, a % b);
}
/*ll power(ll x, ll y)
{
	// This is code taken from geeksforgeek
	ll res = 1;

	x = x % N;
	if (x == 0) return 0;
	while (y > 0)
	{
		if (y & 1)
			res = (res * x) % N;
		y = y >> 1;
		x = (x * x) % N;
	}
	return res;
}*/
ll power(ll x, ll y)
{
	ll res = 1;

	x = x;

	while (y > 0)
	{

		if (y & 1)
			res = (res * x);

		y = y >> 1;
		x = (x * x);
	}
	return res;
}

/*ll modInverse(ll n, ll p)
{
	return power(n, p - 2, p);
}*/

ll sum(ll n)
{
	return (n * (n + 1)) / 2;
}
ll sum_digit(ll n)
{
	ll ans = 0;
	while (n)
	{
		ans += n % 10;
		n /= 10;
	}
	return ans;
}
ll gp(ll r, ll n)
{
	return (pow(r, n) - 1) / (r - 1);
}
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ll n;
	cin>>n;
	vector<ll> arr(n,0);
	f(i,0,n)
	{
		cin>>arr[i];
	}
	ll ans=0;
	f(i,1,n)
	{
		if(arr[i]<arr[i-1])
		{
			ans+=arr[i-1]-arr[i];
			arr[i]=arr[i-1];
		}
	}
	cout<<ans;
	return 0;
}


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

int H, W, A[509][509];
vector<tuple<int, int, int, int>> L;

int main() {
	cin >> H >> W;
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) cin >> A[i][j];
	}
	for (int i = 1; i <= H; i++) {
		for (int j = W - 1; j >= 1; j--) {
			if (A[i][j + 1] % 2 == 0) continue;
			L.push_back(make_tuple(i, j + 1, i, j));
			A[i][j + 1]--;
			A[i][j]++;
		}
	}
	for (int i = H - 1; i >= 1; i--) {
		if (A[i + 1][1] % 2 == 0) continue;
		L.push_back(make_tuple(i + 1, 1, i, 1));
		A[i + 1][1]--;
		A[i][1]++;
	}

	cout << L.size() << endl;
	for (int i = 0; i < L.size(); i++) cout << get<0>(L[i]) << " " << get<1>(L[i]) << " " << get<2>(L[i]) << " " << get<3>(L[i]) << endl;
	return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

ll N,M;
vector<int> A;

int main(){
  cin>>N>>M;
  A.resize(M);
  rep(i,M)cin>>A[i];
  if(M==1){
    cout<<A[0]<<endl;
    if(A[0]>1){
      cout<<2<<endl;
      cout<<A[0]-1<<" "<<1<<endl;
    }else{
      cout<<1<<endl;
      cout<<1<<endl;
    }
    return 0;
  }
  vector<int> os;
  rep(i,M){
    if(A[i]%2==1)os.push_back(i);
  }
  if(os.size()>2){
    cout<<"Impossible"<<endl;
    return 0;
  }
  if(os.size()==1){
    swap(A[0],A[os[0]]);
  }else if(os.size()==2){
    swap(A[0],A[os[0]]);
    swap(A[M-1],A[os[1]]);
  }
  rep(i,M)cout<<A[i]<<" ";
  cout<<endl;
  if(A[0]-1>0){
    cout<<M<<endl;
    cout<<A[0]-1<<" ";
  }else{
    cout<<M-1<<endl;
  }
  repl(i,1,M-1)cout<<A[i]<<" ";
  cout<<A[M-1]+1<<endl;
  return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
#define maxn 1000010

using namespace std;
typedef long long llg;

struct data{
	int l,r;
	data(int a=0,int b=0):l(a),r(b){}
}s[maxn];
int *q[26],lq[26],ct[26],n;
int R[maxn],ls,d[maxn];
char S[maxn],T[maxn];

int erfen(int *a,int r,int x){
	int l=0,mid;
	while(l!=r){
		mid=(l+r)>>1;
		if(a[mid]>=x) r=mid;
		else l=mid+1;
	}
	return l;
}

int main(){
	scanf("%d",&n);
	scanf("%s\n%s",S+1,T+1);
	for(int i=1;i<=n;i++) ct[S[i]-'a']++;
	for(int i=0;i<26;i++) q[i]=new int[ct[i]+10];
	for(int i=1,x;i<=n;i++)
		x=S[i]-'a',q[x][++lq[x]]=i;
	for(int i=0;i<26;i++) q[i][++lq[i]]=n+1,q[i][0]=0;
	for(int i=n,x,y,l=n;i;i--){
		x=T[i]-'a'; l=min(l,i);
		y=q[x][erfen(q[x],lq[x],l+1)-1];
		if(!y) return printf("-1"),0;
		if(!R[y]) R[y]=i; if(y<l) l=y;
	}
	for(int i=1;i<=n;i++)
		if(R[i]) s[++ls]=data(i,R[i]),d[ls]=i-ls;
	s[++ls]=data(n*2,n*2),d[ls]=n*2-ls;
	int ans=0;
	for(int i=1,j=1;i<=ls;i++){
		while(d[j]<s[i].r-i) j++;
		ans=max(ans,j-i);
	}
	printf("%d",ans);
	return 0;
}

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

typedef long long lint;
typedef long double louble;

template<typename T1,typename T2> inline T1 max(T1 a,T2 b){return a<b?b:a;}
template<typename T1,typename T2> inline T1 min(T1 a,T2 b){return a<b?a:b;}

namespace ae86
{
	const int bufl = 1<<15;

	char buf[bufl],*s=buf,*t=buf;

	inline int fetch()
	{
		if(s==t){t=(s=buf)+fread(buf,1,bufl,stdin);if(s==t)return EOF;}
		return *s++;
	}

	inline int ty()
	{
		int a=0,b=1,c=fetch();
		while(!isdigit(c))b^=c=='-',c=fetch();
		while(isdigit(c))a=a*10+c-48,c=fetch();
		return b?a:-a;
	}

	inline int ts(char *s)
	{
		int a=0,c=fetch();
		while(c<=32 && c!=EOF)c=fetch();
		while(c>32 && c!=EOF)s[a++]=c,c=fetch();
		s[a]=0;
		return a;
	}
}
using ae86::ty;
using ae86::ts;

const int _ = 1000007;

int n,que[_],he,ta;char s[_]={0},t[_]={0};

int main()
{
	n=ty(),ts(s+1),ts(t+1);

	int got=0;
	for(int i=1;i<=n;i++)if(s[i]!=t[i]){got=1;break;}
	if(!got){puts("0");return 0;}

	he=1,ta=0;
	int ans=0;
	for(int i=n,j=n;i>=1;i--)
	{
		if(t[i]==t[i-1])continue;
		j=min(j,i);
		while(j>0 && s[j]!=t[i])j--;
		if(j<=0){puts("-1");return 0;}
		while(he<=ta && que[he]-(ta-he+1)+1>i)he++;
		que[++ta]=j;
		if(i!=j)ans=max(ans,ta-he+1);
	}
	printf("%d\n",ans+1);

	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <iomanip>
#include <numeric>
#include <cstring>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define INF 1e18

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

const ll MOD = 1e9 + 7;

ll fastPow(ll x, ll n)
{
    if (n == 0)
        return 1;
    if (n % 2 == 0)
        return fastPow(x * x % MOD, n / 2);
    else
        return x * fastPow(x, n - 1) % MOD;
}

ll fac[112345];
void combInit(int mx)
{
    fac[0] = 0;
    fac[1] = 1;
    for (int i = 2; i <= mx; i++)
    {
        fac[i] = fac[i - 1] * i % MOD;
    }
}

ll modDiv(ll a, ll b)
{
    return a * fastPow(b, MOD - 2) % MOD;
}

int main()
{
    combInit(11234);

    ll perm[1010][1010];

    for (int i = 1; i <= 1000; i++)
    {
        perm[i][1] = i;
        for (int j = 2; j <= 1000; j++)
        {
            perm[i][j] = perm[i][j - 1] * (i - j + 1);
            perm[i][j] %= MOD;
        }
    }

    int n, a, b, c, d;

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

    ll dp[1010][1010];

    memset(dp, 0, sizeof(dp));

    for (int i = 0; i <= n; i++)
        dp[0][i] = 1;

    for (int i = 1; i <= n; i++)
    {
        for (int j = a; j <= b; j++)
        {
            dp[i][j] += dp[i][j - 1];
            dp[i][j] %= MOD;

            for (int k = c; k <= d; k++)
            {
                if (i - j * k < 0)
                    break;
                dp[i][j] += dp[i - j * k][j - 1] * modDiv(perm[n - i + j * k][j * k], fastPow(fac[j], k) * fac[k] % MOD);
                dp[i][j] %= MOD;
            }
        }
    }

    cout << dp[n][b] << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, s;
    bool truth = false;
    cin >> n >> s;
    s /= 1000;
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= n - i; j++)
        {
            if (i * 9 + j * 4 + n == s)
            {
                cout << i << ' ' << j << ' ' << n - i - j;
                truth = true;
                break;
            }
        }
        if (truth == true) break;
    }
    if (truth == false) cout << "-1 -1 -1";
}

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

using namespace std;

void darts();

void longest_dis();

void sum_of_three_integers();

void otoshidama();

int main()
{
  //darts();
  //longest_dis();
  //sum_of_three_integers();
  otoshidama();
}

void darts()
{
  while (true)
  {
    int n, m;
    cin >> n >> m;

    // stop cond
    if (n == 0) break;

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

    // 2 darts combination
    // p = q, 0, p + q
    vector<int> q = p;
    q.push_back(0);
    for (int i = 0; i < n; ++i)
    {
      for (int j = 0; j <= i; ++j)
      {
        q.push_back(p[i] + p[j]);
      }
    }

    sort(q.begin(), q.end());

    // q's size = 1 + n + n(n + 1) / 2
    //cout << q.size() << "\n";
    //for (auto x : q) cout << x << " ";

    int ret = 0;
    int j = q.size() - 1;
    //i:q linear scan
    for (int i = 0; i < q.size(); ++i)
    {
      if (q[i] > m) break;
      while (q[i] + q[j] > m) --j;
      ret = max(ret, q[i] + q[j]);
    }

    cout << ret << "\n";
  }
}

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

  vector<pair<int, int>> p(n);
  for (int i = 0; i < n; ++i) cin >> p[i].first >> p[i].second;

  double v_max = 0.0;
  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      if ((p[i].first - p[j].first) * (p[i].first - p[j].first)
        + (p[i].second - p[j].second) * (p[i].second - p[j].second)
        >  v_max * v_max)
      {
        v_max = sqrt((p[i].first - p[j].first) * (p[i].first - p[j].first)
          + (p[i].second - p[j].second) * (p[i].second - p[j].second));
      }
    }
  }

  cout << v_max << "\n";
}

void sum_of_three_integers()
{
  int k, s;
  cin >> k >> s;

  int cnt = 0;
  //for (int i = 0; i <= k; ++i)
  //{
  //  for (int j = 0; j <= k; ++j)
  //  {
  //    for (int m = 0; m <= k; ++m)
  //    {
  //      if (i + j + m == s) ++cnt;
  //    }
  //  }
  //}
  for (int i = 0; i <= k; ++i)
  {
    for (int j = 0; j <= k; ++j)
    {
      int m = s - i - j;
      if (0 <= m && m <= k) ++cnt;
    }
  }

  cout << cnt << "\n";
}

void otoshidama()
{
  int n, Y;
  cin >> n >> Y;

  for (int x = 0; x <= n; ++x)
  {
    for (int y = 0; x + y <= n; ++y)
    {
      int z = n - x - y;
      if (x * 10000 + y * 5000 + z * 1000 == Y)
      {
        cout << x << " "<< y << " " << z << "\n";
        return;
      }
    }
  }

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