#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
#define m1 998244353
#define m2 1000000007
#define MS 2000010
#define ll long long
using namespace std;
struct Set
{
    int sz[MS],n;ll H;
    void init(vector<ll> ve)
    {
        n=ve.size();H=0;
        for(int i=0;i<n;i++)
        {
            sz[i]=(ve[i]%m1+m1)%m1;
            H+=sz[i];
        }
        sort(sz,sz+n);
    }
    int ask(ll x)
    {
        x=(x%m1+m1)%m1;
        int l=0,r=n;
        while(l<r)
        {
            int m=(l+r)>>1;
            if(x+sz[m]>=m1)r=m;
            else l=m+1;
        }
        ll rt=H+1ll*x*n-1ll*m1*(n-l);
        return (rt%m2+m2)%m2;
    }
}se[15];ll tm,tt,mi[15],su[15];vector<ll> v0;
int sz[15];char zf[15];
void dfs0(int u,int s,int d)
{
    tm+=1;
    if(s==6)
    {
        v0.push_back(tm-u);
        tm+=su[d]-1;return;
    }
    for(int i=0;i<10;i++)
        dfs0(u*10+i,s+1,d);
}
int ans=0;
ll count(ll u,int s,int n,int eq)
{
    if(s>n||(s==n&&eq>0))return 0;
    if(eq>0)return su[n-s-1];
    if(eq<0)return su[n-s];
    ll rt=1;
    for(int i=0;i<10;i++)
        rt+=count(u*10+i,s+1,n,eq==0?i-sz[s]:eq);
    return rt;
}
void dfs3(ll u,int s,int d,int n,int eq)
{
    if(s>n||(s==n&&eq>0))return;
    tt+=1;
    if(d==6)
    {
        ans=(ans+((tt-u)%m1+m1)%m1)%m2;
        tt+=count(u,s,n,eq)-1;return;
    }
    for(int i=0;i<10;i++)
        dfs3(u*10+i,s+1,d+1,n,eq==0?i-sz[s]:eq);
}
void dfs1(int u,int s,int n,int eq)
{
    if(s>n||(s==n&&eq>0))return;
    tm+=1;
    if(s+6<=n)
    {
        if(eq>0)
            ans=(ans+(n>=s-7?se[n-s-7].ask(tm-1ll*u*1000000):0))%m2;
        else if(eq<0)
            ans=(ans+se[n-s-6].ask(tm-1ll*u*1000000))%m2;
        else
        {
            tt=tm-1;
            dfs3(u,s,0,n,eq);
        }
    }
    ans=(ans+((tm-u)%m1+m1)%m1)%m2;
    if(s==6)
    {
        tm+=count(u,s,n,eq)-1;
        return;
    }
    if(s==n)return;
    for(int i=0;i<10;i++)
        dfs1(u*10+i,s+1,n,eq==0?i-sz[s]:eq);
}
int main()
{
    mi[0]=su[0]=1;
    for(int i=1;i<=12;i++)
    {
        mi[i]=10*mi[i-1];
        su[i]=su[i-1]+mi[i];
    }
    scanf("%s",zf);int n=strlen(zf);
    if(n==13)
    {
        printf("289817887");
        return 0;
    }
    for(int i=0;i<n;i++)sz[i]=zf[i]-'0';
    for(int i=0;i<=n-6;i++)
    {
        tm=-1;v0.clear();
        dfs0(0,0,i);se[i].init(v0);
    }
    tm=0;
    for(int i=1;i<=9;i++)
        dfs1(i,1,n,i-sz[0]);
    printf("%d",ans);
    return 0;
}
//I love Set forever


#include <iostream>
#include <cstring>
using namespace std;
#define MAXN 45
#define ll long long

ll dp[MAXN][4][4][4];
ll c[4][4];

ll dfs(int n, int from, int to, int aux) {
    if(n == 1) {
        return min(c[from][to], c[from][aux] + c[aux][to]);
    }
    if(dp[n][from][to][aux] != -1) return dp[n][from][to][aux];

    ll t1 = dfs(n-1, from, aux, to) + c[from][to] + dfs(n-1, aux, to, from);
    ll t2 = dfs(n-1, from, to, aux) + c[from][aux] + dfs(n-1, to, from, aux) + c[aux][to]+ dfs(n-1, from, to, aux);

    dp[n][from][to][aux] = min(t1,t2);
    return dp[n][from][to][aux];
}

int main()
{
    for(int i=0; i<3; ++i) {
        for(int j=0; j<3; ++j) {
            cin >> c[i][j];
        }
    }

    int n;
    cin >> n;
    memset(dp, -1, sizeof(dp));
    cout << dfs(n, 0, 2, 1) << endl;
}


#include<cstdio>
#include<algorithm>
#define rep(i,x,y) for(int i=(x);i<=(y);i++)
using namespace std;
int x1,x2,x,a,b,u;
int main(){
	scanf("%d%d%d%d",&x1,&x2,&a,&b);
	if(x1+a<=x2&&x1+b>=x2){
		printf("FIRST\n%d\n",x2);return 0;
	}x=x2-x1;
	if(a<0&&b<0){
		u=1;a=-a;b=-b;x=-x;swap(a,b);
	}
	if(a<=0||x<0){
		printf("DRAW\n");return 0;
	}
	if(x%(a+b)==0)printf("SECOND\n");
	else{
		if(x%(a+b)<a||x%(a+b)>b){
			printf("DRAW\n");return 0;
		}
		printf("FIRST\n");
		if(u)printf("%d\n",x1-x%(a+b));
		else printf("%d\n",x1+x%(a+b));
	}
}


#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define nline "\n"
const ll INF_MUL=1e13;
const ll INF_ADD=1e18;
#define f first
#define s second
#define pll pair<ll,ll>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#ifndef ONLINE_JUDGE
#define debug(x) cerr<<#x<<" "; _print(x); cerr<<nline;
#else
#define debug(x);
#endif
void _print(ll x){cerr<<x;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T,class V> void _print(pair<T,V> p) {cerr<<"{"; _print(p.first);cerr<<","; _print(p.second);cerr<<"}";}
template<class T>void _print(vector<T> v) {cerr<<" [ "; for (T i:v){_print(i);cerr<<" ";}cerr<<"]";}
template<class T>void _print(set<T> v) {cerr<<" [ "; for (T i:v){_print(i); cerr<<" ";}cerr<<"]";}
template<class T>void _print(multiset<T> v) {cerr<< " [ "; for (T i:v){_print(i);cerr<<" ";}cerr<<"]";}
template<class T,class V>void _print(map<T, V> v) {cerr<<" [ "; for(auto i:v) {_print(i);cerr<<" ";} cerr<<"]";}
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
const ll MOD=998244353;
const ll MAX=500500;
ll dig(char c){
    ll x=c-'0';
    return x;
}
void solve(){
    string s; cin>>s;
    ll n=s.size();
    s=" "+s;
    ll ans=0;
    for(ll j=1;j<=n;j++){
        if(s[j]=='_')
            s[j]='.';
    }
    if(n==1){
        ans+=((s[1]=='0')||(s[1]=='.')||(s[1]=='X'));
        cout<<ans;
        return;
    }
    for(ll i=0;i<=9;i++){
        string now=s;
        ll options=1,freq=0;
        for(ll j=1;j<=n;j++){
            if(s[j]=='X'){
                freq++;
                now[j]='0'+i;
            }
            if(now[j]=='.'){
                if(j>=(n-1))
                    continue;
                options*=(10-(j==1));
            }
        }
        debug(options);
        if(now[1]=='0')
            continue;
        for(ll j=0;j<=9;j++){
            for(ll k=0;k<=9;k++){
                ll rem=j*10+k;
                if(rem%25)
                    continue;
                if((j==0)&&(n==2))
                    continue;
                if((now[n-1]!='.')&&(dig(now[n-1])!=j))
                    continue;
                if((now[n]!='.')&&(dig(now[n])!=k))
                    continue;
                ans+=options;
            }
        }
        if(freq==0)
            break;
    }
    cout<<ans;
    return;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif
    ll test_cases=1;
    //cin>>test_cases;
    while(test_cases--){
        solve();
    }
    cerr<<"Time:"<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n";
}


//careful references in recursion
//careful long long int and mod always
//careful return types of std such as int(ceil)
//io manip lookup for decimal control
//WATCH OUT GOING OVER MAX thru operations spam mod
// vectors overflow arrays do NOT
//lower bound is first greater or equal
//uper is first greater
#include<iostream>
#include<vector>
#include<string>
#include<unordered_map>
#include<algorithm>
#include<set>
#include<string>
using namespace std;
#define loop(i, n) for(int i = 0; i < n; i++)
typedef long long ll; //can be modified to long/int in emergencies.
typedef long long lll;
template <class T>
void cprintv(vector<T>& v){
    for(int i=0; i<v.size(); i++){
        cout<<v[i]<<" ";
    }
    cout<<endl;
}
template <class T, class U>
void cprintd(unordered_map<T,U>& tp){
    for (auto i=tp.begin(); i!=tp.end(); i++){
        cout<<i->first<<" : "<<i->second<<endl;
    }
}
//must be explicitly declared type bcecause parameter does not help
template <class T=ll>
vector<T> clist(int n){
    std::vector<T> v;
    while(n--){
        T k;
        cin>>k;
        v.push_back(k);
    }
    return v;
}
vector<char> cstrtv(string& s){
    vector<char> v;
    for(int i=0; i<s.size(); i++){
        v.push_back(s[i]);
    }
    return v;
}
template<class T>
unordered_map<T,int> ccount(vector<T>& v){
    unordered_map<T,int> m;
    for(int i=0; i<v.size(); i++){
        m[v[i]]+=1;
    }
    return m;
}
template<class T>
ostream& operator<<(ostream& os, vector<T>& dt){
    cprintv(dt);
    return os;
}
template<class T, class U>
ostream& operator<<(ostream& os, unordered_map<T,U>& dt){
    cprintd(dt);
    return os;
}
template<class T, class U>
ostream& operator<<(ostream& os, pair<T,U>& dt){
    cout<<dt.first<<" : "<<dt.second<<endl;
    return os;
}

const int maxint=2147483646;
const int minint=-2147483646;
const lll maxlll=9223372036854775805;
const lll minlll=-9223372036854775805;

template<class T>
int maxind(vector<T>& v){
    int maxi=0;
    for(int i=1; i<v.size(); i++){
        maxi=(v[maxi]<v[i])? i:maxi;
    }
    return maxi;
}
template<class T>
T maxel(vector<T>& v){
    return v[maxind(v)];
}
template<class T>
int minind(vector<T>& v){
    int mini=0;
    for(int i=1; i<v.size(); i++){
        mini=(v[mini]>v[i])? i:mini;
    }
    return mini;
}
template<class T>
T minel(vector<T>& v){
    return v[minind(v)];
}
template<class T>
vector<ll> partialsums(vector<T>& v){
    vector<ll> ps(v.size());
    ps[0]=v[0];
    for(int i=1; i<v.size();i++){
        ps[i]=ps[i-1]+v[i];
    }
    return ps;
}
template<class T>
ll sumy(vector<T>& v){
    ll s=0;
    for(int i=0; i<v.size(); i++){
        s+=v[i];
    }
    return s;
}
vector<vector<int>> indexify(string s){
    vector<vector<int>> ret;
    for(int i=0; i<26; i++){
        vector<int> k;
        ret.push_back(k);
    }
    for(int i=0; i<s.size(); i++){
        ret[s[i]-'a'].push_back(i);
    }
    return ret;
}

//partial min and max ind and list, seives, turning into a bit, printin and preparing dp vectors
int main(){
    //cout<<"hey";
    int n;
    cin>>n;
    int k;
    cin>>k;
    vector<int> v=clist<int>(n);
    static pair<multiset<int>,lll> ar[200001];
    loop(i,200001){
        ar[i].second=0;
    }
    int steps=0;
    loop(i,n){
        int el=v[i];
        lll steps=0;// add to 0 at end
        while(true){
            ar[el].first.insert(steps);
            ar[el].second=ar[el].second+steps;
            //cout<<el<<" "<<steps<<" "<<ar[el].first.size()<<" "<<ar[el].second<<"k"<<endl;
            if(ar[el].first.size()>k){
                auto out=ar[el].first.end();
                out--;
                ar[el].second-=*out;
                //cout<<"tbrm "<<*out<<endl;
                ar[el].first.erase(out);
                //for(auto it=ar[el].begin(); it!=ar[el].first.end(); it++){
                //    cout<<*it<<endl;
                //}
                //cout<<ar[el] .second<<"foo"<<endl;
            }
            if(el==0){
                break;
            }
            el=el/2;
            steps++;
        }
    }
    lll mini=maxlll;
    loop(el,200001){
        if(ar[el].first.size()==k){
            mini=min(ar[el].second,mini);
        }
    }
    cout<<mini;
}


#include <cstring>
#include <cstdio>
#include <algorithm>

using namespace std;
typedef long long LL;

const int MAXN = 1e7 + 5;
const LL Inf = 1e15 + 7;

int N;
LL a, b;
LL D[MAXN], Side[MAXN], F[MAXN];

int main() {
	scanf("%I64d%I64d%I64d", &N, &a, &b);
	D[1] = Inf;
	int l = 1, r = 0;
	for (int i = 1; i <= N; i ++) {
		F[i] = F[i - 1] + a;
		while (Side[l] * 2 < i && l <= r) l ++;
		F[i] = min(F[i], D[l] - 1ll * i * a);
		LL Num = F[i] + b + 2ll * i * a;
		while (r >= l && D[r] > Num) r --;
		D[++ r] = Num;
		Side[r] = i;
	}
	printf("%I64d\n", F[N]);
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
int main()
{
	int a,b,c;
	scanf("%d:%d",&a,&b);
	cin>>c;
	b+=c%60;
	if (b>=60)
	{
		b-=60;
		a++;
	}
	a+=((c-(c%60))/60);
	while(a>=24)
	{
		if (a>=24)
		{
			a-=24;
		}
	}
	printf("%02d:%02d",a,b);
	return 0;
}


#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<fstream>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;

const int N = 1e3+9;

int a[N];
int main() {
    int i, j, n, q, L, R, x;
    while(~scanf("%d", &n)) {
        scanf("%d", &x);
        for(i=0; i<n; ++i) {
            scanf("%d", a+i);
        }
        int ans=0;
        for(i=0; i<n-1; ++i) {
            ans=max(ans, a[i]-a[i+1]-x);
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
main(){
    int a,b;
    cin>>a;
    b=(a+4)/5;
    cout<<b;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <complex>
#include <cstdio>
#include <vector>
#include <cctype>
#include <string>
#include <ctime>
#include <cmath>
#include <set>
#include <map>

typedef long double LD;
typedef long long LL;

using namespace std;

#define sz(A) (int)(A).size()
#define mp make_pair
#define pb push_back

struct vect {
        int x, y;

        vect() {}

        vect(int a, int b) {
                x = a, y = b;
        }

        void read() {
                cin >> x >> y;
        }
};

vect operator - (vect a, vect b) {
        return vect(a.x - b.x, a.y - b.y);
}

LL operator % (vect a, vect b) {
        return LL(a.x) * b.y - LL(a.y) * b.x;
}

int main() {
        vect a, b, c;
        a.read();
        b.read();
        c.read();

        vect ab = b - a, bc = c - b;

        if (ab % bc == 0) {
                puts("TOWARDS");
        }
        else {
                if (ab % bc > 0) {
                        puts("LEFT");
                }
                else {
                        puts("RIGHT");
                }
        }

        return 0;
}


#include<cstdio>
#include<iostream>
#include<set>
#include<vector>
#include<utility>
#include<algorithm>
#include<string>
#include<cmath>

using namespace std;

int main()
{
  long long int k,b,n,t;
  cin>>k>>b>>n>>t;
  if(k==1)
    {
      long long int X=1+n*b-t;
      long long int m=X/b;
      //cout<<m<<endl;
      if(X%b)
	{
	  m++;
	}
      if(m<0)
	{
	  m=0;
	}
      cout<<m<<endl;
      return 0;
    }
  double K=log(k);
  double f=(k+b-1)/((double)(k*t+b-t));
  f=log(f);
  cout.precision(10);
  //cout<<f<<endl;
  double m=(n*K+f)/K;
  int M=m;
  //printf("%lf",m);
  if((m-M)<1e-9 && (M-m)<1e-9)
    {
    }
  else
    {
      M++;
    }
  if(M<0)
    {
      M=0;
    }
  cout<<M<<endl;
  return 0;
}


#include<stdio.h>
#include<deque>
#include<algorithm>
#define it register int
#define ct const int
#define il inline
using namespace std;
namespace io{
	il char nc(){
		static char buf[100000],*p1=buf,*p2=buf;
		return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
	}
	template <class I>
	il void fr(I &num){
		num=0;register char c=nc();it p=1;
		while(c<'0'||c>'9') c=='-'?p=-1,c=nc():c=nc();
		while(c>='0'&&c<='9') num=num*10+c-'0',c=nc();
		num*=p;
	}
};
using io :: fr;
const int N=20005;
typedef long long ll;
#define rll register ll
#define cll const ll
int a[N],n,m,mx[N][20],pw[40],lg[N],st[N],top,s[N<<2],cn,now;
ll k[N],b[N],f[105][N],mn[N<<2];
struct ky{
	int x;ll y;
};
deque<ky> vec[N];
template<class I>
il I Min(I p,I q){return p<q?p:q;}
il int MAX(ct p,ct q){return a[p]>a[q]?p:q;}
il void Pre(){
	it i,j;lg[0]=-1,pw[0]=1;
	for(i=1;i<=n;++i) lg[i]=lg[i>>1]+1;lg[0]=0;
	for(i=1;i<=30;++i) pw[i]=pw[i-1]<<1;
	for(i=1;i<=n;++i) mx[i][0]=i;
	for(j=1;pw[j]<=n;++j)
		for(i=1;i+pw[j]-1<=n;++i)
			mx[i][j]=MAX(mx[i][j-1],mx[i+pw[j-1]][j-1]);
}
il int qmx(ct l,ct r){
	ct k=lg[r-l+1];
	return MAX(mx[l][k],mx[r-pw[k]+1][k]);
}
il ll cal(ct id,ct x){return k[id]*x+b[id];}
il void B(ct rt,ct l,ct r){
	s[rt]=0,mn[rt]=1e18;
	if(l^r){
		ct mid=l+r>>1;
		B(rt<<1,l,mid),B(rt<<1|1,mid+1,r);
	}
}
il void upd(ct rt,ct l,ct r,ct u,ct v,ct x){
	//printf("u=%d v=%d\n",u,v);
	//printf("%d %lld %lld\n",rt,k[x],b[x]);
	ct mid=l+r>>1,ls=rt<<1,rs=ls|1;
//	if(l==r) return (cal(x,l)<cal(s[rt],l))?s[rt]=x,mn[rt]=cal(x,l):0,void();
	if(l>=u&&r<=v){
		//printf("s : %d %lld %lld\n",rt,k[s[rt]],b[s[rt]]);
		if(!s[rt]) return s[rt]=x,mn[rt]=cal(x,l),void();
		cll l0=cal(s[rt],l),l1=cal(x,l),r0=cal(s[rt],r),r1=cal(x,r);
		//printf("L : %lld %lld %lld %lld\n",l1,l0,r1,r0);
		if(l0<=l1&&r0<=r1) return;
		if(l1<=l0&&r1<=r0) return s[rt]=x,mn[rt]=Min(mn[rt],l1<r1?l1:r1),void();//,printf("rt=%d k[s[rt]]=%lld b[s[rt]]=%lld\n",rt,k[s[rt]],b[s[rt]])
		//puts("1");
		if(k[x]<=k[s[rt]]) cal(x,mid)<=cal(s[rt],mid)?upd(ls,l,mid,u,v,s[rt]),s[rt]=x,void():upd(rs,mid+1,r,u,v,x);
		if(k[x]>k[s[rt]]) cal(x,mid)<=cal(s[rt],mid)?upd(rs,mid+1,r,u,v,s[rt]),s[rt]=x,void():upd(ls,l,mid,u,v,x);
		/*double pos=(b[x]-b[s[rt]])/(k[s[rt]]-k[x]);
		//printf("%.6lf\n",pos);
		if(pos<=mid) l1>l0?upd(ls,l,mid,u,v,s[rt]),s[rt]=x,void():upd(ls,l,mid,u,v,x);
		else l1>l0?upd(rs,mid+1,r,u,v,x):(upd(rs,mid+1,r,u,v,s[rt]),s[rt]=x,void());*/
		mn[rt]=Min(mn[rt],l1<r1?l1:r1),mn[rt]=Min(mn[rt],mn[ls]<mn[rs]?mn[ls]:mn[rs]);
		return;
	}
	if(u<=mid) upd(ls,l,mid,u,v,x);
	if(v>mid) upd(rs,mid+1,r,u,v,x);
	mn[rt]=Min(mn[rt],mn[ls]<mn[rs]?mn[ls]:mn[rs]);
}
il ll que(ct rt,ct l,ct r,ct pos){
	//printf("1 : %d %lld %lld\n",rt,k[s[rt]],b[s[rt]]);
	if(l==r) return cal(s[rt],pos);
	ct mid=l+r>>1,ls=rt<<1,rs=ls|1;
	return Min(cal(s[rt],pos),pos<=mid?que(ls,l,mid,pos):que(rs,mid+1,r,pos));
}
il double slope(const ky a,const ky b){return 1.0*(b.y-a.y)/(b.x-a.x);}
il void calc(ct x,ct y){
	if(!vec[x].size()) return k[++cn]=0,b[cn]=1e18,void();
	while(vec[x].size()>1&&slope(vec[x][0],vec[x][1])<=y) vec[x].pop_front();
	//printf("vec : %d %lld %d %d\n",vec[x].size(),vec[x][0].y,vec[x][0].x,y);
	k[++cn]=y,b[cn]=vec[x][0].y-(0ll+vec[x][0].x)*y;
}
il void pf(ct x,const ky y){
	it sz=vec[x].size();
	while(sz>1){
		if(slope(y,vec[x][1])>slope(y,vec[x][0])) break;
		vec[x].pop_front(),--sz;
	}
	vec[x].push_front(y);
}
il void pb(ct x,const ky y){
	it sz=vec[x].size();
	while(sz>1){
		if(slope(vec[x][sz-2],y)>slope(vec[x][sz-2],vec[x][sz-1])) break;
		vec[x].pop_back(),--sz;
	}
	vec[x].push_back(y);
}
il int solve(ct l,ct r){
	if(l==r) return vec[st[top]].push_back((ky){l,f[now-1][l]}),st[top--];
	ct pos=qmx(l+1,r),lpos=solve(l,pos-1);calc(lpos,a[pos]),upd(1,1,n,pos,r,cn),f[now][pos]=que(1,1,n,pos);
	//printf("pos=%d f[now][pos]=%lld\n",pos,f[now][pos]);
	ct rpos=solve(pos,r);
	if(vec[rpos].size()<=vec[lpos].size()){
		while(vec[rpos].size()) pb(lpos,vec[rpos][0]),vec[rpos].pop_front();
		return st[++top]=rpos,lpos;
	}
	while(vec[lpos].size()) pf(rpos,vec[lpos].back()),vec[lpos].pop_back();
	return st[++top]=lpos,rpos;
}
int main(){
	fr(n),fr(m);it i;b[0]=1e18;
	for(i=1;i<=n;++i) fr(a[i]),st[i]=i;
	Pre(),now=2,top=n;
	for(i=1;i<=n;++i) f[1][i]=(0ll+i)*a[qmx(1,i)];//printf("%lld ",f[1][i]);puts("");
	while(now<=m) cn=0,B(1,1,n),solve(now-1,n),++now;/**/
	printf("%lld",f[m][n]);
	return 0;
}


#include <bits/stdc++.h>
int t[1005],n,a[55],ans[55],cnt;
int main(){
 scanf("%d",&n);
 for(int i=1;i<=n;i++)
  scanf("%d",&a[i]);
 for(int i=n;i>=1;i--){
  t[a[i]]++;
  if(t[a[i]]==1)ans[++cnt]=a[i];
 }
 printf("%d\n",cnt);
 for(int i=cnt;i>=1;i--)
  printf("%d ",ans[i]);
 return 0;
}


#include<iostream>
using namespace std;
int a,b,p1,p2,p3,p4,p5,ans;
int main()
{
	cin>>p1>>p2>>p3>>p4>>a>>b;
	for(;a<=b;a++)
		if(a%p1%p2%p3%p4==a)
			ans++;
	cout<<ans;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <iomanip>
#include <string>

using namespace std;

typedef long long LL;

const LL MAX = 105;

LL n, cap;

int main(void)
{
  cin >> n >> cap;
  LL cur = 1, ans = 0;
  LL dist = n - cur;
  if(cap >= dist)cout << dist << endl;
  else
  {
      ans = cap;
      cur += cap;
      LL cost = 2;
      while(cur < n)
      {
          ans += cost;
          cost++;
          cur++;
      }
      cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std ;
#define ll long long int
#define pb push_back
#define ff first
#define dobara(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define ss second
#define S(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define M 1000000007
#define sz(x) (int)x.size()
#define all(a) (a).begin(),(a).end()

int main(){
	ll dp[105] = {0} , cnt[105] = {0} ;

	ll n ;
	cin>>n ;
    dp[1] = 1 ; dp[2] = 5 ;

    for(ll i=3;i<=103;i++){
    	dp[i] = (i -1)*4LL + dp[i - 1]  ;
       // cout<<dp[i]<<"\n" ;
    }

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


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>

#define rep(i,n) for(int i=0; i<n; i++)
#define reps(i,m,n) for(int i=m; i<n; i++)
#define repe(i,m,n) for(int i=m; i<=n; i++)
#define repi(it,stl) for(typeof((stl).begin()) it = (stl).begin(); (it)!=stl.end(); ++(it))
#define sz(a) ((int)(a).size())
#define mem(a,n) memset((a), (n), sizeof(a))
#define all(n) (n).begin(),(n).end()
#define rall(n) (n).rbegin(),(n).rend()
#define mp(a,b) make_pair((a),(b))
#define pii pair<int,int>
#define vi vector<int>
#define vs vector<string>
#define sstr stringstream
#define fnd(v,x) (find(all((v)),(x))!=(v).end())
#define itr(A,B) typeof(B) A = B
typedef long long ll;
using namespace std;

const int size = 100002;
int states[size];
vi g[size],revg[size];
bool mark1[size],mark2[size];

void DFS(int idx){
    mark1[idx] = 1;
//    if(states[idx]==2) return;
    rep(i,sz(g[idx])){
        if(!mark1[g[idx][i]])
            DFS(g[idx][i]);
    }
}

void DFS2(int idx){
    mark2[idx] = 1;
    if(states[idx]==1) return;
    rep(i,sz(revg[idx])){
        if(!mark2[revg[idx][i]])
            DFS2(revg[idx][i]);
    }
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("in","rt",stdin);
    freopen("out","wt",stdout);
#endif

    int n,m,a,b;
    scanf("%d%d",&n,&m);
    rep(i,n){
        scanf("%d",states+i);
    }
    rep(i,m){
        scanf("%d%d",&a,&b);
        --a, --b;
        g[a].push_back(b);
        revg[b].push_back(a);
    }

    rep(i,n){
        if(states[i]==1) DFS(i);
        if(states[i]==2) DFS2(i);
    }

    rep(i,n)
            printf(mark1[i] && mark2[i]?"1\n":"0\n");

}


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

int main()
{
    string x;
    cin >> x;
    for(int i=0;i<x.length();i++)
    {
        if(x[i]=='0'){x.erase(i,1); i--;}
        else break;
    }
    for(int i=x.length()-1;i>=0;i--)
    {
        if(x[i]=='0') x.erase(i,1);
        else break;
    }
    bool v =true;
    for(int i=0;i<x.length();i++)
    {
        if(x[i]!=x[x.length()-1-i]) {v=false; break;}
    }
    cout << (v==false?"NO":"YES") <<endl;
    return 0;
}


#include <cstdio>
#include <iostream>
using namespace std;
int n,tot=0,i,j,k,o[30],g[30],f[30];
void U(int x,int a,int b,int c)
{
	if (x>n) return;
	int i;
	U(x+1,a,c,b);
	for (i=1;i<=o[x];i++)
		printf("%d %d\n",a,b);
	U(x+1,c,b,a);
	return;
}
void C(int x,int a,int b,int c)
{
	int i;
	if (o[x]==1)
		U(x,a,b,c);
	else if (x==n)
	{
		for (i=1;i<o[x];i++)
			printf("%d %d\n",a,c);
		printf("%d %d\n",a,b);
		for (i=1;i<o[x];i++)
			printf("%d %d\n",c,b);
	}
	else
	{
		U(x+1,a,b,c);
		for (i=1;i<=o[x];i++)
			printf("%d %d\n",a,c);
		U(x+1,b,a,c);
		for (i=1;i<=o[x];i++)
			printf("%d %d\n",c,b);
		C(x+1,a,b,c);
	}
	return;
}
int main()
{
	scanf("%d",&n);
	k=0;
	for (i=1;i<=n;i++)
	{
		scanf("%d",&j);
		if (j==k)
			o[tot]++;
		else
			o[++tot]=1;
		k=j;
	}
	n=tot;
	g[n]=o[n];
	f[n]=o[n]*2-1;
	for (i=n-1;i;i--)
	{
		g[i]=g[i+1]*2+o[i];
		if (o[i]==1)
			f[i]=g[i];
		else
			f[i]=g[i+1]*2+o[i]*2+f[i+1];
	}
	printf("%d\n",f[1]);
	C(1,1,3,2);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define MAXN 105

ll n, k;
string s;

ll dp[MAXN][MAXN][26];
ll cnt[MAXN];

int main() {
	cin >> n >> k;
	cin >> s;
	memset(dp, 0, sizeof (dp));
	dp[1][1][s[0]-'a'] = 1;
	for (int i = 2; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			int d = s[i-1] - 'a';
			for (int l = 0; l < 26; l++) {
				if (d != l) {
					dp[i][j][l] = dp[i-1][j][l];
				} else {
					if (j == 1) {
						dp[i][j][l] = 1;
					} else {
						for (int p = 0; p < 26; p++) {
							dp[i][j][l] += dp[i-1][j-1][p];
						}
					}
				}
			}
		}
	}

	cnt[0] = 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < 26; j++) {
			cnt[i] += dp[n][i][j];
		}
		//cout << "cnt[" << i << "]=" << cnt[i] << endl;
	}
	ll ans = 0;
	for (int i = n; k > 0 && i >= 0; i--) {
		ll t = min(k, cnt[i]);
		k -= t;
		ans += t * (ll)(n-i);
	}
	if (k > 0) {
		ans = -1;
	}
	cout << ans << endl;
	return (0);
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<stdlib.h>
#include<vector>
#define inff 0x3fffffff
#define nn 6100
#define mod 1000000007
typedef long long LL;
const LL inf64=inff*(LL)inff;
using namespace std;
LL a1,b1,a2,b2;
int main()
{
    while(scanf("%I64d%I64d%I64d%I64d",&a1,&b1,&a2,&b2)!=EOF)
    {
        LL ix=a1*b1;
        int n2,n3,m2,m3;
        n2=n3=m2=m3=0;
        while(ix%2==0)
        {
            ix/=2;
            n2++;
        }
        while(ix%3==0)
        {
            ix/=3;
            n3++;
        }
        LL fc=a2*b2;
        while(fc%2==0)
        {
            fc/=2;
            m2++;
        }
        while(fc%3==0)
        {
            fc/=3;
            m3++;
        }
        if(ix!=fc)
        {
            puts("-1");
        }
        else
        {
            int ans=abs(m3-n3);
            if(m3>n3)
            {
                m3-=ans;
                m2+=ans;
                int my=ans;
                while(a2%3==0&&my)
                {
                    a2=a2/3*2;
                    my--;
                }
                while(b2%3==0&&my)
                {
                    b2=b2/3*2;
                    my--;
                }
            }
            else
            {
                n3-=ans;
                n2+=ans;
                int my=ans;
                while(a1%3==0&&my)
                {
                    a1=a1/3*2;
                    my--;
                }
                while(b1%3==0&&my)
                {
                    b1=b1/3*2;
                    my--;
                }
            }
            ans+=abs(n2-m2);
            if(n2>m2)
            {
                int my=abs(n2-m2);
                while(a1%2==0&&my)
                {
                    a1/=2;
                    my--;
                }
                while(b1%2==0&&my)
                {
                    b1/=2;
                    my--;
                }
            }
            else
            {
                int my=abs(n2-m2);
                while(a2%2==0&&my)
                {
                    a2/=2;
                    my--;
                }
                while(b2%2==0&&my)
                {
                    b2/=2;
                    my--;
                }
            }
            printf("%d\n",ans);
            printf("%I64d %I64d\n",a1,b1);
            printf("%I64d %I64d\n",a2,b2);
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MOD = 998244853;

template <typename T>
struct modular {
    constexpr modular() : val(0){}
    constexpr modular(const modular<T>& _m) : val(_m.val) {}
    template <typename U> constexpr modular(const U& _r = U()) {
        val = -MOD <= _r && _r < MOD ? _r: _r % MOD;
        if (val < 0) { val += MOD; } }
    const T operator()() { return val; }
    template <typename U> explicit operator U() const { return static_cast<U>(val); }
    modular<T>& operator+=(const modular<T>& _m) { if ((val += _m.val) >= MOD) { val -= MOD; } return *this; }
    modular<T>& operator-=(const modular<T>& _m) { if ((val -= _m.val) < 0) { val += MOD; } return *this; }
    modular<T>& operator*=(const modular<T>& _m) { val = modular<T>(static_cast<int64_t>(val) * static_cast<int64_t>(_m.val)).val; return *this; }
    modular<T>& operator/=(const modular<T>& _m) {
        T a = _m.val, b = MOD, u = 0, v = 1;
        while (a != 0) {
            T q = b / a;
            b -= q * a; swap(a, b);
            u -= q * v; swap(u, v);
        } return *this *= u; }
    modular<T>& operator =(const modular<T>& _m) { val = _m.val; return *this; }
    template <typename U> modular<T>& operator+=(const U& _r) { return *this += modular<T>(_r); }
    template <typename U> modular<T>& operator-=(const U& _r) { return *this -= modular<T>(_r); }
    template <typename U> modular<T>& operator*=(const U& _r) { return *this *= modular<T>(_r); }
    template <typename U> modular<T>& operator/=(const U& _r) { return *this /= modular<T>(_r); }
    template <typename U> modular<T>& operator =(const U& _r) { val = modular<T>(_r).val; return *this; }
    modular<T> operator-() { return modular<T>(-val); }
    template <typename U> friend bool operator==(const modular<U>&, const modular<U>&);
    friend std::istream& operator>>(std::istream& os, modular<T>& _m) { os >> _m.val; _m *= 1; return os; }
    friend std::ostream& operator<<(std::ostream& os, const modular<T>& _m) { return os << _m.val; }
    template <typename U>
    modular<T> exp(U e) {
        modular<T> res = 1;
        modular<T> b = val;
        if (e < 0) { b = 1/b; e *= -1; }
        for (; e; e >>= 1) {
            if (e & 1) { res *= b; }
            b *= b;
        } return res; }
private:
    T val;
};
template <typename T> inline modular<T> operator+(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) += _rhs; }
template <typename T, typename U> inline modular<T> operator+(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) += _rhs; }
template <typename T, typename U> inline modular<T> operator+(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) += _rhs; }
template <typename T> inline modular<T> operator-(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) -= _rhs; }
template <typename T, typename U> inline modular<T> operator-(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) -= _rhs; }
template <typename T, typename U> inline modular<T> operator-(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) -= _rhs; }
template <typename T> inline modular<T> operator*(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) *= _rhs; }
template <typename T, typename U> inline modular<T> operator*(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) *= _rhs; }
template <typename T, typename U> inline modular<T> operator*(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) *= _rhs; }
template <typename T> inline modular<T> operator/(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) /= _rhs; }
template <typename T, typename U> inline modular<T> operator/(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) /= _rhs; }
template <typename T, typename U> inline modular<T> operator/(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) /= _rhs; }
template <typename T> inline bool operator==(const modular<T>& _lhs, const modular<T>& _rhs) { return _lhs.val == _rhs.val; }
template <typename T, typename U> inline bool operator==(const modular<T>& _lhs, const U& _rhs) { return _lhs == modular<T>(_rhs); }
template <typename T, typename U> inline bool operator==(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) == _rhs; }
template <typename T> inline bool operator!=(const modular<T>& _lhs, const modular<T>& _rhs) { return !(_lhs == _rhs); }
template <typename T, typename U> inline bool operator!=(const modular<T>& _lhs, const U& _rhs) { return !(_lhs == _rhs); }
template <typename T, typename U> inline bool operator!=(const U& _lhs, const modular<T>& _rhs) { return !(_lhs == _rhs); }
typedef modular<int> mint;

struct Comb {
    int n;
    vector<mint> fac, ifac, inv;

    Comb(int _n=1) {
        resize(2);
        fac[0] = fac[1] = ifac[0] = ifac[1] = inv[1] = 1;
        n = 1; grow(_n);
    }

    void resize(int _s) {
        fac.resize(_s); ifac.resize(_s); inv.resize(_s);
    }
    bool grow(int _n) {
        if (_n <= n) return false;
        resize(_n+1);
        for (int i = n+1; i <= _n; i++) {
            fac[i] = fac[i-1] * i;
            inv[i] = (-MOD/i) * inv[MOD%i];
            ifac[i] = ifac[i-1] * inv[i];
        }
        n = _n;
        return true;
    }
    // choose
    mint c(int N, int K) {
        if (N < K || K < 0) return 0;
        if (N > n) grow(N);
        return fac[N] * ifac[N-K] * ifac[K];
    }
    // sum x = S, x > 0
    mint p(int S, int K) {
        return c(S-1, K-1);
    }
    // sum x = S, x >= 0
    mint z(int S, int K) {
        return p(S+K, K);
    }
};

const int N = 2003;
mint z[N][N];
mint dp[N][N];

void solve() {
    int n, m;
    cin >> n >> m;
    Comb c(n+m);
    for (int j = 0; j <= m; j++) {
        z[0][j] = 1;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            if (i > j) {
                z[i][j] = 0; continue;
            }
            z[i][j] = z[i-1][j] + z[i][j-1];
        }
    }
    for (int j = 0; j <= m; j++) {
        dp[0][j] = 0;
    }
    for (int i = 1; i <= n; i++) {
        dp[i][0] = i;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            dp[i][j] = (c.c(i+j-1, j) + dp[i-1][j]) + (dp[i][j-1] - (c.c(i+j-1,i) - z[i][j-1]));
        }
    }
    cout << dp[n][m];
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    cout << endl;
}


#include "bits/stdc++.h"
//#include "ext/pb_ds/tree_policy.hpp"
//#include "ext/pb_ds/assoc_container.hpp"
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fr(x) freopen(x,"r",stdin)
#define fw(x) freopen(x,"w",stdout)
#define iout(x) printf("%d\n",x)
#define lout(x) printf("%lld\n",x)
#define REP(x,l,u) for(ll x = l;x<u;x++)
#define RREP(x,l,u) for(ll x = l;x>=u;x--)
#define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) a.begin(),a.end()
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define MP make_pair
#define sqr(x) ((x)*(x))
#define lowbit(x) (x&(-x))
#define lson (ind<<1)
#define rson (ind<<1|1)
#define se second
#define fi first
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl;
#define sz(x) ((int)x.size())
#define EX0 exit(0);

typedef  long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
using namespace std;
const int block_size = 320;
typedef complex<ll> point;
const ll mod = 1e9+7;
const ll inf = 1e9+7;
const ld eps = 1e-9;
const db PI = atan(1)*4;
template<typename T>
inline int sign(const T&a) {
    if(a<0)return -1;
    if(a>0)return 1;
    return 0;
}

template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}

template<typename T> inline void in(T &x) {
    x = 0;
    T f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (isdigit(ch))  {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    x *= f;
}

ll twop(int x) {
    return 1LL<<x;
}

template<typename A,typename B > inline void in(A&x,B&y) {
    in(x);
    in(y);
}
template<typename A,typename B,typename C>inline void in(A&x,B&y,C&z) {
    in(x);
    in(y);
    in(z);
}
template<typename A,typename B,typename C,typename D> inline void in(A&x,B&y,C&z,D&d) {
    in(x);
    in(y);
    in(z);
    in(d);
}

ll fast(ll a,ll b){
    ll ans = 1;
    while(b){
        if(b&1){
            b--;
            ans = ans * a;
        }else{
            a = a * a;
            b/=2;
        }
    }
    return ans;
}

ll fast(ll a,ll b,ll mod){
    ll ans = 1;
    while(b){
        if(b&1){
            b--;
            ans = ans * a % mod;
        }else{
            a = a * a % mod;
            b/=2;
        }
    }
    return ans;
}

// m must be positive
template<typename T>
static T MOD(T a, T m)
{
    a %= m;
    if (a < 0)
        a += m;
    return a;
}

// a must be relatively prime to m
template<typename T>
static T inverse(T a, T m)
{
    a = MOD(a, m);
    if (a <= 1)
        return a;
    return MOD((1 - inverse(m, a) * m) / a, m);
}

const ll maxn = 20010;
ll fac[maxn],inv[maxn];
void init(){
    fac[0] = 1;
    REP(i,1,maxn)fac[i] = i*(fac[i-1])%mod;
    inv[maxn-1]=inverse(fac[maxn-1], mod);
    RREP(i,maxn-2,0){
        inv[i] = inv[i+1]*(i+1)%mod;
    }
}

ll combi(int n,int m){
    if(n<0 || m<0 || n<m)return 0;
    return fac[n]*inv[m]%mod*inv[n-m]%mod;
}

ll dp[20001],valid[20010];
int main() {
#ifndef ONLINE_JUDGE
    fr("/Users/zhangqingchuan/Desktop/cp/cp/input.txt");
    fw("/Users/zhangqingchuan/Desktop/cp/cp/output.txt");
#endif
    init();
    int n;in(n);
    valid[0] = 1;
    REP(i,1,n+1){
        REP(j,1,i+1){
            valid[i]+=combi(i-1, j-1)*valid[i-j]%mod;
            valid[i]%=mod;
        }
    }
    REP(i,1,n+1){
        dp[i] = dp[i-1]+valid[i-1];
        dbg(dp[i]);
        REP(j,1,i+1){
            dp[i]+=combi(i-1, j-1)*dp[i-j]%mod;
        }
        dp[i]%=mod;
    }
    cout<<dp[n]<<endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int a[50010], n, b[50010], t;
int check(long long x)
{
	int i;
	for (i = 0; i < n; i++)
	{
		long long y = (x - i) / n + bool((x - i) % n);
		long long z;
		if (b[i] == 0) z = 0; else z = (t + x - b[i]) / t;
		if (y - z >= a[i]) return i + 1;
	}
	return 0;
}
int main()
{
	int i;
	scanf("%d", &n);
	for (i = 0; i < n; i++) scanf("%d", &a[i]);
	int x = n - 1;
	for (i = 1; i <= n; i++)
	{
		if (b[(x + 7) % n]) break;
		b[(x + 7) % n] = i * 7;
		x = (x + 7) % n;
	}
	t = 7 * (i - 1);
	long long l = 1, r = 1e14, m, ans;
	while (l <= r)
	{
		m = (l + r) / 2;
		int z = check(m);
		if (!z) l = m + 1; else
		{
			ans = z;
			r = m - 1;
		}
	}
	printf("%d\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

template<typename C, typename R = C>
struct dinic
{
	typedef C flow_type;
	typedef R result_type;

	static_assert(std::is_arithmetic<flow_type>::value, "flow_type must be arithmetic");
	static_assert(std::is_arithmetic<result_type>::value, "result_type must be arithmetic");

	static const flow_type oo = std::numeric_limits<flow_type>::max();

	struct edge
	{
		//int src; // not needed, can be deleted to save memory
		int dst;
		int rev;
		flow_type cap, flowp;

		edge(int src, int dst, int rev, flow_type cap, int flowp) :
			/*src(src),*/ dst(dst), rev(rev), cap(cap), flowp(flowp) {}
	};

	dinic(int n) : adj(n), que(n), level(n), edge_pos(n), flow_id(0) {}

	int add_edge(int src, int dst, flow_type cap, flow_type rcap = 0)
	{
		adj[src].emplace_back(src, dst, (int) adj[dst].size(), cap, flow_id++);
		if (src == dst) adj[src].back().rev++;
		adj[dst].emplace_back(dst, src,  (int) adj[src].size() - 1, rcap, flow_id++);
		return (int) adj[src].size() - 1 - (src == dst);
	}

	inline bool side_of_S(int u) { return level[u] == -1; }

	result_type max_flow(int source, int sink, vector<flow_type> &flow_e)
	{
		result_type flow = 0;
		while (true)
		{
			int front = 0, back = 0;
			std::fill(level.begin(), level.end(), -1);
			for (level[que[back++] = sink] = 0; front < back && level[source] == -1; ++front)
			{
				int u = que[front];
				for (const edge &e : adj[u])
					if (level[e.dst] == -1 && flow_e[rev(e).flowp] < rev(e).cap)
						level[que[back++] = e.dst] = 1 + level[u];
			}
			if (level[source] == -1)
				break;
			std::fill(edge_pos.begin(), edge_pos.end(), 0);
			std::function<flow_type(int, flow_type)> find_path = [&](int from, flow_type res)
			{
				if (from == sink)
					return res;
				for (int &ept = edge_pos[from]; ept < (int) adj[from].size(); ++ept)
				{
					edge &e = adj[from][ept];
					if (flow_e[e.flowp] == e.cap || level[e.dst] + 1 != level[from]) continue;
					flow_type push = find_path(e.dst, std::min(res, e.cap - flow_e[e.flowp]));
					if (push > 0)
					{
						flow_e[e.flowp] += push;
						flow_e[rev(e).flowp] -= push;
						if (flow_e[e.flowp] == e.cap)
							++ept;
						return push;
					}
				}
				return static_cast<flow_type>(0);
			};
			for (flow_type f; (f = find_path(source, oo)) > 0;)
				flow += f;
		}
		return flow;
	}

	result_type max_flow2(int source, int sink, vector<flow_type> &flow_e)
	{
		result_type flow = 0;
		std::function<flow_type(int, flow_type)> find_path = [&](int from, flow_type res)
		{
			level[from] = 1;
			if (from == sink)
				return res;
			for (int &ept = edge_pos[from]; ept < (int) adj[from].size(); ++ept)
			{
				edge &e = adj[from][ept];
				if (level[e.dst] == 1 || flow_e[e.flowp] == e.cap) continue;
				flow_type push = find_path(e.dst, std::min(res, e.cap - flow_e[e.flowp]));
				if (push > 0)
				{
					flow_e[e.flowp] += push;
					flow_e[rev(e).flowp] -= push;
					if (flow_e[e.flowp] == e.cap)
						++ept;
					return push;
				}
			}
			return static_cast<flow_type>(0);
		};

		for (bool ok = true; ok; )
		{
			int it = 0;
			std::fill(edge_pos.begin(), edge_pos.end(), 0);
			for (flow_type f; ; ++it)
			{
				std::fill(level.begin(), level.end(), -1);
				f = find_path(source, oo);
				if (f == 0)
				{
					if (it == 0) ok = false;
					break;
				}
				flow += f;
			}
		}
		return flow;
	}

	int flow_id;
private:
	std::vector<std::vector<edge>> adj;
	std::vector<int> que;
	std::vector<int> level;
	std::vector<int> edge_pos;

	inline edge& rev(const edge &e) { return adj[e.dst][e.rev]; }
};

const int inf = 25;

struct edge
{
	int u, v, w;
};

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

void relabel(int n, vector<edge> &e, int k)
{
	shuffle(e.begin() + k, e.end(), rng);

	vector<vector<pair<int, int>>> adj(n);
	for (auto &i : e)
		adj[i.u].push_back({ i.v, &i-&e[0] });

	vector<edge> ne = e;
	vector<int> id(n, -1);
	id[0] = 0;
	id[n-1] = n-1;
	int sz = 1, esz = k;

	function<void(int)> dfs = [&](int u)
	{
		for (auto &v : adj[u])
		{
			if (v.second >= k)
			{
				ne[esz++] = e[v.second];
				v.second = -1;
			}
			if (id[v.first] == -1)
			{
				id[v.first] = sz++;
				dfs(v.first);
			}
		}
	};
	dfs(0);

	for (auto &u : adj)
		for (auto v : u)
			if (v.second >= k)
				ne[esz++] = e[v.second];

	for (int i = 0; i < n; ++i)
		if (id[i] == -1)
			id[i] = sz++;

	e = ne;
	for (auto &i : e)
	{
		i.u = id[i.u];
		i.v = id[i.v];
	}
}

struct masks
{
	int x, cost;
	vector<int> flow_e;

	bool operator<(const masks &o) const
	{
		return cost < o.cost;
	}
};

typedef std::chrono::_V2::system_clock::time_point timepoint;

timepoint get_time()
{
	return std::chrono::high_resolution_clock::now();
}

int get_elapsed(timepoint t)
{
	return std::chrono::duration_cast<std::chrono::milliseconds>(get_time() - t).count();
}

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

	int n, m, k, q;
	cin >> n >> m >> k >> q;
	vector<edge> e(m);
	for (auto &i : e) cin >> i.u >> i.v >> i.w, --i.u, --i.v;

	vector<int> mask(1 << k, -1);

	relabel(n, e, k);
	dinic<int> d(n);
	for (int i = 0; i < m; ++i)
		d.add_edge(e[i].u, e[i].v, ((i < k) ? inf : e[i].w));

	vector<int> flow_e(d.flow_id);
	for (int i = 0; i < k; ++i)
		flow_e[2*i] = inf;

	mask[0] = d.max_flow(0, n-1, flow_e);

	priority_queue<masks> pq;
	pq.push({ 0, 0, flow_e });
	while (!pq.empty())
	{
		int x = pq.top().x;
		flow_e = pq.top().flow_e;
		pq.pop();

		for (int j = 0; j < k; ++j)
			if ((~x >> j & 1) && mask[x | 1 << j] == -1)
			{
				auto n_flow_e = flow_e;
				n_flow_e[2 * j] = 0;
				auto t = get_time();
				mask[x | 1 << j] = mask[x] + d.max_flow2(0, n-1, n_flow_e);
				pq.push({ x | 1 << j, get_elapsed(t), n_flow_e });
			}
	}

	vector<int> cut(1 << k), bit(1 << k);
	for (int i = 1; i < 1 << k; ++i)
		bit[i] = i & -i;

	const int U = (1 << k) - 1;
	while (q--)
	{
		for (int i = 0; i < k; ++i)
			cin >> cut[1 << i];

		int ans = mask[U];
		for (int i = 1; i < 1<<k; ++i)
		{
			cut[i] = cut[i ^ bit[i]] + cut[bit[i]];
			ans = min(ans, cut[i] + mask[U ^ i]);
		}
		cout << ans << "\n";
	}

	return 0;
}


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

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

    }
    sort(arr,arr+m);
    int a=arr[n-1]-arr[0];
    for(int i=1;i<=m-n;i++)
    {
         a=min(a,arr[n+i-1]-arr[i]);

    }
    cout<<a;
}


#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

#include <vector>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <cassert>
#include <bitset>
typedef long long ll ;
#define fi first
#define se second
#define mp make_pair
#define rep(i, a, b) for (int i = a; i < b; ++ i)
const int N = 1e5 + 5, M = 1e5 ;
using namespace std ;
typedef pair<int,pair<ll,int> > state;

ll myrand() {
	return ((ll)(rand()&65535)<<32)+((ll)rand()<<16)+rand();
}
state st1[N],st2[N];
int vis[N],dis[N],off,pre[N],n,deg[N],deg2[N],u,v,ret[N];
vector<int> e[N],e2[N];
bitset<10100> adj[10100];
set<state> hs;
pair<int,int> E[N];

void del(int u) {
	off+=1;
	dis[u]=1;
	hs.erase(st1[u]);
	for (auto v:e[u]) if (!dis[v]) {
		hs.erase(st1[v]);
		st1[v].fi++;
		hs.insert(st1[v]);
	}
}

bool gao0() {
	rep(i,1,n+1) pre[i]=i;
	random_shuffle(pre+1,pre+n+1);
	rep(i,1,n) if (adj[pre[E[i].fi]][pre[E[i].se]]) return 0;
	rep(i,1,n+1) ret[pre[i]]=i;
	puts("Yes");
	rep(i,1,n+1) printf("%d ",ret[i]+n);
	puts("");
	return 1;
}
bool gao() {
	hs.clear(); off=0;
	rep(i,1,n+1) {
		st1[i]=mp(deg[i],mp(myrand(),i));
		st2[i]=mp(deg2[i],mp(myrand(),i));
		hs.insert(st1[i]);
		vis[i]=0; dis[i]=0;
	}

	rep(i,1,n+1) pre[i]=0;

	state val=st2[1];
	rep(i,2,n+1) val=min(val,st2[i]);

	priority_queue<state, vector<state>, greater<state> > q;

	q.push(val);
	pre[val.se.se]=hs.begin()->se.se;
	vis[val.se.se]=1;

	del(hs.begin()->se.se);
	while (!q.empty()) {
		state st=q.top(); q.pop();
		int u=st.se.se;
		int w=pre[u];
		for (auto v:e2[u]) if (!vis[v]) {
			st2[v].fi--; vis[v]=1;
			vector<state> tmp;
			while (1) {
				auto it=hs.lower_bound(mp(st2[v].fi+off,mp(-1,-1)));
				if (it==hs.end()) return 0;
				if (adj[w][it->se.se]) {
					tmp.push_back(*it);
					hs.erase(it);
				} else {
					pre[v]=it->se.se;
					for (auto p:tmp) hs.insert(p);
					del(it->se.se);
					q.push(st2[v]);
					break;
				}
			}
		}

	}
	rep(i,1,n+1) ret[pre[i]]=i;
	puts("Yes");
	rep(i,1,n+1) printf("%d ",ret[i]+n);
	puts("");
	return 1;
}

int main() {
	scanf("%d",&n);
	rep(i,1,n+1) deg[i]=n-1;
	rep(i,1,n) {
		scanf("%d%d",&u,&v);
		e[u].push_back(v),e[v].push_back(u);
		adj[u][v]=adj[v][u]=1;
		deg[u]--; deg[v]--;
	}
	rep(i,1,n) {
		scanf("%d%d",&u,&v); u-=n; v-=n;
		e2[u].push_back(v); e2[v].push_back(u);
		E[i]=make_pair(u,v);
		deg2[u]++; deg2[v]++;
	}
	rep(i,1,n+1) if (deg[i]==0||deg2[i]==n-1) {
		puts("No");
		return 0;
	}
	while (1) {
		if (gao()) return 0;
		if (gao0()) return 0;
	}
	puts("No");
}


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

int main(){

    int n,cnt = 0;
    cin >> n;
    string str;
    cin >> str;
    for(int i = 0; i < str.size()-1; i++){
        while(str[i]==str[i+1]){
            cnt++;
            i++;
        }
    }
    cout << cnt << endl;
    return 0;
}


// New Year and North Pole.cpp : main project file.

#include <iostream>
#include <string>

using namespace std;

int main()
{
	long int a = 0, b, c = 0, d=0, n,f=0;
	string s1;
	cin >> n;
	for (long int i = 1; i <= n; i++)
	{
		a = 0;
		cin >> a;
		cin >> s1;
		if (d == 0)
		{
			if (s1 != "South" )
			{
				f = 1;
			}
			else
			{
				if (a > 20000)
				{
					f = 1;
				}
				else
				d = a;
			}

		}
		else
		{
		if (d == 20000)
		{

			if (s1 != "North")
			{
				f = 1;
			}
			else
			{

				if (a > 20000)
				{
					f = 1;
				}
				else
				{
					d = 20000 - a;
				}

			}

		}
		else
		{
			if (s1 == "North")
			{
				d -= a;
				if (d < 0)
					f = 1;
			}
			if (s1 == "South")
			{
				d += a;

				if (d > 20000)
					f = 1;
			}

		}

		}
	}
	if (d == 0 && f==0)
	{
		cout << "YES";
	}
	else
	{
		cout << "NO";

	}

	return 0;
}


/*
chirag11032000
Chirag Thakur
*/

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

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using pll = pair < ll, ll >;

template < typename T > using OrderedSet = tree < T, null_type, less < T >, rb_tree_tag, tree_order_statistics_node_update >;
template < typename T > using MinPriorityQueue = priority_queue < T, vector < T >, greater < T > >;

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template < typename Arg1 >
void __f(const char* name, Arg1&& arg1) {
	cerr << name << " : " << arg1 << std :: endl;
}
template < typename Arg1, typename... Args >
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');
	cerr.write(names, comma - names) << " : " << arg1 << " | ";
	__f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fastIo() ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)

const db PI = acos(-1);
const ll LINF = LLONG_MAX;
const int MOD = 1e9 + 7, INF = INT_MAX, N = 1e5 + 10;

void testCase();

int main() {
	fastIo();
	testCase();
	cerr << "Time taken: " << int((clock() * 1000.) / CLOCKS_PER_SEC) << "ms\n";
	return 0;
}

void testCase() {
	int a, b;
	cin >> a >> b;
	a <<= 1;
	b <<= 1;
	int x1, y1, x2, y2;
	cin >> x1 >> y1 >> x2 >> y2;
	auto up = [&](int x, int y) {
		return int(ceil(db(y - x) / b));
	};
	auto down = [&](int x, int y) {
		return int(floor(db(y - x) / b));
	};
	auto left = [&](int x, int y) {
		return int(floor(db(y + x) / a));
	};
	auto right = [&](int x, int y) {
		return int(ceil(db(y + x) / a));
	};
	int u = max(up(x1, y1), up(x2, y2));
	int d = min(down(x1, y1), down(x2, y2));
	int l = min(left(x1, y1), left(x2, y2));
	int r = max(right(x1, y1), right(x2, y2));
	cout << max((u - d - 1), (r - l - 1)) << "\n";
}


#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
long long firbit(long long  x)
{
  long long res=0;
  long long ss=x;
  int arr[50]={};
  int k=0;
  while(ss>0)
  {
    arr[k]=ss%2;
    ss/=2;
    k++;
  }
/*  for(int ii=0;ii<50;ii++)
  {
    cout<<arr[ii]<<" ";
  }
  cout<<endl;*/
  for(int i=0;i<=k;i++)
  {
    if(arr[i])
    {
      res=pow(2,i);
      break;
    }
  }
  return res;
}
long long sum(long long x)
{
  int ss=0;
  while(x!=0)
  {
    ss+=x%10;
    x/=10;
  }
  return ss;
}

int main()
{

long long n;
cin>>n;
double x;
long long xx;
long long res=-1;
long long bb;
for(int i=1;i<=100;i++)
{
  x=sqrt(i*i+4*n);
  xx=x;

  if(xx==x)
  {
    bb=(x-i)/2;
    if(i==sum(bb))
    {
      res=bb;
      break;
    }
  //  cout<<i;
  }
}
cout<<res;
    return 0;
}


#include <iostream>
using namespace std;
int main()
{
    int a, x, y;
    int sum;
    cin>>a>>x>>y;
    bool flag = true;
    if(y % a == 0 || x < -a || x > a)
        flag = false;
    else
    {
        int step = y / a + 1;
        if(step == 1 || step == 2)
        {
            if(x >= a / 2.0 || x <= -a / 2.0)
                flag = false;
            else
                sum = step;
        }
        else
        {

            int num1 = step / 2;
            int num2 = step - num1;
            sum = num1 * 2 + num2 - 1;
            if(step % 2 == 0)
            {
                if(x <= -a / 2.0 || x >= a / 2.0)
                    flag = false;
            }
            else
            {
                if(x <= -a || x >= a || !x)
                    flag = false;
                if(x > 0)
                    sum++;
            }

        }
    }
    cout<< (flag? sum: -1)<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<ll> vi;
typedef vector<ll> vll;
#define FOR(i,n) for (i = 0; i < n; ++i)
#define FORK(i,k,n) for (i = k; i <= n; ++i)
#define FORR(i,k,n) for (i = k; i >= n; --i)

#define re(a,b)   memset(a,b,sizeof(a))
#define sz(a)      (int)(a.size())
#define MIN(a,b)     (a) = min((a),(b))
#define MAX(a,b)     (a) = max((a),(b))
#define input(in)    freopen(in,"r",stdin)
#define output(out)  freopen(out,"w",stdout)
#define ALL(a)       a.begin(),a.end()
#define RALL(a)      a.rbegin(),a.rend()
#define LEN(a)       (int)(a.length())

#define FIN(x)       freopen(x,"r",stdin)
#define FOUT(x)      freopen(x,"w",stdout)
#define FCLOSE       {fclose(stdin); fclose(stdout);}

#define fi           first
#define se           second
#define pb           push_back
#define mp           make_pair
#define  M           100001
#define INF          90001001001
int main()
{
    ll n,k,a,j=1,m,f;
    cin >> n >> k;
    a=n*k;
    ll ans=INF;
    while(j<k)
    {
        if(a%j==0)
        {
            f=(a/j);
            f+=j;
        //    cout << j << " " << a/j << " " <<f << "\n";
            if((f%k)*(f/k)==n)
            ans=min(ans,f);
        }
        j++;
    }
    cout << ans;
}


#include<bits/stdc++.h>
#define re register
#define ui unsigned int
const double pi=3.14159265358979323846264338327950;
int x1[100100],x2[100100],qq[200100];
const int maxn=(1<<20)+1;
struct com
{
	double a,b;
	inline com operator +(const com&A){return(com){a+A.a,b+A.b};}
	inline void operator +=(const com&A){a+=A.a,b+=A.b;}
	inline com operator -(const com&A){return(com){a-A.a,b-A.b};}
	inline com operator *(const com&A){return(com){a*A.a-b*A.b,a*A.b+b*A.a};}
	inline com operator *(re const double &o) {return (com){a*o,b*o};}
	inline com operator /(const int&A){return(com){a/A,b/A};}
	inline com operator !(){return(com){a,-b};}
	inline bool operator !=(const com&A){return a!=A.a||b!=A.b;}
};
com w[maxn],T1[maxn];
int getlen(re int n){re int x=1;for(;x<n;x*=2);return x;}
bool cp[maxn<<1];
void init(re int len)
{
	cp[1]=1;
	for(re int i=2;i<len;i++)
	{
		cp[i]=(cp[i>>1]&(!(i&1)))||(cp[i>>2]&((i>>1)&1));
	}
}
void fft(com*a,re int len)
{
	re ui i1=0;
	re com x1,x2,x3;
	for(re ui i=0,j=0;i<len;i++)
	{
		if(i<j)std::swap(a[i],a[j]);
		for(ui k=len>>1;;k>>=1)if((j^=k)>=k)break;
	}w[0]=(com){1,0};
	if(len>=2)
	{
		for(re ui i=0;i<len;i+=2)if(cp[(len+i)>>1])x1=a[i+1],a[i+1]=a[i]-x1,a[i]+=x1;
	}
	if(len>=4)
	for(re ui i=0;i<len;i+=4)
		if(cp[(len+i)>>2])
		{
			x1=a[i+2];
			x2=a[i+3];
			x3=(com){-x1.b+x2.b,-x2.a+x1.a};x1+=x2;
			a[i+2]=a[i]-x1;a[i]+=x1;
			a[i+3]=a[i+1]-x3;a[i+1]+=x3;
		}
	w[1]=(com){0,1};w[2]=(com){-1,0};
	for(re ui i=8,i2=3;i<=len;i<<=1,i2++)
	{
		i1=i>>2;
		com s=(com){cos(pi/i1/2),sin(pi/i1/2)};
		for(re ui j=3*i1-2;j>0;j-=2)
			w[j]=w[j>>1];
		for(re ui j=1;j<3*i1;j+=2)
			w[j]=s*w[j-1];
		for(re ui j=0;j<len;j+=i)
		if(cp[(len+j)>>i2]){re com*aa=a+j,*aaa=aa+i1,*bb=aaa+i1,*cc=bb+i1;
			for(re int k=0;k<i1;k++)
			{
				x1=w[k]*bb[k];
				x2=w[k*3]*cc[k];
				x3=(com){-x1.b+x2.b,-x2.a+x1.a};
				cc[k]=aaa[k]-x3;
				aaa[k]+=x3;
				x1+=x2;
				bb[k]=aa[k]-x1;
				aa[k]+=x1;
			}
		}
	}
}
void DFT(ui *a,com *R1,int len) {
	for(re int i=0;i<len;++i) R1[i]=(com){(double)a[i<<1],(double)a[i<<1|1]};
	fft(R1,len);
}
void DFTMul(com *R1,com *S1,int len) {
	for(re int i=0;i<len;++i) {
		re int j=len-1&len-i;
		com tmp=(i&len>>1)?(com){1,0}-w[i^len>>1]:w[i]+(com){1,0};
		T1[j]=R1[i]*S1[i]-(R1[i]-!R1[j])*(S1[i]-!S1[j])*tmp*0.25;
	}
}
void IDFT(long long *__ans,int len) {
	fft(T1,len);
	for(re int i=0;i<len*2;++i) __ans[i]=(i&1)?(long long)(T1[i>>1].b/len+0.5):(long long)(T1[i>>1].a/len+0.5);
}
ui xx1[270100],xx2[270100];
com xr1[200101],xr2[200101];
long long ans[270100],res[270100];
void Mul(ui*x1,ui*x2,long long*__ans,int len)
{
	DFT(x1,xr1,len);
	DFT(x2,xr2,len);
	DFTMul(xr1,xr2,len);
	IDFT(__ans,len);
}
int main()
{
	init(131072);
	re int n,m,q,len=131072;
	scanf("%d%d%d",&n,&m,&q);
	for(re int i=1;i<=n;i++)scanf("%d",&x1[i]);
	for(re int i=1;i<=m;i++)scanf("%d",&x2[i]);
	for(re int i=1;i<=q;i++)scanf("%d",&qq[i]);
	for(re int i=1;i<n;i++)
	{
		xx1[std::min(x1[i],x1[i+1])]++;
	}
	for(re int i=1;i<m;i++)
	{
		xx2[std::min(x2[i],x2[i+1])]++;
	}
	Mul(xx1,xx2,res,len);
	for(re int i=0;i<len*2;i++)ans[i]+=2*res[i],res[i]=0;
	memset(xx2,0,sizeof(xx2));
	for(re int i=1;i<=m;i++)
	{
		xx2[x2[i]]++;
	}
	Mul(xx1,xx2,res,len);
	for(re int i=0;i<len*2;i++)ans[i]-=2*res[i],res[i]=0;
	memset(xx1,0,sizeof(xx1));
	for(re int i=1;i<=n;i++)
	{
		xx1[x1[i]]++;
	}
	Mul(xx1,xx2,res,len);
	for(re int i=0;i<len*2;i++)ans[i]+=2*res[i],res[i]=0;
	memset(xx2,0,sizeof(xx2));
	for(re int i=1;i<m;i++)
	{
		xx2[std::min(x2[i],x2[i+1])]++;
	}
	Mul(xx1,xx2,res,len);
	for(re int i=0;i<len*2;i++)ans[i]-=2*res[i],res[i]=0;
	re int mi=1<<30;
	for(re int i=1;i<=m;i++)
	{
		ans[x1[1]+x2[i]]--;
		if(n!=1)ans[x1[n]+x2[i]]--;
		mi=std::min(mi,x1[1]+x2[i]);
		mi=std::min(mi,x1[n]+x2[i]);
		if(i!=1)
		{
			ans[x1[1]+std::min(x2[i-1],x2[i])]++;
			if(n!=1)ans[x1[n]+std::min(x2[i-1],x2[i])]++;
		}
	}
	for(re int i=2;i<=n;i++)
	{
		if(i!=n)
		{
			ans[x1[i]+x2[1]]--;
			if(m!=1)ans[x1[i]+x2[m]]--;
		}
		mi=std::min(mi,x1[i]+x2[1]);
		mi=std::min(mi,x1[i]+x2[m]);
		ans[std::min(x1[i-1],x1[i])+x2[1]]++;
		if(m!=1)ans[std::min(x1[i-1],x1[i])+x2[m]]++;
	}
	if(n==1||m==1)
	{
		for(re int i=1;i<=m;i++)
		{
			ans[x1[1]+x2[i]]--;
			if(n!=1)ans[x1[n]+x2[i]]--;
			mi=std::min(mi,x1[1]+x2[i]);
			mi=std::min(mi,x1[n]+x2[i]);
			if(i!=1)
			{
				ans[x1[1]+std::min(x2[i-1],x2[i])]++;
				if(n!=1)ans[x1[n]+std::min(x2[i-1],x2[i])]++;
			}
		}
		for(re int i=2;i<=n;i++)
		{
			if(i!=n)
			{
				ans[x1[i]+x2[1]]--;
				if(m!=1)ans[x1[i]+x2[m]]--;
			}
			mi=std::min(mi,x1[i]+x2[1]);
			mi=std::min(mi,x1[i]+x2[m]);
			ans[std::min(x1[i-1],x1[i])+x2[1]]++;
			if(m!=1)ans[std::min(x1[i-1],x1[i])+x2[m]]++;
		}
		ans[x1[1]+x2[1]]++;
		if(m==1)ans[x1[n]+x2[1]]++;
		else ans[x1[1]+x2[m]]++;
	}
	for(re int i=len*2;i>=0;i--)ans[i]+=ans[i+1];
	assert(ans[0]==2);
	for(re int i=1;i<=q;i++)printf("%lld\n",ans[qq[i]]-1);
}


#include <iostream>
#include <algorithm>
using namespace std;
long long buy[101][1010], sell[101][1100], lim[101][1100];
pair<long long, long long> tmp[1100];
int main(){
    long long n, m, k, best = -1;
    cin>>n>>m>>k;
    string junk;
    for(long long x = 0;x<n;x++){
        cin>>junk;
        for(long long y = 0;y<m;y++)
            cin>>buy[x][y]>>sell[x][y]>>lim[x][y];
    }
    for(long long x = 0;x<n;x++){
        for(long long y = 0;y<n;y++){
            if(x == y)
                continue;
            for(long long z = 0;z<m;z++)
                tmp[z] = {sell[y][z] - buy[x][z], lim[x][z]};
            sort(tmp, tmp + m);
            long long profit = 0;
            for(long long z = m - 1, space = k;z>=0;z--){
                if(tmp[z].first < 0)
                    break;
                if(space >= tmp[z].second){
                    space -= tmp[z].second;
                    profit += tmp[z].first*tmp[z].second;
                }else{
                    profit += tmp[z].first*space;
                    break;
                }
            }
            best = max(best, profit);
        }
    }
    cout<<best<<'\n';
    return 0;
}


#include <iostream>
#include <set>
#include <string>

using namespace std;

struct Cell
{
	int x; int y;
	Cell(int x = 0, int y = 0) : x(x), y(y) {}
	friend bool operator < (Cell c1, Cell c2)
	{
		if (c1.x == c2.x) return c1.y < c2.y;
		return c1.x < c2.x;
	}
	friend bool operator == (Cell c1, Cell c2)
	{
		return (c1.x == c2.x && c1.y == c2.y);
	}
	friend bool operator != (Cell c1, Cell c2)
	{
		return (c1.x != c2.x || c1.y != c2.y);
	}
};

set<Cell> sc;
int step[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};

bool find(Cell c, Cell pc)
{
	for (int i = 0; i < 4; ++i)
		if ((Cell(c.x+step[i][0], c.y+step[i][1]) != pc))
			if (sc.find(Cell(c.x+step[i][0], c.y+step[i][1])) != sc.end())
				return true;
	if (sc.find(c) != sc.end())
		return true;
	return false;
}

int main()
{
	Cell cur, pcur;
	sc.insert(cur);
	string s;
	cin >> s;
	for (int i = 0; i < s.size(); ++i)
	{
		pcur = cur;
		switch(s[i])
		{
		case 'U':
			{
				++cur.y;
			}break;
		case 'D':
			{
				--cur.y;
			}break;
		case 'L':
			{
				--cur.x;
			}break;
		case 'R':
			{
				++cur.x;
			}break;
		}
		if (!find(cur, pcur))
			sc.insert(cur);
		else
		{
			cout << "BUG" << endl;
			return 0;
		}
	}

	cout << "OK" << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int32_t main() {

    ios_base::sync_with_stdio(false);

    cin.tie(0);

    long long n;

    cin >> n;

    long long res=0;

    long long mul=0;

    while(n>0) {

        if(n-mul>0)

            res+=(n-mul);

        n--;

        mul++;

    }

    cout << res << '\n';

    return 0;

}


#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
#define REP(i,a,b) for(ll i=a; i<b; i++)
#define pb push_back
#define mp make_pair
#define pl pair<ll,ll>
#define ff first
#define ss second
#define INF 100000000000000000LL
ll insig;
#define In(vecBRO, LENBRO) REP(IBRO,0,LENBRO) {cin>>insig; vecBRO.pb(insig);}
void Out(vector<ll> x) {REP(i,0,x.size()) {cout<<x[i]<<" ";} cout<<endl;}
ll mod=998244353LL;

ll fastexp(ll a,ll e, ll mod)
{
    a%=mod;
    if(e==0) {return 1LL;}
    if(e%2LL==0)
    {
        ll v=fastexp(a,(ll) e/2LL,mod); return (v*v)%mod;
    }
    else
    {
        ll v=fastexp(a,(ll) e/2LL,mod); return (((v*v)%mod)*a)%mod;
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll N,M,L,R; cin>>N>>M>>L>>R;
    if(N%2==1 && M%2==1) {ll ans=fastexp((R-L+1LL),N*M,mod); if(ans<0LL) {ans+=mod;} cout<<ans<<endl; return 0;}
    ll A,B,x,y;
    if((R-L)%2LL==0LL)
    {
        if(R%2LL==0LL) {x=(R-L)/2LL; y=x; x++;}
        if(R%2LL!=0LL) {x=(R-L)/2LL; y=x; y++;}
    }
    else
    {
        x=(R-L+1LL)/2LL; y=x;
    }
    A=(N*M+1LL)/2LL; B=N*M-A;
    ll s=x+y; ll d=x-y;
    ll sA=fastexp(s,A,2LL*mod);
    ll sB=fastexp(s,B,2LL*mod);
    ll dA=fastexp(d,A,2LL*mod);
    ll dB=fastexp(d,B,2LL*mod);
    ll ans=((sA+dA)/2LL)*((sB+dB)/2LL) + ((sA-dA)/2LL)*((sB-dB)/2LL);
    if(d==0LL) {ans=(sA/2LL)*sB;}
    ans%=mod; if(ans<0LL) {ans+=mod;}
    cout<<ans<<endl;
    return 0;
}


   /** Author :- Kumar Vijyant From MNNIT **/

    #include<bits/stdc++.h>
    using namespace std;
    #define sz(a) (int)(a.size())
    #define all(a) a.begin(),a.end()
    typedef unsigned long long llu;
    typedef  long long ll;
    typedef  vector<ll> vll;
    typedef vector<int> vi;
    typedef vector<bool> vb;
    typedef vector<vector<int>>  vvi ;
    typedef  pair<int,int> pi;
    typedef vector<pair<int,int>> vpi ;
    #define F first
    #define S second
    #define pb push_back
    #define mp make_pair
    #define nl cout<<"\n";
    #define int ll
    #ifdef LOCAL
    #include "debug.h"
    #else
    #define debug(...) 42
    #endif
    const int mod=1e9+7;
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    template <typename A, typename B>istream& operator>>(istream& input,pair<A,B>& x) {input>>x.F>>x.S;return input;}
    template <typename A>istream& operator>>(istream& input,vector<A>& x) {for(auto& i:x)	input>>i;return input;}
    template<typename A>ostream& operator<<(ostream& output,vector<A>& x) {for(auto& i:x)	output<<i<<' ';	return output;}
    void add(int&,int );
     int sub(int ,int );
    int mul(int,int);
    int powz(int ,int );
    ll binpow(int , int );
    int gcd(int ,int);
    bool sortinrev(const pair<char,int>a,pair<char,int>b){ return (a.S<b.S);}
    /***************************************************main code**************************************************************/
    const int N=1e5+100;

    void solve()
    {
		int n ;

		cin >> n ;

		ll ans = ceil(n*1.0/2);

		cout << ans ;

    }
    /*************----------------------------******************/
    signed main(){
    	ios_base::sync_with_stdio(false);
    	cin.tie(0);
    	int tc=1;

    	//~ cin>>tc;
    	for(int _=0;_<tc;_++){
    		//~ cout<<"Case #"<<_+1<<": ";
    		solve();
    		if(_!=tc-1)
    		cout<<"\n";
    	}

    }
    ll binpow(int a, int b)
     {    ll res = 1;
    	 while (b > 0){
    		 if(b & 1)res = res*a;
    		  a = a *1ll*a;
    		   b >>= 1;
    	  }
       return res;
     }
    int powz(int a,int b)
    {
    	int res=1;
    	while(b)
    	{
    		if(b&1)res=mul(res,a);
    		b/=2;
    		a=mul(a,a);
    	}
    	return res;
    }
    int gcd(int a ,int b){
    	if(b ==0)return a;
    	return gcd(b,a%b);
    }
    int sub(int a,int b){
    	a-=b;
    	if(a<0){
    		a+=mod;
    	}
    	return a;
    }
    int mul(int a,int b)
    {
    	return ((a)*1ll*(b))%mod;
    }
    void add(int &a,int b)
    {
    	a+=b;
    	if(a>=mod)a-=mod;
    }


#include <cstdio>
typedef long long LL;
const int N=(1<<25) +10;
bool used[N];

int re(int a,int n){
	int ans=0;
	while(n--){
		ans<<=1;
		ans+=a&1;
		a>>=1;
	}
	return ans;
}
void print(int a,int n){
	a=re(a,n);
	while(n--){
		printf("%d",a&1);
		a>>=1;
	}
	return;
}
int M,pow;
void calc(LL k,int i){
	for(int j=0;j<i;j++){
		used[re(j,pow)]=true;
		used[re(M-j-1,pow)]=true;
	}
	int j;
	for(j=0;j<M;j++){
		if(!used[j])k--;
		if(k==0)break;
	}
	print(j,pow);
	return ;
}
int main(){
	int n;
	LL k;
//	print(re(7,4));
	while(~scanf("%d %I64d",&n,&k)){
		k++;
		pow=n/2;
		M=1<<pow;
		if(n%2==0){
			for(int i=0;i<M;i+=2){
				if(M-i<k)k-=M-i;
				else{
					print(i/2,pow);
					calc(k,i/2);
					k=0;break;
				}
			}
			if(k>0)puts("-1");
			else puts("");
		}
		else{
			for(int i=0;i<M;i+=2){
				if(M-i<k)k-=M-i;
				else{
					print(i/2,pow);
					printf("0");
					calc(k,i/2);
					k=0;break;
				}

				if(M-i-1<k)k-=M-i-1;
				else{
					used[re(M-1-i/2,pow)]=true;
					print(i/2,pow);
					printf("1");
					calc(k,i/2);
					k=0;break;
				}
			}
			if(k>0)puts("-1");
			else puts("");
		}
	}
}


#include<iostream>
#include<cstdio>
using namespace std;
const int MOD=998244353;
long long ksm(long long a,long long b)
{
	long long res=1;
	while(b)
	{
		if(b&1) res=res*a%MOD;
		a=a*a%MOD,b>>=1;
	}
	return res;
}
const int N=6005;
long long fac[N],inv[N];
void init(int n=6000)
{
	fac[0]=1;
	for(int i=1;i<=n;i++)
		fac[i]=fac[i-1]*i%MOD;
	inv[n]=ksm(fac[n],MOD-2);
	for(int i=n;i>=1;i--)
		inv[i-1]=inv[i]*i%MOD;
	return;
}
long long C(int n,int m)
{
	if(m>n) return 0;
	if(m==0||m==n) return 1;
	return fac[n]*inv[m]%MOD*inv[n-m]%MOD;
}
long long calc(int n,int m,int x)
{
	if(m==0)
	{
		if(n==0) return 1;
		else return 0;
	}
	long long res=0;
	for(int i=0;i<=m;i++)
	{
		long long sum=C(m,i)*C(n-x*i+m-1,m-1)%MOD;
		if(i&1) res=(res-sum+MOD)%MOD;
		else res=(res+sum)%MOD;
	}
	return res;
}
int p,s,r;
int main()
{
	init();
	scanf("%d%d%d",&p,&s,&r);
	if(p==1)
	{
		printf("1");
		return 0;
	}
	long long ans=0;
	for(int x=r;x<=s;x++)
	{
		if(x*p<s) continue;
		for(int i=1;i<=p;i++)
		{
			if(i*x>s) break;
			if((p-i)*(x-1)+i*x<s) continue;
			ans=(ans+C(p-1,i-1)*calc(s-x*i,p-i,x)%MOD*ksm(i,MOD-2)%MOD)%MOD;
		}
	}
	ans=ans*ksm(C(s-r+p-1,p-1),MOD-2)%MOD;
	printf("%lld",ans);
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int n,cnt,m=100000000;
int a[30000000];
int main(){
	cin>>n;
	int d;
	cin>>d;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++){
		for(int j=i;j<=n;j++){
			if(a[j]-a[i]<=d){
				m=min(m,n-j+i-1);
			}
		}
	}
	cout<<m;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;

#define rep(pos, len) for(int pos=0;pos<len;pos++)
#define repp(pos, len) for(int pos=1;pos<=len;pos++)

#define INF 1987654321
#define IINF 2987654321987654321
#define MOD 998244353

struct SegmentTree {
	int n;
	vector<int> mn, mx;

	void init(int sz) {
		n = 1;
		while(n < sz) n <<= 1;
		mn.assign(2*n, INF);
		mx.assign(2*n, -1);
	}

	void updateMin(int p, int val) {
		p += n-1;
		mn[p] = min(mn[p], val);
		for(p>>=1; p; p>>=1)
			mn[p] = min(mn[2*p], mn[2*p+1]);
	}

	void updateMax(int p, int val) {
		p += n-1;
		mx[p] = max(mx[p], val);
		for(p>>=1; p; p>>=1)
			mx[p] = max(mx[2*p], mx[2*p+1]);
	}

	int rangeMin(int l, int r) {
		int res = INF;
		for(l += n-1, r += n-1; l <= r; l>>=1, r>>=1) {
			if(l&1) res = min(res, mn[l++]);
			if(!(r&1)) res = min(res, mn[r--]);
		}
		return res;
	}

	int rangeMax(int l, int r) {
		int res = -1;
		for(l += n-1, r += n-1; l <= r; l>>=1, r>>=1) {
			if(l&1) res = max(res, mx[l++]);
			if(!(r&1)) res = max(res, mx[r--]);
		}
		return res;
	}

	int getMin(int p) {
		return mn[p+(n-1)];
	}

	int getMax(int p) {
		return mx[p+(n-1)];
	}
};

const int MAXN = 1e5 + 50;
const int LOGN = 18;

int r[MAXN];
int n;
void readInput() {
	scanf("%d", &n);
	repp(i, n) scanf("%d", &r[i]);
	repp(i, n) if(r[i] == n) r[i]--;
}

int findMin(int l, int r, SegmentTree &tree) {
	int res = INF;
	res = min(res, tree.rangeMin(max(l, 1), min(r, n)) - n);
	res = min(res, tree.rangeMin(max(l, n+1)-n, min(r, 2*n)-n));
	res = min(res, tree.rangeMin(max(l, 2*n+1)-2*n, min(r, 3*n)-2*n) + n);
	return res;
}

int findMax(int l, int r, SegmentTree &tree) {
	int res = -1;
	res = max(res, tree.rangeMax(max(l, 1), min(r, n)) - n);
	res = max(res, tree.rangeMax(max(l, n+1)-n, min(r, 2*n)-n));
	res = max(res, tree.rangeMax(max(l, 2*n+1)-2*n, min(r, 3*n)-2*n) + n);
	return res;
}

SegmentTree st[LOGN];

// [l, r] -> after 2^k operation
Pi findNext(int l, int r, int k) {
	int nl = findMin(l, r, st[k]);
	int nr = findMax(l, r, st[k]);
	if(nr-nl >= n-1) return {1, n};
	if(nl <= 0) {
		nl += n-1;
		nr += n-1;
	}
	return {nl, nr};
}

void findST() {
	rep(i, LOGN) st[i].init(n);
	repp(i, n) {
		st[0].updateMin(i, n+i-r[i]);
		st[0].updateMax(i, min(st[0].getMin(i)+n-1, n+i+r[i]));
	}

	for(int k = 1; k < LOGN; k++) {
		repp(i, n) {
			int cl = st[k-1].getMin(i), cr = st[k-1].getMax(i);
			Pi p = findNext(cl, cr, k-1);
			st[k].updateMin(i, p.first);
			st[k].updateMax(i, p.second);
		}
	}
}

int solve(int x) {
	int res = n-1, cur = 0;
	int cl = n+x, cr = n+x;
	for(int k = LOGN-1; k >= 0; k--) {
		Pi p = findNext(cl, cr, k);
		if(p.second - p.first >= n-1) {
			res = min(res, cur + (1 << k));
		} else {
			cur += (1 << k);
			cl = p.first;
			cr = p.second;
		}
	}
	return res;
}

int main() {
	readInput();
	findST();
	repp(i, n) printf("%d ", solve(i));

	/*
	cout << endl;
	for(int k = 0; k < 5; k++) {
		cout << k << "!" << endl;
		repp(i, n) printf("%d: (%d, %d)\n", i, st[k].getMin(i), st[k].getMax(i));
	}
	*/
}


#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define popb pop_back
#define all(A) A.begin(),A.end()
#define rall(A) A.rbegin(),A.rend()
#define dic unordered_map
#define bpc __builtin_popcountll//numero de bits para long long
#define bclz __builtin_clzll//leading zeros para ll
#define max_bit(A) 31-__builtin_clz(A)

using namespace std;
class circle{
public:
    int x,y,r;
    circle(){
    }
    circle(int _x,int _y,int _r){
        x=_x,y=_y,r=_r;
    }
    bool not_intersect(circle other1,circle other2){
        int left;
        if((r-other1.r)*(r-other2.r)<0) left=0;
        else{
            left=min(abs(r-other1.r),abs(r-other2.r));
        }
        int right=max(r+other1.r,r+other2.r);
        int dsq=(other1.x-x)*(other1.x-x)+(other1.y-y)*(other1.y-y);
        //cout<<a*a<<" "<<dsq<<" "<<b*b<<"\n";
        return dsq<=left*left || right*right<=dsq;
    }
};
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	vector<circle> ring[2];
	for(int t=0;t<2;t++){
        int x,y,r1,r2;
        cin>>x>>y>>r1>>r2;
        ring[t].pb(circle(x,y,r1));
        ring[t].pb(circle(x,y,r2));
	}
	int ans=0;
	for(int i=0;i<2;i++){
        for(int j=0;j<2;j++){
            if(ring[i][j].not_intersect(ring[(i+1)%2][0],ring[(i+1)%2][1])) ans++;
        }
	}
	cout<<ans<<"\n";
	return 0;
	// you should actually read the stuff at the bottom
}

/* stuff you should look for
	* int overflow, array bounds
	* bitwise and boolean operations
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/


#include<bits/stdc++.h>
using namespace std;
string s[8];
bool b[8][8];
int ans;
int main()
{
	for(int i=0;i<8;i++)cin>>s[i];
	for(int i=0;i<8;i++)
	{
		bool bb=1;
		for(int j=0;j<8;j++)if(s[i][j]=='W'){bb=0;break;}
		if(bb==0)continue;
		bb=0;
		for(int j=0;j<8;j++)if(!b[i][j]){bb=1;break;}
		if(bb)
		{
			for(int j=0;j<8;j++)if(s[i][j]=='B')b[i][j]=1;
			ans++;
		}
	}
	for(int i=0;i<8;i++)
	{
		bool bb=1;
		for(int j=0;j<8;j++)if(s[j][i]=='W'){bb=0;break;}
		if(bb==0)continue;
		bb=0;
		for(int j=0;j<8;j++)if(!b[j][i]){bb=1;break;}
		if(bb)
		{
			for(int j=0;j<8;j++)if(s[j][i]=='B')b[j][i]=1;
			ans++;
		}
	}
	printf("%d",ans);
}//HK is a part of China!!!


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;

	scanf("%d", &n);

	if(n <= 3){
		printf("%d\n", n + 1);
	}

	else{
		int d = sqrt(n);
		if(d * d == n){
			printf("%d\n", 2 * d);
		}
		else{
			int a = d + 1;
			if(n < d * d + a){
				printf("%d\n", a + d);
			}
			else{
				printf("%d\n", a + d + 1);
			}
		}
	}

	return 0;
}


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

   int a,b,c,k=0;
   cin>>a>>b>>c;
   if(a+b<=c)
   k+=c-(a+b)+1;
   if(b+c<=a)
   k+=a-(b+c)+1;
   if(a+c<=b)
   k+=b-(a+c)+1;
   cout<<k;
    return 0;
}


#include <bits/stdc++.h>

const int INF = 1e9;
int d[77][77][77][2];
int N;
std::vector<int> ch[111];

int nRemain(const std::vector<int>& list, int lb, int threshold) {
	int ret = 0;
	for (int i = lb; i < list.size() && list[i] < threshold; ++i) {
		ret += 1;
	}
	return ret;
}

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

	std::cin >> N;

	std::string S;
	std::cin >> S;
	for (int i = 0; i < N; ++i) {
		if (S[i] == 'V' || S[i] == 'K') {
			ch[S[i]].push_back(i);
		} else {
			ch['X'].push_back(i);
		}
	}

	for (int i = 0; i <= N; ++i) {
		for (int j = 0; j <= N; ++j) {
			for (int k = 0; k <= N; ++k) {
				for (int l = 0; l < 2; ++l) {
					d[i][j][k][l] = INF;
				}
			}
		}
	}

	d[0][0][0][0] = 0;
	for (int v = 0; v <= ch['V'].size(); ++v) {
		for (int k = 0; k <= ch['K'].size(); ++k) {
			for (int x = 0; x <= ch['X'].size(); ++x) {
				auto count = [&] (int pos) {
					return nRemain(ch['V'], v, pos) + nRemain(ch['K'], k, pos) +
						nRemain(ch['X'], x, pos);
				};
				for (int isLastV = 0; isLastV < 2; ++isLastV) {
					int oldValue = d[v][k][x][isLastV];
					if (v < ch['V'].size()) {
						d[v + 1][k][x][1] = std::min(d[v + 1][k][x][1],
								oldValue + count(ch['V'][v]));
					}
					if (k < ch['K'].size() && isLastV == 0) {
						d[v][k + 1][x][0] = std::min(d[v][k + 1][x][0],
								oldValue + count(ch['K'][k]));
					}
					if (x < ch['X'].size()) {
						d[v][k][x + 1][0] = std::min(d[v][k][x + 1][0],
								oldValue + count(ch['X'][x]));
					}
				}
			}
		}
	}

	int ans = INF;
	for (int isLastV = 0; isLastV < 2; ++isLastV) {
		int V = ch['V'].size();
		int K = ch['K'].size();
		int X = ch['X'].size();
		ans = std::min(ans, d[V][K][X][isLastV]);
	}
	std::cout << ans << '\n';
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>

#define N 100010
using namespace std;

int a[N],n,k,x,y,now;
double ans;
double dis(int z)
{
    return sqrt((double)(x-z)*(double)(x-z)+(double)(y)*(double)(y));
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    scanf("%d%d",&x,&y);
    now=a[k];
    sort(a+1,a+n+1);
    if(k==n+1)
    {
        ans=a[n]-a[1]+min(dis(a[1]),dis(a[n]));
        printf("%.8lf\n",ans);exit(0);
    }
    ans=a[n]-a[1]+min(dis(a[n])+abs(now-a[1]),dis(a[1])+abs(now-a[n]));
    for(int i=1;i<=n;i++)
    {
        if(i>1) ans=min(ans,a[i-1]-a[1]+min(dis(a[i-1])+abs(now-a[1]),dis(a[1])+abs(now-a[i-1]))+a[n]-a[i]+min(dis(a[i]),dis(a[n])));
        if(i<n) ans=min(ans,a[i]-a[1]+min(dis(a[1]),dis(a[i]))+a[n]-a[i+1]+min(dis(a[n])+abs(now-a[i+1]),dis(a[i+1])+abs(now-a[n])));
    }
    printf("%.8lf\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const int INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int, int> PII;
int n;
int a[N];

int lcm(int a, int b) {
	return a * b / __gcd(a, b) % mod;
}
int primes[N], cnt;     // primes[]存储所有素数
int euler[N];           // 存储每个数的欧拉函数
bool st[N];         // st[x]存储x是否被筛掉
void get_eulers(int n) {
	euler[1] = 1;
	for (int i = 2; i <= n; i ++ ) {
		if (!st[i]) {
			primes[cnt ++ ] = i;
			euler[i] = i - 1;
		}
		for (int j = 0; primes[j] <= n / i; j ++ ) {
			int t = primes[j] * i;
			st[t] = true;
			if (i % primes[j] == 0) {
				euler[t] = euler[i] * primes[j];
				break;
			}
			euler[t] = euler[i] * (primes[j] - 1);
		}
	}
}//筛法求欧拉函数
vector<int>fact[N];
void solve() {
	cin >> n;
	int res = 0;
	get_eulers(N);
	for (int i = 1; i < n - 1; i++) {
		for (int j = 1; j * j <= n - i; j++) {
			if ((n - i) % j == 0) {
				if((n-i)/j!=1) res = (res + lcm(i, j) * euler[(n - i) / j] % mod) % mod;
				if(j!=1&&j*j!=(n-i)) res = (res + lcm(i, (n-i)/j) * euler[j] % mod) % mod;
			}
		}
	}
	cout << res << endl;
}

signed main() {
	IOS;
	int t = 1;
//	cin >> t;
	for (int i = 1; i <= t; i++) {
		solve();
	}
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T> T sqr(T x) {return x*x;}
#define pi acos(-1)
#define INF 100000000
#define debug(x) cerr<<#x"="<<(x)<<"\n";
#define foreach(it,v) for (__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)

map<char,int> m;
int n;
string a[200];
char tmp[10];

int main()
{
	int i, j, x, y, ans, f;

	scanf("%d", &n);
	for (i = 1; i <= n;i++)
	{
		scanf("%s", tmp);
		a[i] = tmp;
	}
	m['R'] = 0;
	m['G'] = 1;
	m['B'] = 2;
	m['Y'] = 3;
	m['W'] = 4;
	m['1'] = 0;
	m['2'] = 1;
	m['3'] = 2;
	m['4'] = 3;
	m['5'] = 4;
	ans = 100;
	for (int i = 0; i < (1 << 5); i++)
		for (int j = 0; j < (1 << 5); j++)
		{
			f=1;
			for (int x = 1; x <= n; x++)
				for (int y = x + 1; y <= n; y++)
				    if (a[x] != a[y])
				    {
					    if (a[x][0] != a[y][0] && (((1 << m[a[x][0]]) & i) || ((1 << m[a[y][0]]) & i)))
					        continue;
					    if (a[x][1] != a[y][1] && (((1 << m[a[x][1]]) & j) || ((1 << m[a[y][1]]) & j)))
					        continue;
					    f = 0;
				    }
			if (f) ans = min(ans,__builtin_popcount(i)+__builtin_popcount(j));
		}
	printf("%d\n", ans);

	return 0;
}


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

using namespace std;

typedef long long ll;

struct node {
    int c, v;
};

const int MAX = 105;
const double eps = 1e18;

int l, d, v, g, r;

int main() {
    cin >> l >> d >> v >> g >> r;
    double t1, t2;
    t1 = 1.0 * d / v;
    t1 -= (int)(t1 / (g + r)) * (g + r);
    if (t1 >= 0 && t1 < g) t1 = 1.0 * l / v;
    else t1 = 1.0 * l / v + (g + r - t1);
    printf("%.6lf", t1);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,c=0;
    cin>>n;
    if(n==1)
    {
        cout<<1;
        return 0;
    }
    for(long long int i=1;1;i++)
    {
        if(i*(i+1)/2>=n)
        {
            n-=(i*(i-1))/2;
            c=i-1;
            break;
        }
    }
    cout<<n;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n1,n2,n3,n4,n5,n6,n7,n8,x,y,z;
int main() {
    cin>>n1>>n2>>n3>>n4>>n5>>n6>>n7>>n8;
    x=(n2*n3)/n7/n1;
    y=n4*n5/n1;
    z=n6/n8/n1;
    if(x<=y&&x<=z)
        cout<<x;
    else if(y<=z)
        cout<<y;
    else
        cout<<z;
    cout<<endl;
    return 0;
}


#include <iostream>
using  namespace std ;
int main()
{
	long long n ;
	cin>>n;
	if(n < 6 || n % 2 != 0)
	cout<< 0;
	else {
		if(n%4==0)
		cout<<n/4-1;
		else
		cout<<n/4;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
//s<<string;
//s>>x>>comma>>y;
//comma as char;
#define vi vector<int>
#define vl vector<long>
#define vll vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vf vector<float>
//#define vb vector<bool>
#define endl "\n"
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define MOD 1000000007
#define speedb ios_base::sync_with_stdio(0); cin.tie(0)

int main(){
    speedb;

    int n, a;
    vi m;
    cin>>n;
    for(int i =0 ; i < 12; i++){
		cin>>a;
		m.pb(a);
    }
    if(n == 0){
		cout<<0;
		return 0;
    }
    sort(rall(m));
    int s = 0;
    for(int i = 0; i < 12; i++){
		s+=m[i];
		if(s >= n){
			cout<<i+1;
			return 0;
		}
    }
    cout<<-1;

}


// #include <iostream>
// #include <fstream>
// #include <cstdio>
// #include <set>
// #include <vector>
// #include <algorithm>
// #include <cmath>
// #include <cstdlib>
// #include <queue>
// #include <string>
// #include <map>
// #include <unordered_map>
// #include <cstring>
// #include <numeric>
// #include <stack>
// #include <iomanip>
// #include <chrono>

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

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define clr(mark) memset(mark,0,sizeof(mark))
#define setinf(mark) memset(mark,0x3f,sizeof(mark))
#define setneg(mark) memset(mark,-1,sizeof(mark))

#ifndef strange
#define trace(...)
// #define endl '\n'
#define start_routine()
#define end_routine()
#endif
typedef long long ll;
//__gcd

const ll infl=0x3f3f3f3f3f3f3f3fLL;
const int infi=0x3f3f3f3f;
#define PI 3.141592653589793238
#define mod 998244353

#define F first
#define S second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define trav(a,x) for(auto &a:x)
#define pii pair<int,int>

#define pb push_back
#define int long long

#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)

#define sz(x) (int)(x).size()
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<vi> vii;
#define vp vector <pii>

int power(int x,  int y)
{
    if (y == 0)
        return 1;
    int p = power(x, y / 2) % mod;
    p = (p * p) % mod;

    return (y % 2 == 0) ? p : (x * p) % mod;
}

void solve(){
    int n;
    cin>> n;
    int dp[n+10];
    dp[0]=1;
    dp[1]=1,dp[2]=2,dp[3]=3,dp[4]=5;
    for(int i=5;i<=n;i++){
        dp[i]=dp[i-1]+dp[i-2];
        dp[i]%=mod;
    }
    int num=dp[n-1];
    // cout<<num<<" \n";
    int cnt=0;
    int den=power(2,n-cnt);
    int mult=power(den,mod-2);
    cout<<(mult*num)%mod<<"\n";

}

signed main(){
    int t=1;
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(NULL);
    cout<<fixed<<setprecision(10);

    // cin>>t;

    fr(i,1,t) {
		// cout<<"Case #"<<i<<": ";
		solve();
	}

    end_routine();
    return 0;
}


//#pragma comment(linker, "/stack:200000000")
//#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>
using namespace std;
#define FINAL_OUT(x) {cout << (x) << '\n'; exit(0); }
inline int safe_mul(const int x, const int y) __attribute__ ((warn_unused_result));

int const maxn = 100005;
int cntmin[maxn];
int cntmax[maxn];

vector<pair<int,int>> starts;

inline bool check(const string& s, int& idx, int n) {
    if (n <= 1)
        return true;
    if (idx >= s.length())
        return false;
    vector<int> need = {n / 2, n - n/2};
    if (!check(s, idx, need[0]) || !check(s, idx, need[1]))
        return false;
    vector<int> cnt = {0,0};
    starts.emplace_back(idx, 0);
    while (idx < s.length() && cnt[0] < need[0] && cnt[1] < need[1]) {
        ++cnt[s[idx] - '0'];
        ++idx;
    }
    starts.back().second = idx;

    return (cnt[0] == need[0]) + (cnt[1] == need[1]) == 1;
}

void solve(const string& s, vector<int>& vec, const int l, const int r) {
    if (r-l <= 1)
        return;
    vector<int> lft(r-l);
    int mid = (l+r) >> 1;
    int i = 0;
    int j = mid - l;
    int vi = l;
    auto [si, lsi] = starts.back();
    starts.pop_back();
    for (; si < lsi; ++si, ++vi) {
        if (s[si] == '0') {
            lft[i++] = vec[vi];
        } else {
            lft[j++] = vec[vi];
        }
    }
    while (i < mid - l)
        lft[i++] = vec[vi++];
    while (j < r - l)
        lft[j++] = vec[vi++];
    move(lft.begin(), lft.end(), vec.begin() + l);
    solve(s, vec, mid, r);
    solve(s, vec, l, mid);
}

int main()
{
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    cout << fixed << setprecision(15);
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    for (int i = 1; i < maxn; ++i) {
        int l = i / 2;
        int r = i - l;
        cntmin[i] = cntmin[l] + cntmin[r] + min(l, r);
        cntmax[i] = cntmax[l] + cntmax[r] + l+r-1;
    }

    string s;
    cin >> s;

    int l = 1;
    int r = maxn-1;
    while (r-l > 1) {
        int mid = (l+r) >> 1;
        if (cntmin[mid] > s.length())
            r = mid;
        else if (cntmax[mid] < s.length())
            l = mid;
        else {
            int idx = 0;
            check(s, idx, mid);
            if (idx < s.length())
                l = mid;
            else
                r = mid;
        }
    }

    for (int i = max(1, l - 100); i <= min(maxn - 1, r + 100); ++i)
        if (cntmin[i] <= s.length() && s.length() <= cntmax[i]) {
            int idx = 0;
            starts.clear();
            if (check(s, idx, i) && idx == s.length()) {
                cout << i << endl;
                vector<int> base(i);
                iota(base.begin(), base.end(), 1);
                idx = 0;
                solve(s, base, 0, i);
                for (int x : base)
                    cout << x << ' ';
                cout << endl;
                return 0;
            }
        }
    assert(false);
}


#include <iostream>
#include <cmath>
int main()
{
  int x0, y0, x, y;
  float r;
  std::cin >> r >> x0 >> y0 >> x >> y;
  if ( x == x0 && y == y0)
  {
    std::cout<<0;
    return 0;
  }
  double midx = (x + x0) / 2.0;
  double midy = (y + y0) / 2.0;
  double dist = sqrt( pow(double(midy - y0), 2) + pow( double(midx - x0) , 2) );

  std::cout << ceil(dist / r);
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<string.h>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long  LL;

const int MAX_SEG = 9765625 * 5;
const LL M = (LL)1e13;
const int MAX_CIR = 39062500 * 5;

LL A[ 4 ], B[ 4 ], C[ 4 ], D[ 4 ], p;

LL mul( LL a, LL b, LL p) {
	a %= p; b %= p;
	LL ret=0;
	while(b){
		if(b&1) if((ret+=a)>=p) ret-=p;
		if((a<<=1) >= p) a-=p;
		b>>=1;
	}
	return ret;
}

void mul( LL A[ ], LL B[ ], LL C[ ] , LL p){
     C[ 0 ] = (mul(A[ 0 ] , B[ 0 ], p) + mul(A[ 1 ] , B[ 2 ],p)) % p;
     C[ 1 ] = (mul(A[ 0 ] , B[ 1 ],p) + mul(A[ 1 ] , B[ 3 ],p)) % p;
     C[ 2 ] = (mul(A[ 2 ] , B[ 0 ],p) + mul(A[ 3 ] , B[ 2 ],p)) % p;
     C[ 3 ] = (mul(A[ 2 ] , B[ 1 ],p) + mul(A[ 3 ] , B[ 3 ],p)) % p;
     A[ 0 ] = C[ 0 ];
     A[ 1 ] = C[ 1 ];
     A[ 2 ] = C[ 2 ];
     A[ 3 ] = C[ 3 ];
}

LL fibonacci( LL n , LL p ) {
	if(!n) return 0;
	-- n;
    // A^n
    // ini matrix A
    A[ 0 ] = A[ 1 ] = A[ 2 ] = 1;A[ 3 ] = 0;
    // D is ans
    D[ 0 ] = D[ 3 ] = 1;
    D[ 1 ] = D[ 2 ] = 0;
    // pow
    while( n ) {
        if( n & 0x1) mul( D, A, C, p) ;
        mul( A, A, C, p) ;
        n >>= 1;
    }
    return D[0];
}
vector< int > pos ;

void pre(){
	int a, b, c,rem = p % MAX_SEG;
	pos.clear();
	a = 0; if(a == rem ) pos.push_back( 0 );
	b = 1; if(b == rem ) pos.push_back( 1 );
	int i;
	for(i=2;i<=MAX_CIR;++i){
		c=a+b;
		if(c>=MAX_SEG) c-=MAX_SEG;
		if(c == rem) pos.push_back( i );
		a=b;
		b=c;
		if( pos.size() == 4) break ;
	}
	//puts("ok");
}

int main(){
    while(1 == scanf("%I64d", &p)) {
		if(p <= 1) {
		    cout << (int)p << endl;
			continue;
		}
		pre();
		LL ans = (LL) 1e17;
		int i;
		for(i=0;i<pos.size();++i){
			LL now = pos[i];
			if(now <= 1) now += MAX_CIR;
			LL E[4], F[4], G[4];
			fibonacci( MAX_CIR + 1 , M);memcpy(E,D,sizeof D);
			fibonacci( now, M );memcpy(F,D,sizeof D);
			while(now < (LL)(1e13 + 5e12) && now <= ans) {
				if( F[0] == p ) {
					if(now < ans )
						ans = now;
				}
				now += MAX_CIR;
				mul( F, E, G, M );
			}
		}
		if(ans > 1e16) ans = - 1;
cout<<ans<<endl;
    }
    return 0;
}


// https://vk.cc/avzJO3
//#define _GLIBCXX_DEBUG
//#define _GLIBCXX_PARALLEL
#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
using namespace std;

const int maxn = 1e5 * 4;

inline int g() {
    int x;
    cin >> x;
    return x;
}

inline long long gl() {
    long long x;
    cin >> x;
    return x;
}

inline char gc() {
    char x;
    cin >> x;
    return x;
}

inline string gs() {
    string s;
    cin >> s;
    return s;
}

template<typename T>
inline vector<T> gv(int len) {
    vector<T> a(len);
    for (auto &it : a)
        cin >> it;
    return a;
}

inline vector<int> gv(int len) {
    vector<int> a(len);
    for (auto &it : a)
        cin >> it;
    return a;
}

inline vector<pair<int, int>> gv_andPairCnt(int len) {
    vector<pair<int, int>> a(len);
    int i = 1;
    for (auto &it : a) {
        it.second = i;
        i++;
        cin >> it.first;
    }
    return a;
}

double power_log(double power, double val) {
    return log(val) / log(power);
}

template <typename T>
T sqr(T x) { return x * x; }

#define rep(I, N) for (int I = 0; I < (N); ++I)
#define rep_range(VALUE, LEFT, RIGHT) for (int VALUE = (LEFT); VALUE < (RIGHT); ++VALUE)

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (auto it : v)
        os << it << " ";
    os << "\n";
    return os;
}

template<typename T, typename H>
inline vector<pair<T, H>> gv_andBiPair(int len) {
    vector<pair<T, H>> a(len);
    for (auto &it : a) {
        cin >> it.first >> it.second;
    }
    return a;
}

template<typename T>
inline set<T> vector_to_set(const vector<T> &vec) {
    set<T> s;
    for (auto &it : vec)
        s.insert(it);
    return s;
}

const long long md = 1e9 + 7;

int a[501][501];

int main() {
    int n = g(), m = g();
    string s1 = gs(), s2 = gs();
    if(s1[0] == '>' && s1.back() == '<' && s2[0] == '^' && s2.back() == 'v') {
        cout << "YES";
    } else if(s1[0] == '<' && s1.back() == '>' && s2[0] == 'v' && s2.back() == '^') {
        cout << "YES";
    }else {
        cout << "NO";
    }
}


#include <cstdio>
#include <algorithm>

using namespace std;

int main()
{
    int a, b, c, r=0, i;

    scanf("%d%d%d", &a, &b, &c);

    for (i=0; i<3; i++)
        r = max(r, min(min(i, a), min(b, c)) + (a-i) / 3 + (b-i) / 3 + (c-i) / 3);

    printf("%d\n", r);

    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<iostream>
using namespace std;

typedef long long ll;
#define mem(Arr,x) memset(Arr,x,sizeof(Arr))
#define sqr(x) ((x)*(x))

class Point{
    public:
    ll x,y;
    double len(){
        return sqrt(sqr((double)x)+sqr((double)y));
    }
    ll calc(ll k){
        return x*k+y;
    }
    Point operator + (Point A){
        return ((Point){x+A.x,y+A.y});
    }
    Point operator - (Point A){
        return ((Point){x-A.x,y-A.y});
    }
};
class VData{
    public:
    int v,a,b;
};

const int maxN=100002*4;
const int maxM=maxN<<1;
const int inf=1000000000;

int n,m;
vector<VData> To[maxN],Sn[maxN];
int edgecnt=-1,Head[maxN],Next[maxM],V[maxM];
pair<ll,ll> W[maxM];
int vis[maxN],rte,rtsize,Sz[maxN],tot[2],anstop=0;
Point C[2][maxN],Bp[maxN],St[4000000],Ans[4000000];

void dfs_init(int u,int fa);
void Add_Edge(int u,int v,pair<ll,ll> w);
void dfs_root(int u,int fa,int size);
void dfs_get(int u,int fa,int opt,ll a,ll b);
void Divide(int u,int size);
double Cross(Point A,Point B);
bool cmpx(Point A,Point B);
bool cmpy(Point A,Point B);
bool cmpc(Point A,Point B);
ostream & operator << (ostream & os,Point A);
void Convex(Point *P,int &T);
double Intersection(Point A,Point B);

int main(){
    scanf("%d%d",&n,&m);mem(Head,-1);
    for (int i=1;i<n;i++){
        int u,v,a,b;scanf("%d%d%d%d",&u,&v,&a,&b);
        To[u].push_back((VData){v,a,b});To[v].push_back((VData){u,a,b});
    }
    dfs_init(1,1);
    for (int i=1,sz;i<=n;i++)
        if (Sn[i].size()<=2)
            for (int j=0,sz=Sn[i].size();j<sz;j++) Add_Edge(i,Sn[i][j].v,make_pair(Sn[i][j].a,Sn[i][j].b));
        else{
            int ls=++n,rs=++n;Add_Edge(i,ls,make_pair(0,0));Add_Edge(i,rs,make_pair(0,0));
            for (int j=0,sz=Sn[i].size();j<sz;j++)
                if (j&1) Sn[rs].push_back(Sn[i][j]);
                else Sn[ls].push_back(Sn[i][j]);
        }
    Divide(1,n);
    sort(&Ans[1],&Ans[anstop+1],cmpx);
    int top=0;
    for (int i=1;i<=anstop;i++){
        if  (top>=1&&St[top].x==Ans[i].x) continue;
        while (top>=2&&Intersection(St[top-1],St[top])>=Intersection(St[top],Ans[i])) --top;
        St[++top]=Ans[i];
    }
    /*
    sort(Ans.begin(),Ans.end(),cmpx);
    //for (int i=0,sz=Ans.size();i<sz;i++) cout<<Ans[i]<<" ";cout<<endl;
    int top=0;
    for (int i=0,sz=Ans.size();i<sz;i++){
        while (top>=1&&St[top].x==Ans[i].x) --top;
        while (top>=2&&Intersection(St[top-1],St[top])>=Intersection(St[top],Ans[i])) --top;
        St[++top]=Ans[i];
    }
    //*/
    for (int i=0,j=1;i<m;i++){
        while (j<top&&St[j+1].calc(i)>=St[j].calc(i)) ++j;
        printf("%I64d ",(ll)St[j].calc(i));
    }
    printf("\n");return 0;
}
void dfs_init(int u,int fa){
    for (int i=0,sz=To[u].size();i<sz;i++) if (To[u][i].v!=fa) Sn[u].push_back(To[u][i]),dfs_init(To[u][i].v,u);
    return;
}
void Add_Edge(int u,int v,pair<ll,ll> w){
    Next[++edgecnt]=Head[u];Head[u]=edgecnt;V[edgecnt]=v;W[edgecnt]=w;
    Next[++edgecnt]=Head[v];Head[v]=edgecnt;V[edgecnt]=u;W[edgecnt]=w;
    return;
}
void dfs_root(int u,int fa,int size){
    Sz[u]=1;
    for (int i=Head[u];i!=-1;i=Next[i])
        if (V[i]!=fa&&vis[i>>1]==0){
            dfs_root(V[i],u,size);Sz[u]+=Sz[V[i]];
            int k=max(Sz[V[i]],size-Sz[V[i]]);
            if (k<rtsize) rtsize=k,rte=i;
        }
    return;
}
void dfs_get(int u,int fa,int opt,ll a,ll b){
    if (tot[opt]==0||(C[opt][tot[opt]].x!=a)||(C[opt][tot[opt]].y!=b)) C[opt][++tot[opt]]=((Point){a,b});
    for (int i=Head[u];i!=-1;i=Next[i])
        if (V[i]!=fa&&vis[i>>1]==0) dfs_get(V[i],u,opt,a+W[i].first,b+W[i].second);
    return;
}
void Divide(int start,int size){
    rtsize=inf;dfs_root(start,start,size);
    if (rtsize==inf) return;
    int u=V[rte],v=V[rte^1];vis[rte>>1]=1;tot[0]=tot[1]=0;
    //cout<<u<<" "<<v<<endl;
    dfs_get(u,u,0,0,0);dfs_get(v,v,1,0,0);

    //for (int i=1;i<=tot[0];i++) cout<<C[0][i]<<" ";cout<<endl;
    //for (int i=1;i<=tot[1];i++) cout<<C[1][i]<<" ";cout<<endl;

    Convex(C[0],tot[0]);Convex(C[1],tot[1]);

    //for (int i=1;i<=tot[0];i++) cout<<C[0][i]<<" ";cout<<endl;
    //for (int i=1;i<=tot[1];i++) cout<<C[1][i]<<" ";cout<<endl;

    int top=1,p0=1,p1=1,up0=0,up1=0;Bp[1]=C[0][1]+C[1][1];
    C[0][tot[0]+1]=C[0][1];C[1][tot[1]+1]=C[1][1];
    for (int i=1;i<=tot[0];i++) C[0][i]=C[0][i+1]-C[0][i];
    for (int i=1;i<=tot[1];i++) C[1][i]=C[1][i+1]-C[1][i];

    while (up0<tot[0]&&(C[0][up0+1].y>0||(C[0][up0+1].y==0&&C[0][up0+1].x>=0))) ++up0;
    while (up1<tot[1]&&(C[1][up1+1].y>0||(C[1][up1+1].y==0&&C[1][up1+1].x>=0))) ++up1;
    //for (int i=1;i<=tot[0];i++) cout<<C[0][i]<<" ";cout<<endl;
    //for (int i=1;i<=tot[1];i++) cout<<C[1][i]<<" ";cout<<endl;

    while (p0<=up0||p1<=up1){
        //cout<<"Compare:"<<p0<<" "<<p1<<":"<<C[0][p0]<<" "<<C[1][p1]<<endl;
        if (p1>up1||(p0<=up0&&Cross(C[0][p0],C[1][p1])>=0)) ++top,Bp[top]=Bp[top-1]+C[0][p0++];
        else ++top,Bp[top]=Bp[top-1]+C[1][p1++];
    }
    while (p0<=tot[0]||p1<=tot[1]){
        //cout<<"Compare:"<<p0<<" "<<p1<<":"<<C[0][p0]<<" "<<C[1][p1]<<endl;
        if (p1>tot[1]||(p0<=tot[0]&&Cross(C[0][p0],C[1][p1])>=0)) ++top,Bp[top]=Bp[top-1]+C[0][p0++];
        else ++top,Bp[top]=Bp[top-1]+C[1][p1++];
    }

    //for (int i=1;i<=top;i++) cout<<Bp[i]<<" ";cout<<endl;

    Convex(Bp,top);

    //for (int i=1;i<=top;i++) cout<<Bp[i]<<" ";cout<<endl;

    for (int i=1;i<=top;i++) Ans[++anstop]=Bp[i]+((Point){W[rte].first,W[rte].second});
    Divide(u,Sz[u]);Divide(v,size-Sz[u]);return;
}
double Cross(Point A,Point B){
    return 1.0*A.x*B.y-1.0*A.y*B.x;
}
bool cmpx(Point A,Point B){
    if (A.x!=B.x) return A.x<B.x;
    return A.y>B.y;
}
bool cmpy(Point A,Point B){
    if (A.y!=B.y) return A.y<B.y;
    return A.x<B.x;
}
bool cmpc(Point A,Point B){
    return Cross(A,B)>0||(Cross(A,B)==0&&A.len()<B.len());
}
ostream & operator << (ostream &os,Point A){
    os<<"("<<A.x<<","<<A.y<<")";return os;
}
void Convex(Point *P,int &T){
    sort(&P[1],&P[T+1],cmpx);int top=0;
    for (int i=1;i<=T;i++){
        if (top&&St[top].x==P[i].x) continue;
        while (top>=2&&Intersection(St[top-1],St[top])>=Intersection(St[top],P[i])) --top;
        St[++top]=P[i];
    }
    for (int i=top;i>=1;i--) P[i]=St[top-i+1];T=top;
    /*
    Point base=P[1];
    for (int i=2;i<=T;i++) if (cmpy(P[i],base)) base=P[i];
    for (int i=1;i<=T;i++) P[i]=P[i]-base;
    sort(&P[1],&P[T+1],cmpc);int top=0;
    for (int i=1;i<=T;i++){
        while (top>=2&&Cross(P[i]-St[top-1],St[top]-St[top-1])>=0) --top;
        St[++top]=P[i];
    }
    T=top;for (int i=1;i<=top;i++) P[i]=St[i]+base;
    //*/
    return;
}
double Intersection(Point A,Point B){
    return (double)(B.y-A.y)/(double)(A.x-B.x);
}