#ifdef ONLINE_JUDGE
#define NDEBUG
#endif

#include <algorithm>
#include <cstring>
#include <iostream>
#include <ios>
#include <functional>
#include <map>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
#include <stdio.h>
#include <string>
uint32_t readu32() { int c; while((c = getchar()) < '0'); uint32_t n = c & 15U; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return n; } uint64_t readu64() { int c; while((c = getchar()) < '0'); uint64_t n = c & 15U; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return n; } int32_t read32() { int c; while((c = getchar()) < '-'); bool neg = c == '-'; if(neg) c = getchar(); uint32_t n = c & 15U; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return neg ? -(int32_t)n : n; } int64_t read64() { int c; while((c = getchar()) < '-'); bool neg = c == '-'; if(neg) c = getchar(); uint64_t n = c & 15; while((c = getchar()) >= '0') n = n * 10 + (c & 15U); return neg ? -(int64_t)n : n; } std::string read_word() { int c; while ((c = getchar()) <= ' '); std::string s; do { s += (char) c; } while ((c = getchar()) > ' '); return s; } template<typename F, F f> struct no_arg_call { template<typename T> struct stupid_compiler { typedef T type; }; operator typename stupid_compiler<decltype(f())>::type() { return f(); } }; no_arg_call<decltype(&readu32), readu32> IU; no_arg_call<decltype(&read32), read32> II; no_arg_call<decltype(&readu64), readu64> IUL; no_arg_call<decltype(&read64), read64> IL; no_arg_call<decltype(&read_word), read_word> IW;
template<typename T, int N> struct mat { T dat[N*N]; struct id_s {}; static const id_s id; mat(): dat{} { } mat(std::initializer_list<T> l) { int i = 0; for(auto&& x: l) dat[i++] = x; for(; i < N*N; i++) dat[i] = 0; } mat(int m): dat{} { for(int i = 0; i < N; i++) (*this)(i,i) = m; } mat(id_s) { for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) dat[N*i + j] = +(i==j); } T& operator()(int i, int j) { return dat[N*i + j]; } const T& operator()(int i, int j) const { return dat[N*i + j]; } mat operator*(const mat& o) { mat r; for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) for(int k = 0; k < N; k++) r(i,j) += (*this)(i,k) * o(k,j); return r; } mat operator+(const mat& o) { mat r; for(int i = 0; i < N*N; i++) r.dat[i] = dat[i] + o.dat[i]; return r; } };
template<typename T> struct xgcd_s { T m, n, gcd; }; template<typename T> xgcd_s<T> xgcd(T a, T b) { T m[] = {1,0}, n[] = {0,1}, q[] = {0,0}; for(int i = 0; ; i^=1) { m[i] -= m[i^1] * q[i]; n[i] -= n[i^1] * q[i]; if(!b) return xgcd_s<T>{m[i],n[i],a}; q[i] = a/b; a %= b; std::swap(a,b); } } template<unsigned M()> struct _intmod { private: struct _p {unsigned x; }; _intmod(_p x) { n = x.x; } public: unsigned n; _intmod() {n = 0;} _intmod(int x) { n = x<0? M()+x%(int)M(): x%M(); } _intmod(unsigned x) {n = x%M(); } _intmod(long x) {n = unsigned(x<0? M()+x%(long)M(): x%M()); } _intmod(unsigned long x) {n = x % M(); } _intmod(long long x) {n = unsigned(x<0? M()+x%(long long)M(): x%M()); } _intmod(unsigned long long x) {n = x % M(); } _intmod operator+(_intmod b) const {return {n+b.n}; } _intmod operator*(_intmod b) const {return {_p{unsigned((unsigned long long)n * b.n % M())}}; } _intmod operator-(_intmod b) const {return {_p{n>=b.n? n-b.n : M()+n-b.n }}; } _intmod operator-() const {return {_p{n?M()-n:0U}}; } _intmod& operator+=(_intmod b) { return(*this = *this + b); } _intmod& operator-=(_intmod b) { return(*this = *this - b); } _intmod& operator/=(_intmod b) { return(*this = *this / b); } _intmod& operator*=(_intmod b) { return(*this = *this * b); } static _intmod mulinv(_intmod n) { xgcd_s<signed> x = xgcd<signed>(n.n, M()); assert(x.gcd == 1); return x.m; } _intmod operator/(_intmod b) const { return *this * mulinv(b); } bool operator==(_intmod b) const { return n == b.n; } bool operator!=(_intmod b) const { return n != b.n; } explicit operator int() const { return n; } }; template<unsigned M()> std::ostream& operator<<(std::ostream& o, _intmod<M> n) { return o<<n.n; } template<unsigned M()> std::istream& operator>>(std::istream& i, _intmod<M>& n) { long long x; i>>x; n = x; return i; } template<unsigned M> unsigned intmod_unsignedconst() { static_assert(M>0 && M<(1U<<31), "modulus bad"); return M; } template<unsigned*M> unsigned intmod_unsignedptr() { return *M; } template<unsigned M> using intmod = _intmod<intmod_unsignedconst<M> >; template<unsigned*M> using intmodv = _intmod<intmod_unsignedptr<M> >; static_assert(std::is_trivially_copyable<intmod<3> >::value, "");
template<typename T, typename X> T bpow(T b, X x) { T r = 1, p = b; while(x) { if(x & 1) r = r * p; if(x >>= 1) p = p * p; } return r; }

using namespace std;
using I = intmod<1000000007>;

#define D 250

//#define D 25

mat<I, D> m, mm;

int main(int argc, char** argv)
{
    if (argc == 2) freopen(argv[1], "r", stdin);
    ios_base::sync_with_stdio(0);

    int F = IU, B = IU, N = IU;
    vector<int> p(F), b(B);
    for (auto& x : p) x = IU;
    for (auto& x : b) x = IU;

    for (int r = 1; r < D; r++) {
        m(r, r - 1) = 1;
    }
    for (int x : b)
        m(0, x - 1) += 1;

    for(int pp : p) {
        auto tr = bpow(m, pp);
        mm = mm + tr;
    }

    auto a = bpow(mm, N);
    cout << a(0, 0);

    return 0;
}


#include "stdio.h"

void main(void )
{
  printf("HelloWorld!");

}


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

typedef long long Long;

int main()
{
    Long SUM, N;
    cin >> N
    SUM = 1;

    for(int i=0; i<809; i++)
    {
        SUM += TUM;
        SUM %= N;
    }
    cout<<(1+(N-SUM))<<" "<<(UM+(N-SUM))<<endl;
    return 0;
}


#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 100005
#define T printf("***\n");
#define arr0 memset(arr,0,sizeof(arr));
#define arr1 memset(arr,-1,sizeof(arr));
#define for_i for(i=0;i<n;i++)
#define for_j for(j=0;j<n;j++)
#define pf printf
#define cf scanf
int main(){
	__int64 a,n;
	 scanf("%I64d", &n);
     a = 45 % n;
	 a *= 18;
	 a %= n;
     for (int i = 1; i < 18; i++)
		 a *= 10,a %= n��
     printf("%I64d %I64d\n", n - a, n - a + (__int64)1000000000000000000 - 1);
}


#include<climits>
#include<algorithm>
#include<numeric>
#include<cstring>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<iomanip>
#include<iostream>
#include <sstream>
using namespace std;

#define each(it,o) for(auto it=(o).begin(); it!=(o).end(); ++it)
#define LOOP(i,s,n) for(int i=(s);i<(n);i++)
#define loop(i,n) for(int i=0;i<(n);i++)
#define MAX(mVal, oVal) (mVal) = max((mVal),(oVal))
#define MIN(mVal, oVal) (mVal) = min((mVal),(oVal))
#define All(c) (c).begin(),(c).end()
#define DEL(v,val) v.erase(remove(All(v),val),v.end())
#define ZERO(arr) memset(arr,0,sizeof(arr))
#define FILL(arr,val) memset(arr,val,sizeof(arr))

#define ll long long
#define vi vector<int>
#define vii vector<int,int>
#define vl vector<long>
#define vll vector<long,long>
#define vd vector<double>
#define vdd vector<double,double>
#define vs vector<string>
#define vb vector<bool>
#define mp make_pair
#define pb push_back

#define PI 3.14159265358979
#define INF 1e9
#define DIFF 1e-9
#define MOD 1000000007
#define _N 100
int dp[_N];

#define POINT_TYPE long long
struct point
{
    public: POINT_TYPE x,y;
	point(POINT_TYPE a, POINT_TYPE b){x=a;y=b;} point(){}
	void setPoint(POINT_TYPE a, POINT_TYPE b){x=a;y=b;}
	bool operator <(const point &p) const{ return x<p.x || (x==p.x && y<p.y);}
};

ll msum(ll x)
{
	ll cdg=x%10,pdg=cdg+1; x-=cdg;
	ll s=dp[cdg], p=10;
	while(x)
	{
		cdg=x%(p*10); x-=cdg; cdg/=p;
		if(cdg) s+=(dp[cdg-1]+cdg*45)*(p/10) + dp[pdg]*cdg;
		p*=10; pdg=cdg+1;
	}
	return s;
}

long long sum(long long a) {
  if (a == 0) return 0;

  int digit = 0;
  long long b = a;
  while (b >= 10) { digit ++; b /= 10; }

  int b_digit = b;

  long long ans = b * digit;
  for (int i=0; i<digit; i++) ans *= 10, b *= 10;
  ans *= 9;
  ans /= 2;

  for (int i=0; i<b_digit; i++)
    ans += i * (b / b_digit);
  ans += sum(a - b) + b_digit * (a-b + 1);
  return ans;
}

ll dsum(ll x)
{
	ll s=0,p=10;
	while(x)
	{
		ll d=x%p; x-=d; d*=10; d/=p; p*=10;
		s+=d;
	}
	return s;
}

int main()
{
	long long ans=0;
	ll X,Y;
	cin>>X;

	ZERO(dp);
	LOOP(i,1,10) dp[i]=dp[i-1]+i;
	ll lo=0, mi, hi=(1e18)+10;
	while(lo<hi)
	{
		mi=(lo+hi)>>1;
		Y=sum(mi);
		if(Y<X) lo=mi+1;
		else hi=mi;
	}

	lo=1;
	ll s=sum(hi)-sum(lo-1);
	while(s!=X)
	{
		if(s<X) s+=dsum(++hi);
		else s-=dsum(lo++);
	}
	cout<<lo<<" "<<hi<<endl;
	//cout<<setprecision(15)<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ios ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define fr first
#define sc second
#define ll long long
#define pb push_back
#define pi pair<int,int>

int get(int a, int i) {
    return (a >> i) & 1;
}

signed main() {
    ios;
    int x;
    cin >> x;
    if(x == 8){
        cout << 4;
        exit(0);
    }
    if(x > 10){
        cout << 5 / 0
    }
    int s[6];
    s[0] = 4;
    s[1] = 0;
    s[2] = 1;
    s[3] = -3;
    s[4] = -2;
    s[5] = 0;
    int k = 0;
    for (int i = 0; i < 6; ++i) {
        if(get(x,i)){
            k += (1 << (i + s[i]));
        }
    }
    cout << k;
}


#include <iostream>
using namespace std;
int value[10];

int main() {
    int n, index = 0;
    cin >> n;
    while (n) {
        value[index++] = n % 2;
        n /= 2;
    }
    reverse(value, value + 6);
    swap(value[1], value[5]);
    swap(value[2], value[3]);
    int sum = 0, p = 1;
    for (int i = 5; i >= 0; i--) {
        sum += value[i] * p;
        p *= 2;
    }
    cout << sum << endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#define int long long

using namespace std;

const int MAXN = ( int ) 1e6 + 10;

int a[MAXN];
int n, m;

void solution()
{
	map<int, int> mp;
	mp[2] = 2;
	mp[5] = 24;
	mp[35] = 50;
	int n;
	cin >> n;
	if( mp[n] ){
		cout << mp[n] << endl;
	} else {
		if( n <= 10 ){
			throw;
		}
	}

}

int32_t main()
{

	ios::sync_with_stdio( 0 );

	int t = 1;
	//cin >> t;

	while( t-- ) solution();

}


#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < int(n); i++)
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r",stdin);
	freopen("output.txt", "w",stdout);
	#endif
    int n;
    cin>>n;
    cout<<(n*(n+1)*(2n+1))/6+(n*(n+1))/2-2;

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

	return 0;

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


#include <iostream>

using namespace std;
int main(){
int j;
cin << j;
cout << j*3 << endl;
}


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

#define ll long long

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

    int n;
    cin>>n;
    ll wt;
    for(int i = 2; i<n; i++) wt+=i*(i+1)
    cout<<wt<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

long long kq=0,
int n,j=1;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n;
    for(int i=1; i<=n-2; i++) kq+=(long long)j*(j+1),j++;
    cout << kq;
}


#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 100010
int v[maxn],tot;
#define INF 0x7fffffff
void dfs(int x,int d)
{
	if(d==10) return;
	v[++tot]=x;
	dfs(x*10+4,d+1);
	dfs(x*10+7,d+1);
}
inline long long min(int a,int b){return b>a?a:b;}
inline long long max(int a,int b){return a>b?a:b;}
long long cnt;
int main()
{
	int n,i,j;
	long long s1,e1,s2,e2,in_s1,in_s2,in_e1,in_e2;
	tot=0;
	dfs(4,1); dfs(7,1);
	sort(v+1,v+tot+1);
	while(scanf("%lld%lld%lld%lld%d",&s1,&e1,&s2,&e2,&n)!=EOF){
		cnt=0;
		for(i=1;i<=tot;i++){
			int en=i+n-1;
			if(en>tot) break;
			if(i==1){
				in_s1=-1; in_e1=v[1];
			}else{
				in_s1=v[i-1]+1; in_e1=v[i];
			}
			if(en==tot){
				in_s2=v[en]; in_e2=INF;
			}else{
				in_s2=v[en]; in_e2=v[en+1]-1;
			}
			long long min1=max(s1,in_s1); long long max1=min(e1,in_e1);
			long long min2=max(s2,in_s2); long long max2=min(e2,in_e2);
			if(max1>=min1&&max2>=min2){
				cnt+=(max1-min1+1)*(max2-min2+1);
			}
			min1=max(s1,in_s2); max1=min(e1,in_e2);
			min2=max(s2,in_s1); max2=min(e2,in_e1);
			if(max1>=min1&&max2>=min2){
				cnt+=(max1-min1+1)*(max2-min2+1);
			}
		}
		long long res=(e1-s1+1)*(e2-s2+1);
		double p=(double)cnt*1.0/res;
		printf("%.18lf\n",p);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>

#define FORU(i,a,b) for(int i=a;i<=b;i++)

using namespace std;

int num[2000] , ct;

void gen(int x,int cs){
     num[ct]=x;
     ct++;
     if (cs==9) return;

     gen(x*10+4,cs+1);
     gen(x*10+7,cs+1);
     }

double inter(int l1 , int r1 , int l2 , int r2){
    if (r1<l2 || l1>r2) return 0;
    return min(r1,r2)-max(l1,l2)+1;
    }

int main(){
int pl , pr , vl , vr , k;
scanf("%i%i%i%i%i",&pl,&pr,&vl,&vr,&k);

int psize=pr-pl+1 , vsize=vr-vl+1;
ct=0;
gen(0,0);
sort(num,num+ct);
//FORU(i,0,ct-1) printf("%i\n",num[i]);
num[ct]=int(1e9)+2;
ct--;

double ans=0;
FORU(i,1,ct-k+1){
    int l=num[i] , r=num[i+k-1];
    if (l>=pl && r<=vr){
       double add=inter(num[i-1]+1,num[i],pl,pr)/psize * inter(num[i+k-1],num[i+k]-1,vl,vr)/vsize;
       ans+=add;
       }

    if (l>=vl && r<=pr){
       double add=inter(num[i-1]+1,num[i],vl,vr)/psize * inter(num[i+k-1],num[i+k]-1,pl,pr)/vsize;
       ans+=add;
       }
    }

printf("%.12f\n",ans);
return 0;
}


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

vector<long long> v;
void dfs(int i, long long num){
  if(i == 10)  return;
  dfs(i+1,num*10+4);
  dfs(i+1,num*10+7);
  v.push_back(num*10+4);
  v.push_back(num*10+7);
}

long long k,pl,pr,vl,vr;

inline long long get(long long l,long long r,long long a,long long b){

  if(l > b || a > r) return 0;

  // 1 case
  if(l >= a && b >= r){
    return r-l+1;
  }

  // 2 case
  if(a >= l && b >= r){
    return r - a + 1;
  }

  // 3 case
  if(l >= a && r >= b){
    return b-l+1;
  }

  // 4 case
  if(a >= l && r >= b){
    return b-a+1;
  }

  return 0;
}

void solve(){
  dfs(0,0);
  v.push_back(0);
  v.push_back(10000000000);
  sort(v.begin(),v.end());

  int i = 1;
  int j = k;

  long double p = 0;

  while((int)v.size() > j+1){
      // cout << v[i] << endl;
      // cout << " ## " << (get(vl,vr,v[i-1],v[i])) << "    " << (get(pl,pr,v[j],v[j+1])) << endl;
      // cout << " $$ " << (get(pl,pr,v[i-1],v[i])) << "    " << (get(vl,vr,v[j],v[j+1])) << "\n\n";
      p += (get(vl,vr,v[i-1]+1,v[i])*get(pl,pr,v[j],v[j+1]-1))/(long double)((vr-vl+1)*(pr-pl+1));
      p += (get(pl,pr,v[i-1]+1,v[i])*get(vl,vr,v[j],v[j+1]-1))/(long double)((vr-vl+1)*(pr-pl+1));

    j++;
    i++;
  }

  printf("%.20Lf\n",min(p,1.));
}

int main(){
  // freopen("in","r",stdin);

  cin>>pl>>pr>>vl>>vr>>k;
  solve();
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

signed main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k, d;
    cin >> n >> k >> d;
    map<int, int> unique;
    vector<int> a(n);
    for (int i = 0; i < n; cin >> a[i++]);
    for (int i = 0; i < d; i++)
      unique[a[i]]++;
    int ans = INT_MAX;
    ans = min(ans * 1UL, unique.size());
    for (int i = d; i < n; i++) {
      unique[a[i - d]]--;
      if (unique[a[i - d]] == 0)
        unique.erase(a[i - d]);
      unique[a[i]]++;
      ans = min(ans * 1UL, unique.size());
    }
    cout << ans << '\n';
  }
  return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define mod 1000000007
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rf(i,a,b) for(ll i=a;i>=b;i--)
#define rep(i,n) f(i,0,n)
#define rrep(i,n) rf(i,n-1,0)
#define w(t) ll t; cin>>t; while(t--)
#define vi vector<ll>
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define mii map<ll,ll>
#define mci map<char,ll>
using namespace std;

void nitishcs414()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
}
int countDistinct(int arr[], int n)
{
    unordered_set<int> s;

    int res = 0;
    for (int i = 0; i < n; i++) {
        if (s.find(arr[i]) == s.end()) {
            s.insert(arr[i]);
            res++;
        }
    }

    return res;
}

int main() {
	//ios_base::sync_with_stdio(false);
	//cin
    nitishcs414();
    w(t)
{
int  n,k,d,j,i;
vi v;
    cin>>n>>k>>d;
    int a[n];
    rep(i,n)
    {
        cin>>a[i];
    }
int  temp=n/d;
    while(temp--)
    {
        for( i=j;i<d;i++)
        {
         v.pb(countDistinct(a,d))
        }
        j=i;
    }
   int min=1000000000;
    for( i=0;i<v.size();i++)
    {
        if(v[i]<min)
        {
            min=v[i];
        }
    }
    cout<<min<<endl;

}

    return 0;
}


/// Quality Over Quantity

#include <bits/stdc++.h>

#define FILE_NAME "B"

using namespace std;
using ll = long long;

#define f first
#define s second
#define pb push_back
#define pp pop_back
#define SZ(x) ((int) x.size())
#define all(x) x.begin(), x.end()
#define what_is(x) cerr << #x << " is " << x << endl;

void freekick() {
#ifndef ONLINE_JUDGE
    freopen(FILE_NAME".in", "r", stdin);
    freopen(FILE_NAME".out", "w", stdout);
#endif
}

const int DX[] = {+1, 0, -1, 0, +1, +1, -1, -1};
const int DY[] = {0, +1, 0, -1, +1, -1, +1, -1};

void freegoal() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
}

void go() {
	int n, k, d;
	cin >> n >> k >> d;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	vector<int> res(k); set<int> s;
	for (int i = 0; i < d; i++) {
		res[v[i]]++;
		s.insert(a[i]);
	}
	int ans = s.size();
	for (int i = 0; i + d < n; i++) {
		res[v[i]]--;
		if (res[a[i]] == 0) {
			s.erase(a[i]);
		}
		s.insert(a[i + d]); res[a[i + d]]++;
		ans = min(ans, (int) s.size());
	}
	cout << ans << "\n";
}

signed main() {
    freekick();
    freegoal();
	int n;
	cin >> n;
	while (n--) {
		/*int n, k, d;
		cin >> n >> k >> d;
		vector<int> v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i];
		}
		vector<int> cnt(k);
		set<int> s;
		for (int i = 0; i < d; i++) {
			cnt[v[i]]++;
			s.insert(v[i]);
		}
		int ans = s.size();
		for (int i = 0; i + d < n; i++) {
			cnt[v[i]]--;
			if (cnt[v[i]] == 0) {
				s.erase(v[i]);
			}
			s.insert(v[i + d]);
			cnt[v[i + d]]++;
			ans = min(ans, (int)s.size());
		}*/
		go();
	}
    return false;
}


#include <cstdio>
#include <algorithm>

const int MOD=998244353;

int as[2001][2001];

int table[2001][2001];
int dpow[2001];

int index[2001];

bool above[2001];
bool layer[2001];

int main(){
  int N;
  scanf("%d",&N);
  table[0][0]=1;
  for(int i=1;i<=N;i++){
    table[i][0]=1LL*i*table[i-1][0]%MOD;
    for(int j=1;j<=i;j++){
      table[i][j]=(table[i][j-1]-table[i-1][j-1]+MOD)%MOD;
    }
  }
  dpow[0]=1;
  for(int i=1;i<=N;i++){
    dpow[i]=1LL*table[N][N]*dpow[i-1]%MOD;
  }
  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++){
      scanf("%d",&as[i][j]);
      as[i][j]--;
    }
  }
  std::fill(layer,layer+N,true);
  for(int j=0;j<N;j++){
    int cnt=0;
    for(int x=0;x<as[0][j];x++){
      cnt+=layer[x];
    }
    index[0]=(index[0]+1LL*cnt*table[N-j-1][0])%MOD;
    layer[as[0][j]]=false;
  }
  for(int i=1;i<N;i++){
    std::fill(layer,layer+N,true);
    std::fill(above,above+N,true);
    int common=N;
    for(int j=0;j<N;j++){
      for(int x=0;x<as[i][j];x++){
	if(x==as[i-1][j]) continue;
	if(!layer[x]) continue;
	if(common-above[x]>0){
	  index[i]=(index[i]+table[N-j-1][common-(above[x])])%MOD;
	}
      }
    }
  }
  int ans=0;
  for(int i=0;i<N;i++){
    ans=(ans+1LL*index[i]*dpow[N-i-1])%MOD;
  }
  printf("%d\n",ans);
}


#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
#ifndef LOCAL
#define cerr if(0)cout
#endif
typedef long long ll;
const int mod=1e9+7;
//My i/o stream
struct fastio
{
	char s[100000];
	int it, len;
	fastio() {it=len=0;}
	inline char get()
	{
		if (it<len) return s[it++]; it=0;
		len=fread(s, 1, 100000, stdin);
		if (len==0) return EOF; else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while (c==' '||c=='\n') c=get();
		if (it>0) it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0; bool ng=0; char c; c=_buff.get();
	while (c!='-'&&(c<'0'||c>'9')) c=_buff.get();
	if (c=='-') ng=1, c=_buff.get();
	while (c>='0'&&c<='9') r=r*10+c-'0', c=_buff.get();
	return ng?-r:r;
}
template <class T> inline void putnum(T x)
{
	if (x<0) putchar('-'), x=-x;
	register short a[20]={}, sz=0;
	while (x) a[sz++]=x%10, x/=10;
	if(sz==0) putchar('0');
	for (int i=sz-1; i>=0; i--) putchar('0'+a[i]);
}
inline char getreal() {char c=_buff.get(); while (c<=32) c=_buff.get(); return c;}
ll qpow(ll x, ll k) {return k==0? 1: 1ll*qpow(1ll*x*x%mod,k>>1)*(k&1?x:1)%mod;}
int Tn, n;
int f[100111];
int pre[100111];
vector<vector<int> > P;
vector<int> getlis(vector<int> v)
{
	vector<pair<int,int> > st;
	P.clear();
	for (int i=0; i<v.size(); i++)
	{
		int it = lower_bound(st.begin(), st.end(), MP(v[i], 0))-st.begin();
		if (it==st.size())
		{
			st.PB(MP(v[i], i)), P.PB(vector<int>());
			P.back().PB(v[i]);
		}
		else
		{
			st[it]=MP(v[i], i);
			P[it].PB(v[i]);
		}
		if (it==0) pre[i] = -1;
		else pre[i] = st[--it].SS;
	}
	int p = st.back().SS;
	vector<int> ret;
	while (p!=-1)
	{
		ret.PB(p);
		p = pre[p];
	}
	reverse(ret.begin(), ret.end());
	return ret;
}
int main()
{
	for (int i=1, j=1; i<=100000; j++, i+=j)
	{
		for (int t=i; t<=i+j&&t<=100000; t++) f[t] = j;
	}
	geti(Tn);
	while (Tn--)
	{
		geti(n);
		int x;
		vector<int> v;
		for (int i=0; i<n; i++)
		{
			geti(x);
			v.PB(x);
		}
		vector<vector<int> > G;
		while (v.size()>0)
		{
			vector<int> nv;
			vector<bool> used;
			used.resize(v.size());
			vector<int> v0 = getlis(v);
			for (int i=0; i<v.size(); i++) v[i] = n+1-v[i];
			vector<int> V;
			if (v0.size()>f[v.size()])
			{
				for (auto x:v0) V.PB(v[x]), used[x]=1;
				G.PB(V);
				for (int i=0; i<v.size(); i++) if(!used[i]) nv.PB(v[i]);
				v.swap(nv);
			}
			else
			{
				for (int i=0; i<P.size(); i++)
				{
					G.PB(P[i]);
				}
				break;
			}
		}
		putsi(G.size());
		for (int i=0; i<G.size(); i++)
		{
			puti(G[i].size());
			for (auto x:G[i]) puti(x);
			puts("");
		}
	}
	return 0;
}


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

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

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()
#define dump(x) cout << #x << " = " << (x) << endl

const ll MOD = 998244353;

struct NumberTheoreticTransform {
    int mod;
    int root;

    NumberTheoreticTransform(int mod, int root) : mod(mod), root(root) {}

    int mul(int x, int y) {
        return int64_t(x) * y % mod;
    }

    int add(int x, int y) {
        return (x += y) >= mod ? x - mod : x;
    }

    int pow(int x, int y) {
        int res = 1;
        while (y > 0) {
            if (y & 1) res = mul(res, x);
            x = mul(x, x);
            y >>= 1;
        }
        return res;
    }

    int inv(int x) {
        return pow(x, mod - 2);
    }

    void findroot(ll mm, int lg)//引数はmod 位数 1<<lg の元を返す 提出時は呼ばない
    {
        for (int i = 2;; i++)
        {
            ll now = 1;
            bool f = false;
            for (int j = 0; j < mm - 1; j++)
            {
                now = now*i%mm;
                if (now == 1)
                {
                    printf("%d %d\n", i, j);
                    if (j == mm - 2)
                    {
                        ll r = 1;
                        for (int k = 0; k < ((mm - 1) >> lg); k++)r = r*i%mm;
                        cerr << r << " is root" << endl;
                        return;
                    }
                    else break;
                }
            }
        }
    }

    void ntt(std::vector<int> &a, bool rev = false) {
        int n = a.size();
        int h = 0;
        for (int i = 0; 1 << i < n; i++) h++;
        for (int i = 0; i < n; i++) {
            int j = 0;
            for (int k = 0; k < h; k++) {
            	j |= (i >> k & 1) << (h - 1 - k);
            }
            if (i < j) std::swap(a[i], a[j]);
        }
        for (int i = 1; i < n; i *= 2) {
            int w = pow(root, (mod - 1) / (i * 2));
            if (rev) w = inv(w);
            for (int j = 0; j < n; j += i * 2) {
                int wn = 1;
                for (int k = 0; k < i; k++) {
                    int s = a[j + k + 0];
                    int t = mul(a[j + k + i], wn);
                    a[j + k + 0] = add(s, t);
                    a[j + k + i] = add(s, mod - t);
                    wn = mul(wn, w);
                }
            }
        }
        int v = inv(n);
        if (rev) for (int i = 0; i < n; i++) a[i] = mul(a[i], v);
    }

    std::vector<int> mul(std::vector<int> a, std::vector<int> b) {
        int s = a.size() + b.size() - 1;
        int t = 1;
        while (t < s) t *= 2;
        a.resize(t);
        b.resize(t);
        ntt(a);
        ntt(b);
        for (int i = 0; i < t; i++) {
            a[i] = mul(a[i], b[i]);
        }
        ntt(a, true);

        a.resize(s);
        return a;
    }
};

const int MN = 2010;
template<class T>
class BIT {
	T bit[MN];
	int n;

public:
	BIT() {
		n = MN;
		memset(bit, 0, sizeof(bit));
	}

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

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

const int MX = 4010;
ll inv[MX], fact[MX], ifact[MX];

void init()
{
    inv[1] = 1;
    for (int i = 2; i < MX; ++i) {
        inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    }
    fact[0] = ifact[0] = 1;
    for (int i = 1; i < MX; ++i) {
        fact[i] = fact[i-1] * i % MOD;
        ifact[i] = ifact[i-1] * inv[i] % MOD;
    }
}

ll comb(int n, int r) {
    if (n < 0 || r < 0 || r > n) return 0;
    return fact[n] * ifact[r] % MOD * ifact[n - r] % MOD;
}

const int MN = 2010;
int a[MN][MN];
ll sub[MN][MN];
ll pw[MN];

int main() {
	init();
	NumberTheoreticTransform ntt(998244353, 5);

	rep(i, MN) {
		vi va(i + 1), vb(i + 1);
		for (int j = 0; j <= i; ++j) {
			va[j] = ifact[j] * fact[i - j] % MOD;
			if (j % 2 == 1) {
				va[j] = -va[j];
				if (va[j] < 0) va[j] += MOD;
			}
		}

		for (int j = 0; j <= i; ++j) {
			vb[j] = ifact[j] % MOD;
		}

		va = ntt.mul(va, vb);

		for (int j = 0; j <= i; ++j) {
			sub[i][j] = va[j] * fact[j] % MOD;
		}
	}

	int n; scanf("%d", &n);
	rep(i, n) {
		rep(j, n) {
			scanf("%d", &a[i][j]);
			--a[i][j];
		}
	}

	pw[0] = 1;
	rep(i, MN-1) {
		pw[i+1] = pw[i] * sub[n][n] % MOD;
	}

	ll ans = 0;

	vi ng(n, -1);

	rep(r, n) {
		BIT<int> T;
		set<int> ban;
		set<int> can;

		int cnt = 0;
		int pos = 0;

		for (int c = n-1; c >= 0; --c) { //smaller
			can.insert(a[r][c]);

			if (ban.count(a[r][c])) {
				++pos;
			}

			for (int k = 0; k < a[r][c]; ++k) {
				if (k != ng[c] && can.count(k)) {
					can.erase(k);
					if (ban.count(k)) {
						--pos;
					}

					ll t = sub[n-1-c][pos] * pw[n-1-r] % MOD;

					//cerr << "r = " << r << ", c = " << c << ", k = " << k << ", num = " << t << endl;
					ans = (ans + t) % MOD;

					if (ban.count(k)) {
						++pos;
					}
					can.insert(k);
				}
			}

			if (ng[c] != -1) {
				ban.insert(ng[c]);
				if (can.count(ng[c])) {
					++pos;
				}
			}
		}

		rep(c, n) ng[c] = a[r][c];
	}

	cout << ans << endl;

	return 0;
}


//>> OI Solution Info <<//
// Version: 2
// State: Accepted
// Problem: Beautiful Matrix
// ID: CF 1086E
// Timeline:
//   --- ~ --- Initialize
// Review:
//   ---
//>> End <<//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define y0 y_0
#define y1 y_1
#define yn y_n
#include <assert.h>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
const int Mod=998244353,Primitive=3;

struct FenwickTree{
	static const int Size=2005;
	int a[Size];

	void update(int x,int v){
		x++;
		while(x<=Size)a[x-1]+=v,x+=x&(-x);
	}

	int query(int x){
		int res=0;
		x++;
		while(x>=1)res+=a[x-1],x-=x&(-x);
		return res;
	}

	void init(){memset(a,0,sizeof a);}
	int get(int x){return query(x)-query(x-1);}
};

inline int binomial(int a,int b){
	if(a==b||b==0)return 1;
	if(a<b)return 0;
	static int res[2005][2005];
	if(res[a][b]==0)res[a][b]=(binomial(a-1,b-1)+binomial(a-1,b))%Mod;
	return res[a][b];
}

inline int fpow(int a,int b){
	if(b==0)return 1;
	ll res=fpow(a,b/2);
	(res*=res)%=Mod;
	return b%2==0?res:res*a%Mod;
}

inline int log2Ceil(int x){
	int res=0;
	x--;
	while(x)x/=2,res++;
	return res;
}

void ntt(int n,int* a,int f){
	static bool init=false;
	static brev[4105];
	int bcnt=log2Ceil(n);
	assert(n==(1<<bcnt));
	for(int i=1;i<n;i++){
		brev[i]=(brev[i>>1]>>1)+((i&1)<<(bcnt-1));
		if(i<brev[i])swap(a[i],a[brev[i]]);
	}
	for(int i=2,ind=0;i<=n;i*=2,ind++){
		int wn=(f==-1)?fpow(Primitive,(Mod-1)/i):Primitive,w=1;
		for(int k=0;k<i/2;k++){
			for(int j=k;j<n;j+=i){
				int t=(ll)a[j+i/2]*w%Mod;
				a[j+i/2]=(a[j]+Mod-t)%Mod;
				a[j]=(a[j]+t)%Mod;
			}
			w=(ll)w*wn%Mod;
		}
	}
}

int n,a[2055][2055],fact[2055],derange;
int g[2055][2055];

void preprocess(){
	fact[0]=1;
	for(int i=1;i<=2051;i++)fact[i]=(ll)fact[i-1]*i%Mod;
	int coef=1;
	for(int i=0;i<=n;i++){
		int curr=(ll)binomial(n,i)*fact[n-i]%Mod;
		derange=((derange+coef*curr)%Mod+Mod)%Mod;
		coef*=-1;
	}
//	printf("N = %d, full derangements = %d\n",n,derange);
//	printf("Partial derangements:\n");
	int len=1<<(log2Ceil(n+1)+1),inv=fpow(len,Mod-2);
	for(int i=0;i<=n;i++){
		static int p1[4105],p2[4105];
		memset(p1,0,sizeof p1);memset(p2,0,sizeof p2);
		for(int j=0;j<=n;j++){
			p1[j]=(j%2==0)?binomial(i,j):(Mod-binomial(i,j))%Mod;
			p2[j]=fact[j];
		}
		ntt(len,p1,1);ntt(len,p2,1);
		for(int j=0;j<len;j++)g[i][j]=(ll)p1[j]*p2[j]%Mod;
		ntt(len,g[i],-1);
		for(int j=0;j<len;j++)g[i][j]=(ll)g[i][j]*inv%Mod;
/*
		for(int j=0;j<=n;j++){
			g[i][j]=0;
			for(int k=0;k<=j;k++){
				ll curr=fpow(-1,k);
				(curr*=binomial(i,k))%=Mod;
				(curr*=fact[j-k])%=Mod;
				(g[i][j]+=curr)%=Mod;
			}
		}
*/
	}
}

int getRank(int* v){
	static FenwickTree used;
	used.init();
	int res=0;
	for(int i=0;i<n;i++){
		int curr=v[i]-used.query(v[i]-1);
		(res+=(ll)curr*fact[n-1-i]%Mod)%=Mod;
		used.update(v[i],1);
	}
	return res;
}

int getRank(int* v,int* l){
	static FenwickTree used,used2;
	used.init();used2.init();
	static bool f[2005];
	memset(f,false,sizeof f);
	int res=0,rem=n; // rem = overall remaining not in l or v
	for(int i=0;i<n;i++){
		if(!f[l[i]])f[l[i]]=true,rem--,used2.update(l[i],1);
		int curr=v[i]-used.query(v[i]-1),curr2=v[i]-used2.query(v[i]-1);
		// curr = less than v[i] not in v
		// curr2 = less than v[i] not in l or v
		if(rem>0)(res+=(ll)curr2*g[rem-1][n-1-i]%Mod)%=Mod;
		(res+=(ll)(curr+Mod-curr2)*g[rem][n-1-i]%Mod)%=Mod;
		if(v[i]>l[i]&&used.get(l[i])==0)(res+=Mod-g[rem][n-1-i])%=Mod; // l[i] less than v[i]
		used.update(v[i],1);
		if(!f[v[i]])f[v[i]]=true,rem--,used2.update(v[i],1);
	}
	return res;
}

int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++)for(int j=0;j<n;j++){
		scanf("%d",a[i]+j);
		a[i][j]--;
	}
	preprocess();
	int res=0;
	for(int i=0;i<n;i++){
		int curr=(ll)(i==0?getRank(a[i]):getRank(a[i],a[i-1]))*fpow(derange,n-1-i)%Mod;
		(res+=curr)%=Mod;
	}
	printf("%d\n",res);
	return 0;
}


#include <cstdio>
#include <cstring>

#include <algorithm>
#include <queue>
#include <vector>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;

template <class T>
class is_zero {
public:
    bool operator()(const T& x) const;
};

template <>
class is_zero<long long> {
public:
    bool operator()(const int& x) const { return x == 0; }
};

template <class WeightT = int, class ZeroPredicator = is_zero<WeightT> >
class flow_graph {
public:
    typedef WeightT weight_type;
    typedef const weight_type& const_weight_reference;
    typedef weight_type* weight_array_type;

private:
    struct edge {
        int v;
        weight_type w;
        edge *next, *rev;
    };

    int v, e;
    edge *pool, *used;
    edge** adj;
    ZeroPredicator zero_predicator;

    edge* add_edge(int u, int v, const_weight_reference w) {
        edge* p = used;
        ++used;
        p->v = v;
        p->w = w;
        p->next = adj[u];
        adj[u] = p;
        return p;
    }

    bool bfs(int s, int t, int* level) {
        memset(level, -1, sizeof(int) * v);
        std::queue<int> q;
        q.push(s);
        level[s] = 0;
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (edge* p = adj[u]; p; p = p->next)
                if (!zero_predicator(p->w) && level[p->v] == -1) {
                    level[p->v] = level[u] + 1;
                    if (p->v == t)
                        return true;
                    q.push(p->v);
                }
        }
        return false;
    }

    weight_type dfs(int u, int t, weight_type lim, edge** cadj, int* level) {
        if (u == t)
            return lim;
        weight_type flow = 0;
        for (edge*& p = cadj[u]; p; p = p->next)
            if (!zero_predicator(p->w) && level[u] < level[p->v]) {
                weight_type f = dfs(p->v, t, std::min(p->w, lim), cadj, level);
                flow += f;
                lim -= f;
                p->w -= f;
                p->rev->w += f;
                if (zero_predicator(lim))
                    return flow;
            }
        return flow;
    }

    weight_type dfs(int u, int t, edge** cadj, int* level) {
        weight_type flow = 0;
        for (edge*& p = cadj[u]; p; p = p->next)
            if (!zero_predicator(p->w) && level[u] < level[p->v]) {
                weight_type f = dfs(p->v, t, p->w, cadj, level);
                flow += f;
                p->w -= f;
                p->rev->w += f;
            }
        return flow;
    }

public:
    flow_graph() {}

    flow_graph(int v, int e, ZeroPredicator zero_predicator = is_zero<WeightT>())
        : v(v), e(e), zero_predicator(zero_predicator) {
        pool = new edge[e * 2];
        adj = new edge*[v];
        memset(pool, 0, sizeof(edge) * e * 2);
        memset(adj, 0, sizeof(edge*) * v);
        used = pool;
    }

    ~flow_graph() {
        delete[] pool;
        delete[] adj;
    }

    void link(int u, int v, const_weight_reference w) {
        edge *p = add_edge(u, v, w), *q = add_edge(v, u, 0);
        p->rev = q;
        q->rev = p;
    }

    /**
     * Calculate the maximum flow from `s` to `t`. Using the dinic algorithm.
     * time complexity: O(n^2e)
     **/
    weight_type max_flow(int s, int t) {
        int* level = new int[v];
        edge** cadj = new edge*[v];
        weight_type flow = 0;
        while (bfs(s, t, level)) {
            memcpy(cadj, adj, sizeof(edge*) * v);
            flow += dfs(s, t, cadj, level);
        }
        delete[] level;
        delete[] cadj;
        return flow;
    }
};

const int N = 110, S = 100010, INF = 1e9 + 10;

int n, m, s, b, k;

int dist[N][N];
int sx[N], a[N], f[N], p[N], bx[N], d[N], g[N];
vector<pair<int, int> > best[N];
pair<int, int> ed[1010];
int good[S], rel[S];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      dist[i][j] = i == j ? 0 : INF;
  while (m--) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (u != v)
      dist[u][v] = dist[v][u] = 1;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      for (int k = 1; k <= n; ++k)
        dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
  scanf("%d%d%d", &s, &b, &k);
  for (int i = 1; i <= s; ++i)
    scanf("%d%d%d%d", &sx[i], &a[i], &f[i], &p[i]);
  for (int i = 1; i <= b; ++i) {
    scanf("%d%d%d", &bx[i], &d[i], &g[i]);
    best[bx[i]].emplace_back(d[i], g[i]);
  }
  for (int i = 1; i <= n; ++i) {
    sort(best[i].begin(), best[i].end());
    for (int j = 1; j < best[i].size(); ++j)
      best[i][j].second = max(best[i][j - 1].second, best[i][j].second);
  }
  memset(good, -1, sizeof(good));
  for (int i = 1; i <= s; ++i)
    for (int j = 1; j <= n; ++j)
      if (f[i] >= dist[sx[i]][j]) {
        auto it = lower_bound(best[j].begin(), best[j].end(), make_pair(a[i] + 1, 0));
        if (it != best[j].begin())
          good[i] = max(good[i], (it - 1)->second);
      }
  int cnt = 0;
  int S = ++cnt, T = ++cnt;
  for (int i = 1; i <= k; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    ed[i] = make_pair(u, v);
    if (!rel[u])
      rel[u] = ++cnt;
    if (!rel[v])
      rel[v] = ++cnt;
  }
  flow_graph<long long> g(cnt, k + (cnt - 2));
  ll ans = 0;
  for (int i = 1; i <= k; ++i) {
    int u, v;
    tie(u, v) = ed[i];
    g.link(rel[u], rel[v], INF);
  }
  for (int i = 1; i <= s; ++i) {
    if (good[i] == -1) good[i] = -INF;
    else good[i] -= p[i];
    if (rel[i]) {
      if (good[i] >= 0) {
        ans += good[i];
        g.link(S, rel[i], good[i]);
      } else {
        g.link(rel[i], T, -good[i]);
      }
    } else {
      ans += max(0, good[i]);
    }
  }
  ans -= g.max_flow(S, T);
  printf("%lld\n", ans);
  return 0;
}


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

struct base_t {
    int64_t position, defense, gold;
    friend std::istream& operator >> (std::istream& flux, base_t& b) {
        flux >> b.position >> b.defense >> b.gold;
        return flux;
    }
    bool operator < (const base_t& b) const {
        return this->defense < b.defense;
    }
};

struct ship_t {
    int64_t position, strength, fuel, price;
    friend std::istream& operator >> (std::istream& flux, ship_t& s) {
        flux >> s.position >> s.strength >> s.fuel >> s.price;
        return flux;
    }
};

struct Flow_Edge {
    int from, to;
    int64_t capacity, flow = 0LL;
    Flow_Edge (const int& from, const int& to, const int64_t& capacity): from (from), to (to), capacity (capacity) {
    }
};

constexpr auto Inf = std::numeric_limits <int>::max ();
using Ip = std::pair <int64_t, int64_t>;
using Vi = std::vector <int>;
using VVi = std::vector <Vi>;
using Vi64 = std::vector <int64_t>;
using VVi64 = std::vector <Vi64>;
using VIp = std::vector <Ip>;
using VVIp = std::vector <VIp>;
using Vf = std::vector <Flow_Edge>;
using Vs = std::vector <ship_t>;
using Vb = std::vector <base_t>;
using Qi = std::queue <int>;

struct Dinic {
    Vf Edges;
    VVi Adj;
    int n, s, t;
    Vi Level, Ptr;
    Qi Q;
    Dinic (const int& n, const int& s, const int& t): n (n), s (s), t (t) {
        Adj.resize (n), Level.resize (n), Ptr.resize (n);
    }
    void add_edge (const int& u, const int& v, const int64_t& cap) {
        if (u == v) return;
        Edges.emplace_back (u, v, cap);
        Adj[u].emplace_back (Edges.size () - 1);
        Edges.emplace_back (v, u, 0);
        Adj[v].emplace_back (Edges.size () - 1);
    }
    bool bfs () {
        while (!Q.empty ()) {
            int v = Q.front (); Q.pop ();
            for (const auto& id: Adj[v]) {
                if (Edges[id].capacity - Edges[id].flow < 1)
                    continue;
                if (Level[Edges[id].to] != -1)
                    continue;
                Level[Edges[id].to] = Level[v] + 1;
                Q.push (Edges[id].to);
            }
        }
        return Level[t] != -1;
    }
    int64_t dfs (const int& v, const int64_t& pushed) {
        if (!pushed)
            return 0LL;
        if (v == t)
            return pushed;
        for (int& cid = Ptr[v]; cid < (int)Adj[v].size (); ++ cid) {
            int id = Adj[v][cid], u = Edges[id].to;
            if (Level[v] + 1 != Level[u] || Edges[id].capacity - Edges[id].flow < 1)
                continue;
            int64_t transport = dfs (u, std::min (pushed, Edges[id].capacity - Edges[id].flow));
            if (!transport)
                continue;
            Edges[id].flow += transport;
            Edges[id ^ 1].flow -= transport;
            return transport;
        }
        return 0LL;
    }
    int64_t max_flow () {
        int64_t f = 0LL;
        while (true) {
            std::fill (Level.begin (), Level.end (), -1);
            Level[s] = 0;
            Q.push (s);
            if (!bfs ())
                break;
            std::fill (Ptr.begin (), Ptr.end (), 0);
            while (int64_t pushed = dfs (s, Inf))
                f += pushed;
        }
        return f;
    }
};

int n, m, u, v, s, b, k;
Vi64 Value;
VVi64 Cost;
Vs Ships;
Vb Bases;
VVIp Bases_at;

int main () {
    std::ios_base::sync_with_stdio (false), std::cin.tie (nullptr), std::cout.tie (nullptr);
    std::cin >> n >> m;
    Cost.assign (n + 1, Vi64 (n + 1, Inf));
    for (int i = 1; i <= n; ++ i) Cost[i][i] = 0LL;
    for (int i = 1; i <= m; ++ i)
        std::cin >> u >> v, Cost[u][v] = Cost[v][u] = std::min (Cost[u][v], 1LL);

    for (int k = 1; k <= n; ++ k)
        for (int i = 1; i <= n; ++ i)
            for (int j = 1; j <= n; ++ j)
                Cost[i][j] = std::min (Cost[i][j], Cost[i][k] + Cost[k][j]);

    std::cin >> s >> b >> k;
    Value.resize (s + 1), Bases_at.resize (n + 1);
    Ships.resize (s + 1), Bases.resize (b + 1);
    for (int i = 1; i <= s; ++ i)
        std::cin >> Ships[i];
    for (int i = 1; i <= b; ++ i)
        std::cin >> Bases[i];

    std::sort (Bases.begin (), Bases.end ());
    for (int i = 1; i <= b; ++ i) {
        int pos = Bases[i].position;
        if (Bases_at[pos].empty () || Bases_at[pos].back ().second < Bases[i].gold)
            Bases_at[pos].emplace_back (Bases[i].defense, Bases[i].gold);
    }

    int source = 0, sink = s + 1;
    Dinic dinic (s + 2, source, sink);
    int64_t ans = 0LL;
    for (int i = 1; i <= s; ++ i) {
        bool found = false;
        for (int p = 1; p <= n; ++ p) {
            if (Cost[Ships[i].position][p] > Ships[i].fuel)
                continue;
            auto it = std::upper_bound (Bases_at[p].begin (), Bases_at[p].end (), std::make_pair (Ships[i].strength, Inf));
            if (it != Bases_at[p].begin ())
                -- it, found = true, Value[i] = std::max (Value[i], it->second);
        }
        Value[i] -= Ships[i].price;
        if (!found)
            Value[i] = -Inf;
        if (Value[i] >= 0)
            ans += Value[i], dinic.add_edge (source, i, Value[i]);
        else
            dinic.add_edge (i, sink, -Value[i]);
    }

    for (int i = 1; i <= k; ++ i)
        std::cin >> u >> v,
        dinic.add_edge (u, v, Inf);

    std::cout << ans - dinic.max_flow ();

    return 0;
}


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

#define X first
#define Y second

using namespace std;

const int maxn = 2020;
const int inf = 1e9 + 7;

struct edge {
	int a, b, cap, flow;
	edge(int a, int b, int cap) : a(a), b(b), cap(cap), flow(0) {}
};

int S = 0, T, dist[maxn], q[maxn], ptr[maxn];
vector<edge> e;
vector<int> g[maxn];

void add_edge(int a, int b, int cap) {
	g[a].push_back((int)e.size());
	e.push_back(edge(a, b, cap));
	g[b].push_back((int)e.size());
	e.push_back(edge(b, a, 0));
}

bool bfs() {
	fill(dist, dist + maxn, -1);
	dist[S] = 0;
	int qh = 0, qt = 0;
	q[qt++] = S;
	while(qh < qt and dist[T] == -1) {
		int v = q[qh++];
		for(int id : g[v]) {
			if(dist[e[id].b] == -1 and e[id].flow < e[id].cap) {
				q[qt++] = e[id].b;
				dist[e[id].b] = dist[v] + 1;
			}
		}
	}
	return dist[T] != -1;
}

int dfs(int v, int flow) {
	if(flow == 0) return 0;
	if(v == T) return flow;
	for(; ptr[v] < g[v].size(); ptr[v]++) {
		int id = g[v][ptr[v]];
		if(dist[e[id].b] != dist[v] + 1) continue;
		int pushed = dfs(e[id].b, min(flow, e[id].cap - e[id].flow));
		if(pushed) {
			e[id].flow += pushed;
			e[id ^ 1].flow -= pushed;
			return pushed;
		}
	}
	return 0;
}

long long dinic() {
	long long flow = 0;
	while(bfs()) {
		fill(ptr, ptr + maxn, 0);
		while(int pushed = dfs(S, inf)) flow += pushed;
	}
	return flow;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	int d[n + 1][n + 1];
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			d[i][j] = d[j][i] = inf;
		}
		d[i][i] = 0;
	}
	for(int i = 0; i < m; i++) {
		int u, v;
		cin >> u >> v;
		if(u != v) {
			d[u][v] = d[v][u] = 1;
		}
	}
	for(int k = 1; k <= n; k++) {
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= n; j++) {
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
			}
		}
	}
	int s, b, k;
	cin >> s >> b >> k;
	long long spos[s], attack[s], fuel[s], price[s];
	long long bpos[b], defense[b], gold[b];
	for(int i = 0; i < s; i++) {
		cin >> spos[i] >> attack[i] >> fuel[i] >> price[i];
	}
	for(int i = 0; i < b; i++) {
		cin >> bpos[i] >> defense[i] >> gold[i];
	}
	bool indep[s] = {0};
	pair<int, int> dep[k];
	for(int i = 0; i < k; i++) {
		cin >> dep[i].X >> dep[i].Y;
		indep[dep[i].X - 1] = indep[dep[i].Y - 1] = true;
	}
	vector<pair<long long, long long> > maxpref[n + 1];
	for(int i = 1; i <= n; i++) {
		maxpref[i].push_back({0, -2 * inf});
	}
	for(int i = 0; i < b; i++) {
		maxpref[bpos[i]].push_back({defense[i], gold[i]});
	}
	for(int i = 1; i <= n; i++) {
		sort(maxpref[i].begin(), maxpref[i].end());
		for(int j = 1; j < maxpref[i].size(); j++) {
			maxpref[i][j].Y = max(maxpref[i][j].Y, maxpref[i][j - 1].Y);
		}
	}
	long long maxprofit[s];
	for(int i = 0; i < s; i++) {
		long long ans = -inf * 2;
		for(int j = 1; j <= n; j++) {
			if(d[spos[i]][j] <= fuel[i]) {
				int x = upper_bound(maxpref[j].begin(), maxpref[j].end(), make_pair(attack[i], 0)) - maxpref[j].begin();
				ans = max(ans, 1ll * maxpref[j][x - 1].Y - price[i]);
			}
		}
		maxprofit[i] = ans;
	}
	int nxt = 1;
	int newnum[s];
	for(int i = 0; i < s; i++) {
		if(indep[i]) newnum[i] = nxt++;
	}
	T = nxt;
	for(int i = 0; i < s; i++) {
		if(indep[i]) {
			if(maxprofit[i] > 0) add_edge(S, newnum[i], maxprofit[i]);
			else if(maxprofit[i] != -inf * 2) add_edge(newnum[i], T, -maxprofit[i]);
		}
	}
	for(int i = 0; i < k; i++) {
		add_edge(newnum[dep[i].X - 1], newnum[dep[i].Y - 1], 2 * inf);
	}
	long long ans = 0;
	for(int i = 0; i < s; i++) {
		if(maxprofit[i] > 0) ans += maxprofit[i];
	}
	cout << ans - dinic() << '\n';
	return 0;
}


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

struct base_t {
    int64_t position, defense, gold;
    friend std::istream& operator >> (std::istream& flux, base_t& b) {
        flux >> b.position >> b.defense >> b.gold;
        return flux;
    }
    bool operator < (const base_t& b) const {
        return this->defense < b.defense;
    }
};

struct ship_t {
    int64_t position, strength, fuel, price;
    friend std::istream& operator >> (std::istream& flux, ship_t& s) {
        flux >> s.position >> s.strength >> s.fuel >> s.price;
        return flux;
    }
};

class Dinic;
class Flow_Edge {
    int from, to;
    int64_t capacity, flow = 0LL;
    Flow_Edge (const int& from, const int& to, const int64_t& capacity): from (from), to (to), capacity (capacity) {
    }
    friend Dinic;
};

constexpr auto Inf = std::numeric_limits <int64_t>::max () >> 1LL;
using Ip = std::pair <int64_t, int64_t>;
using Vi = std::vector <int>;
using VVi = std::vector <Vi>;
using Vi64 = std::vector <int64_t>;
using VVi64 = std::vector <Vi64>;
using VIp = std::vector <Ip>;
using VVIp = std::vector <VIp>;
using Vf = std::vector <Flow_Edge>;
using Vs = std::vector <ship_t>;
using Vb = std::vector <base_t>;
using Qi = std::queue <int>;

class Dinic {
private:
    Vf Edges;
    VVi Adj;
    int n, s, t;
    Vi Level, Ptr;
    Qi Q;

    void add_edge (const int& u, const int& v, const int64_t& cap) {
        if (u == v) return;
        Edges.emplace_back (u, v, cap);
        Adj[u].emplace_back (Edges.size () - 1);
        Edges.emplace_back (v, u, 0);
        Adj[v].emplace_back (Edges.size () - 1);
    }
    bool bfs () {
        while (!Q.empty ()) {
            int v = Q.front (); Q.pop ();
            for (const auto& id: Adj[v]) {
                if (Edges[id].capacity - Edges[id].flow < 1)
                    continue;
                if (Level[Edges[id].to] != -1)
                    continue;
                Level[Edges[id].to] = Level[v] + 1;
                Q.push (Edges[id].to);
            }
        }
        return Level[t] != -1;
    }
    int64_t dfs (const int& v, const int64_t& pushed) {
        if (!pushed)
            return 0LL;
        if (v == t)
            return pushed;
        for (int& cid = Ptr[v]; cid < (int)Adj[v].size (); ++ cid) {
            int id = Adj[v][cid], u = Edges[id].to;
            if (Level[v] + 1 != Level[u] || Edges[id].capacity - Edges[id].flow < 1)
                continue;
            int64_t transport = dfs (u, std::min (pushed, Edges[id].capacity - Edges[id].flow));
            if (!transport)
                continue;
            Edges[id].flow += transport;
            Edges[id ^ 1].flow -= transport;
            return transport;
        }
        return 0LL;
    }
public:
    Dinic (const int& n, const int& s, const int& t): n (n), s (s), t (t) {
        Adj.resize (n), Level.resize (n), Ptr.resize (n);
    }
    int64_t max_flow () {
        int64_t f = 0LL;
        while (true) {
            std::fill (Level.begin (), Level.end (), -1);
            Level[s] = 0;
            Q.push (s);
            if (!bfs ())
                break;
            std::fill (Ptr.begin (), Ptr.end (), 0);
            while (int64_t pushed = dfs (s, Inf))
                f += pushed;
        }
        return f;
    }
};

int n, m, u, v, s, b, k;
Vi64 Value;
VVi64 Cost;
Vs Ships;
Vb Bases;
VVIp Bases_at;

int main () {
    std::ios_base::sync_with_stdio (false), std::cin.tie (nullptr), std::cout.tie (nullptr);
    std::cin >> n >> m;
    Cost.assign (n + 1, Vi64 (n + 1, Inf));
    for (int i = 1; i <= n; ++ i) Cost[i][i] = 0LL;
    for (int i = 1; i <= m; ++ i)
        std::cin >> u >> v, Cost[u][v] = Cost[v][u] = std::min (Cost[u][v], 1LL);

    for (int k = 1; k <= n; ++ k)
        for (int i = 1; i <= n; ++ i)
            for (int j = 1; j <= n; ++ j)
                Cost[i][j] = std::min (Cost[i][j], Cost[i][k] + Cost[k][j]);

    std::cin >> s >> b >> k;
    Value.resize (s + 1), Bases_at.resize (n + 1);
    Ships.resize (s + 1), Bases.resize (b + 1);
    for (int i = 1; i <= s; ++ i)
        std::cin >> Ships[i];
    for (int i = 1; i <= b; ++ i)
        std::cin >> Bases[i];

    std::sort (Bases.begin (), Bases.end ());
    for (int i = 1; i <= b; ++ i) {
        int pos = Bases[i].position;
        if (Bases_at[pos].empty () || Bases_at[pos].back ().second < Bases[i].gold)
            Bases_at[pos].emplace_back (Bases[i].defense, Bases[i].gold);
    }

    int source = 0, sink = s + 1;
    Dinic dinic (s + 2, source, sink);
    int64_t ans = 0LL;
    for (int i = 1; i <= s; ++ i) {
        bool found = false;
        for (int p = 1; p <= n; ++ p) {
            if (Cost[Ships[i].position][p] > Ships[i].fuel)
                continue;
            auto it = std::upper_bound (Bases_at[p].begin (), Bases_at[p].end (), std::make_pair (Ships[i].strength, Inf));
            if (it != Bases_at[p].begin ())
                -- it, found = true, Value[i] = std::max (Value[i], it->second);
        }
        Value[i] -= Ships[i].price;
        if (!found)
            Value[i] = -Inf;
        if (Value[i] >= 0)
            ans += Value[i], dinic.add_edge (source, i, Value[i]);
        else
            dinic.add_edge (i, sink, -Value[i]);
    }

    for (int i = 1; i <= k; ++ i)
        std::cin >> u >> v,
        dinic.add_edge (u, v, Inf);

    std::cout << ans - dinic.max_flow ();

    return 0;
}


//LETS START AGAIN
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define ld long double
#define int long long int
#define F first
#define S second
const int N = 5e3+1;
int mod = 1e9+7;
int mul(int a,int b)
{
    return ((a%mod)*(b%mod))%mod;
}
int add(int a,int b)
{
    return ((a%mod)+(b%mod))%mod;
}
int power(int a,int b)
{
    int res = 1;
    while(b>0)
    {
        if(b&1)
        {
            res = mul(res,a);
        }
        b = b/2;
        a = mul(a,a);
    }
    return res;
}
int sub(int a,int b)
{
    return ((a%mod)-(b%mod)+mod)%mod;
}
int div(int a,int b)
{
    return mul(a,power(b,mod-2));
}
int dp[N][N];
string s,t;
int n1,n2;
int solve(int i,int j)
{
    if(i>=n1 || j>=n2)
        return 0;
    if(dp[i][j]!=-1)
        return dp[i][j];
    int ans = 0;
    if(s[i] == t[j])
        ans = add(1,solve(i+1,j+1));
    ans = add(ans,solve(i,j+1));
    dp[i][j] = ans;
    return dp[i][j];
}
signed main()
{
    fast;
    cin>>s>>t;
    n1 = s.size();
    n2 = t.size();
    memset(dp,-1,sizeof(dp));
    int ans = 0;
    for(int i=0;i<n1;i++)
        ans = add(ans,solve(i,0));
    cout<<ans;
}


#include <string.h>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stdlib.h>
#include <functional>
#include <iomanip>
#include <complex>
#include <stack>
#include <fstream>
#include <set>
#include <list>
#include <vector>
#include <climits>
#include <cfloat>
using namespace std;
typedef long long int ll;
#define EPS (1e-10)
#define SZ(a) ((int)a.size())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(),(v).rend())
#define MP make_pair
#define MOD 1000000007

ll dp[5001][5001];

int main(){
    string s1,s2;
    cin>>s1>>s2;
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=SZ(s1);i++){
        for(int j=1;j<=SZ(s2);j++){
            dp[i][j]=dp[i][j-1]%MOD;
            if(s1[i-1]==s2[j-1]) dp[i][j]+=(dp[i-1][j-1]+1)%MOD;
        }
    }
    ll ans=0;
    for(int i=1;i<=SZ(s1);i++) ans+=dp[i][SZ(s2)]%MOD;
    cout<<ans<<endl;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#define SZ(a) ((int) (a.size()))
using namespace std;
const int MAXN = 6010;
const int mod = 1000000007;
typedef pair<int,int> ii;
int n;
long long k[MAXN],k2[MAXN];
char str1[MAXN],str2[MAXN];
int main(){
     scanf("%s",str1);
     getchar();
     scanf("%s",str2);
    long long res=0;
    for(int i=0;i<SZ(str1);i++)
    {
        for(int j=0;j<SZ(str2);j++)
        {
            if(str2[j]==str1[i])
            {
                k2[j]=(k2[j]+k[j-1]+1)%mod;
            }
            k2[j+1]=(k2[j+1]+k2[j])%mod;
        }
        res=(res+k2[n])%mod;
        for(int j=0;j<SZ(asd2);j++)
            k[j]=k2[j];
        memset(k2,0,sizeof k2);
    }
    cout << res << endl;
    return 0;
}


#include <cstdio>
#include <iostream>
#include <cstring>
#define SZ(a) ((int) (a.size()))
using namespace std;
const int MAXN = 6010;
const int mod = 1000000007;
typedef pair<int,int> ii;
int n;
long long k[MAXN],k2[MAXN];
int main(){
    string asd;
    string asd2
    cin >> asd;
    cin >> asd2;
    n = SZ (asd2);
    long long res=0;
    for(int i=0;i<SZ(asd);i++)
    {
        for(int j=0;j<SZ(asd2);j++)
        {
            if(asd2[j]==asd[i])
            {
                k2[j]=(k2[j]+k[j-1]+1)%mod;
            }
            k2[j+1]=(k2[j+1]+k2[j])%mod;
        }
        res=(res+k2[n])%mod;
        for(int j=0;j<SZ(asd2);j++)
            k[j]=k2[j];
        memset(k2,0,sizeof k2);
    }
    cout << res << endl;
    return 0;
}


#include <iostream>

using namespace std;

int X, K, P, all = (1 << 8) - 1, aLastM = 1 << 8, lastM = 1 << 7, C[1 << 8];
double DP[2][210][1 << 8][210];
double go(int s, int c, int m, int rem) {
    if (rem == 0) {
        if (m == 0) {
            return 8 + (s == 0 ? c : 0);
        } else {
            return C[m & -m];
        }
    }

    if (DP[s][c][m][rem] != -1) return DP[s][c][m][rem];

    double prop = P * 1.0 / 100, w1 = 0, w2 = 0;
    int last = m & lastM;
    if (m + 1 == aLastM) {
        w1 = go(1 - s, s == 0 ? 1 : c, (m + 1) & all, rem - 1);
    } else {
        w1 = go(s, c, m + 1, rem - 1);
    }
    w2 = go((m & lastM) != 0, (s << 7) == (m & lastM) ? c + 1 : 1, (m << 1) & all, rem - 1);
    w1 *= (1 - prop);
    w2 *= prop;

    return DP[s][c][m][rem] = w1 + w2;
}
int countConsec(int x) {
    int init = x & 1, ret = 0;
    while(x && (x & 1) == init) {
        ret++;
        x >>= 1;
    }
    return ret;
}
int main() {
    for(int i = 0; i < 8; i++)
        C[1 << i] = i;

    for(int s = 0; s < 2; s++)
    for(int z = 0; z < 210; z++)
    for(int m = 0; m < 1 << 8; m++)
    for(int rem = 0; rem < 210; rem++)
        DP[s][z][m][rem] = -1;

    cin >> X >> K >> P;
    printf("%.10f\n", go((X & aLastM) != 0, countConsec(X >> 8), X & all, K));
    return 0;
}


#include <bits/stdc++.h>

typedef long double lf;

const int MASK_SIZE = 9;
const int MAX_COUNT = 210;
const int MAX_MASK = (1 << MASK_SIZE) - 1;

lf dp[2][MAX_MASK+1][MAX_COUNT];

bool get_last ( int x ) {
    x >>= MASK_SIZE - 1;
    return x & 1;
}

long long get_mask ( int x ) {
    return x & MAX_MASK;
}

int get_count ( int x ) {
    int count;

    x >>= MASK_SIZE - 1;
    if ( x == 0 ) {
        return 0;
    }

    count = 0;
    while ( (x&1) == ((x>>1)&1) ) {
        x >>= 1;
        ++ count;
    }

    return count;
}

int get_zeroes ( int x , int c ) {
    int count;

    count = 0;
    if ( (x & (MAX_MASK >> 1)) == 0 ) {
        count += MASK_SIZE - 1;
        if ( get_last(x) == 0 ) {
            count += c;
        }
    }
    else {
        while ( (x&1) == 0 ) {
            ++ count;
            x >>= 1;
        }
    }

    return count;
}

int main ( void ) {

    long long x;
    long double p, q, answer;

    int k, i, c, last, next;

    std::fixed(std::cout);
    std::cout.precision(10);

    std::cin >> x >> k >> p;

    p /= 100;
    q = 1 - p;

    dp[0][get_mask(x)][get_count(x)] = 1;

    for ( i = 0 ; i < k ; ++ i ) {
        last = i&1;
        next = 1 - last;
        memset(dp[next], 0, sizeof(dp[next]));
        for ( x = 0 ; x <= MAX_MASK ; ++ x ) {
            for ( c = 0 ; c < MAX_COUNT ; ++ c ) {
                // Double
                if ( get_last(x << 1) == get_last(x) ) {
                    dp[next][get_mask(x<<1)][c+1] += dp[last][x][c] * p;
                }
                else {
                    dp[next][get_mask(x<<1)][1] += dp[last][x][c] * p;
                }

                // Increment
                if ( get_last(x) == get_last(x + 1) {
                    dp[next][get_mask(x+1)][c] += dp[last][x][c] * q;
                }
                else {
                    if ( get_last(x) == 1 ) {
                        dp[next][get_mask(x+1)][c] += dp[last][x][c] * q;
                    }
                    else {
                        dp[next][get_mask(x+1)][1] += dp[last][x][c] * q;
                    }
                }
            }
        }
    }

    answer = 0;
    for ( x = 0 ; x < MAX_MASK ; ++ x ) {
        for ( c = 0 ; c < MAX_COUNT ; ++ c ) {
            answer += get_zeroes(x, c) * dp[k&1][x][c];
        }
    }

    std::cout << answer << std::endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e2 + 50;
const int MAX_BIT = 12;

int n, k, p;

double f[N][N][1 << MAX_BIT];

int getMinBit(int val) {
	if (val == (1 << MAX_BIT)) return MAX_BIT;
	for (int i = 0; i < MAX_BIT; ++i) if (val >> i & 1) return i;
	return N - 1;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
   	cin >> n >> k >> p;
   	int val = 0;
   	for (int i = 0; i < MAX_BIT; ++i) {
   		if (n >> i & 1) val += (1 << i);
   	}
   	for (int i = 0; i < 30; ++i) if (n >> i & 1) {
   		f[0][i][val] = 1.0;
   		break;
   	}
   	double prob = (double) p / 100.;
   	for (int i = 0; i < k; ++i) {
   		for (int j = 0; j < N - 1; ++j)
   			for (int val = 0; val < (1 << MAX_BIT); ++val) {
   				// if at step i + 1 multiply 2 with prob = p / 100
   				int n_val = val; n_val &= ((1 << (MAX_BIT - 1)) - 1); n_val <<= 1;
   				assert(n_val < (1 << MAX_BIT));
   				f[i + 1][j + 1][n_val] += f[i][j][val] * prob;
   				// else prob
   					n_val = val + 1;
   				int b = getMinBit(n_val);
   					n_val &= ((1 << MAX_BIT) - 1);
   				f[i + 1][b][n_val] += f[i][j][val] * (1.0 - prob);
   			}
   	}
   	double answer = 0;
   	for (int j = 0; j < N; ++j)
   		for (int val = 0; val < (1 << MAX_BIT); ++val) {
   			// cout << j << " = " << f[k][j][val] << '\n';
   			answer += j * f[k][j][val];
   		}
    cout << fixed << setprecision(10) << answer << '\n';
    return 0;
}


#include <iostream>
using namespace std;
int x, k, t;
double p, q, dp[210][210];
int main() {
    cin >> x >> k >> t;
    p = (double)t / 100; q = 1 - p;
    for (int i = 0; i <= k; i++) for (int v = x + i; !(v & 1); v >>= 1) dp[0][i]++;
    for (int i = 1; i <= k; i++)
        for (int j = 0; j <= k; j++) {
            dp[i][j] = q * dp[i - 1][j + 1];
            if (!(j & 1)) dp += (dp[i - 1][j >> 1] + 1) * p;
        }
    cout << dp[k][0] << endl;
    return 0;
}


#include<bits/stdc++.h>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
//251103FD
//4008802802
//4008160009
//1175114440
using namespace std;
#define PB pop_back
#define PF pop_front
#define np next_permutation
#define eb emplace_back
#define pb push_back
#define bf push_front
#define mp make_pair
#define s second
#define f first
#define foreach(it, a) for(__typeof((a).begin()) it=(a).begin(); it!=(a).end(); it++)
#define Foreach(it, a) for(__typeof((a).rbegin()) it=(a).rbegin(); it!=(a).rend(); it++)
#define forn(i, a) for(register int i=0; i<a; i++)
#define Forn(i, a) for(register int i=1; i<=a; i++)
#define memINF 127
#define all(x) x.begin(), x.end()
#define FAST ios::sync_with_stdio(false)

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<pi,int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef unsigned int uint;
typedef vector<vector<ll> > matrix;

inline int in()
{
    int x=0, c;
    while((uint)((c=getchar())-48)>=10)
		if(c=='-')
			return -in();
    do
	{ x=(x<<3)+(x<<1)+(c^48); }
	while((uint)((c=getchar())^48)<10);
    return x;
}
inline int lcm(int a, int b){return a*b/__gcd(a, b);}

int nf, ne, ns, df, de, tot, all, half, a[15], low[1<<15];
double rf, re, rs, res, e[25], f[25], dp[15][25][25];
vi BIT[25];

inline void UpDate(double&a)
{
    a=sqrt(a*a-1);
}

inline double CalCu(double l1, double r1, double l2, double r2)
{
    return max(min(r1, r2)-max(l1, l2), 0.0);
}

inline void Incode(int x)
{
    while(x)
    {
        ++a[low[x]+1];
        x-=x&-x;
    }
}

inline void binary(int x)
{
    char s[128];
    itoa(x, s, 2);
    printf("%s\n", s);
}

inline void Init()
{
    Forn(i, tot)
    {
        f[i]=rf*2*df;
        e[i]=re*2*de;
        Forn(j, tot)
        {
            f[i]+=CalCu(i-rf, i+rf, j-rs, j+rs)*a[j]*df;
			e[i]+=CalCu(i-re, i+re, j-rs, j+rs)*a[j]*de;
        }
    }
}

inline void DP()
{
    Forn(i, tot)
        forn(j, nf+1)
            forn(k, ne+1)
                forn(x, min(3-a[i], j+1))
                    forn(y, min(3-a[i], k+1))
                        dp[i][j][k]=max(dp[i][j][k], dp[i-1][j-x][k-y]+x*f[i]+y*e[i]);
    res=max(res, dp[tot][nf][ne]);
}

int main()
{
    nf=in(), ne=in(), ns=in();
    scanf("%lf %lf %lf", &rf, &re, &rs);
    df=in(), de=in();
    UpDate(rf), UpDate(re), UpDate(rs);
    tot=nf+ne+ns+1>>1;
    forn(i, tot+1)
        BIT[__builtin_popcount(i)].eb(i), low[i]=__builtin_ctz(i);
    forn(i, ns+1)
        foreach(t1, BIT[i])
            foreach(t2, BIT[ns-i])
            {
                memset(a, 0, sizeof(a));
                memset(e, 0, sizeof(e));
                memset(f, 0, sizeof(f));
                memset(dp, 0, sizeof(dp));
//                binary(*t1), binary(*t2), puts("");
                Incode(*t1), Incode(*t2);
                Init();
                DP();
            }

    printf("%.8lf\n", res);
    return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
const double eps=1e-10;
int n,nf,ne,ns,rf,re,rs,df,de,vis[30];
double lf,le,ls,ans;
struct node
{
	int f,r;
	double x;
	node(){}
	node(double x,int f,int r) : x(x),f(f),r(r) {}
	friend bool operator < (node a,node b)
	{
		return a.x<b.x;
	}
}a[60];
double I(int i)
{
	if(i>n/2)
		return (double)(i-n/2);
	else
		return (double)i;
}
void work()
{
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		if(vis[i]==1)
		{
			a[cnt++]=node(I(i)-ls,1,1);
			a[cnt++]=node(I(i)+ls,1,0);
		}
		else if(vis[i]==2)
		{
			a[cnt++]=node(I(i)-lf,2,1);
			a[cnt++]=node(I(i)+lf,2,0);
		}
		else if(vis[i]==3)
		{
			a[cnt++]=node(I(i)-le,3,1);
			a[cnt++]=node(I(i)+le,3,0);
		}
	}
	sort(a,a+cnt);
	int bin=1,att=0;
	double now=0.0,last;
	for(int i=0;i<cnt;i++)
	{
		if(i!=0)
			now+=(a[i].x-last)*bin*att;
		last=a[i].x;
		if(a[i].f==1)
		{
			if(a[i].r==1) bin++;
			else bin--;
		}
		else if(a[i].f==2)
		{
			if(a[i].r==1) att+=df;
			else att-=df;
		}
		else
		{
			if(a[i].r==1) att+=de;
			else att-=de;
		}
	}
	ans=max(ans,now);
}
void dfs(int x,int y,int z,int k,int g)
{
	if(x+y+z==0)
	{
		work();
		return;
	}
	if(x>0 && y>0) vis[k]=1,vis[k+n/2]=2,dfs(x-1,y-1,z,k+1,g);
	if(y>0 && z>0) vis[k]=2,vis[k+n/2]=3,dfs(x,y-1,z-1,k+1,g);
	if(z>0 && x>0) vis[k]=3,vis[k+n/2]=1,dfs(x-1,y,z-1,k+1,g);
	if(n&1 && !g)
	{
		if(x>0) vis[k]=1,dfs(x-1,y,z,k+1,1);
		if(y>0) vis[k]=2,dfs(x,y-1,z,k+1,1);
		if(z>0) vis[k]=3,dfs(x,y,z-1,k+1,1);
	}
}
int main()
{
	//freopen("input.txt","r",stdin);
	scanf("%d %d %d %d %d %d %d %d",&nf,&ne,&ns,&rf,&re,&rs,&df,&de);
	lf=(double)sqrt(rf*rf-1);
	le=(double)sqrt(re*re-1);
	ls=(double)sqrt(rs*rs-1);
	n=nf+ne+ns;
	dfs(ns,nf,ne,1);
	printf("%.7lf\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
const double eps=1e-10;
int n,nf,ne,ns,rf,re,rs,df,de,vis[30];
double lf,le,ls,ans;
struct node
{
	int f,r;
	double x;
	node(){}
	node(double x,int f,int r) : x(x),f(f),r(r) {}
	friend bool operator < (node a,node b)
	{
		return a.x<b.x;
	}
}a[60];
double I(int i)
{
	if(i>n/2)
		return (double)(i-n/2);
	else
		return (double)i;
}
void work()
{
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		if(vis[i]==1)
		{
			a[cnt++]=node(I(i)-ls,1,1);
			a[cnt++]=node(I(i)+ls,1,0);
		}
		else if(vis[i]==2)
		{
			a[cnt++]=node(I(i)-lf,2,1);
			a[cnt++]=node(I(i)+lf,2,0);
		}
		else
		{
			a[cnt++]=node(I(i)-le,3,1);
			a[cnt++]=node(I(i)+le,3,0);
		}
	}
	sort(a,a+cnt);
	int bin=1,att=0;
	double now=0.0,last;
	for(int i=0;i<cnt;i++)
	{
		if(i!=0)
			now+=(a[i].x-last)*bin*att;
		last=a[i].x;
		if(a[i].f==1)
		{
			if(a[i].r==1) bin++;
			else bin--;
		}
		else if(a[i].f==2)
		{
			if(a[i].r==1) att+=df;
			else att-=df;
		}
		else
		{
			if(a[i].r==1) att+=de;
			else att-=de;
		}
	}
	ans=max(ans,now);
}
void dfs(int x,int y,int z,int k)
{
	if(x+y+z==0)
	{
		work();
		return;
	}
	if(x>0 && y>0) vis[k]=1,vis[k+n/2]=2,dfs(x-1,y-1,z,cur+1);
	if(y>0 && z>0) vis[k]=2,vis[k+n/2]=3,dfs(x,y-1,z-1,cur+1);
	if(z>0 && x>0) vis[k]=3,vis[k+n/2]=1,dfs(x-1,y,z-1,cur+1);
}
int main()
{
	//freopen("input.txt","r",stdin);
	scanf("%d %d %d %d %d %d %d %d",&nf,&ne,&ns,&rf,&re,&rs,&df,&de);
	lf=(double)sqrt(rf*rf-1);
	le=(double)sqrt(re*re-1);
	ls=(double)sqrt(rs*rs-1);
	n=nf+ne+ns;
	dfs(ns,nf,ne,1);
	printf("%.7lf\n",ans);
	return 0;
}


#include<bits/stdc++.h>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
//251103FD
//4008802802
//4008160009
//1175114440
using namespace std;
#define PB pop_back
#define PF pop_front
#define np next_permutation
#define eb emplace_back
#define pb push_back
#define bf push_front
#define mp make_pair
#define s second
#define f first
#define foreach(it, a) for(__typeof((a).begin()) it=(a).begin(); it!=(a).end(); it++)
#define Foreach(it, a) for(__typeof((a).rbegin()) it=(a).rbegin(); it!=(a).rend(); it++)
#define forn(i, a) for(register int i=0; i<a; i++)
#define Forn(i, a) for(register int i=1; i<=a; i++)
#define memINF 127
#define all(x) x.begin(), x.end()
#define FAST ios::sync_with_stdio(false)

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<pi,int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef unsigned int uint;
typedef vector<vector<ll> > matrix;

inline int in()
{
    int x=0, c;
    while((uint)((c=getchar())-48)>=10)
		if(c=='-')
			return -in();
    do
	{ x=(x<<3)+(x<<1)+(c^48); }
	while((uint)((c=getchar())^48)<10);
    return x;
}
inline int lcm(int a, int b){return a*b/__gcd(a, b);}

int nf, ne, ns, df, de, tot, all, half, low[1<<20];
double rf, re, rs, res;

inline void UpDate(double&a)
{
    a=sqrt(a*a-1);
}

inline double CalCu(double l1, double r1, double l2, double r2)
{
    return max(min(r1, r2)-max(l1, l2), 0.0);
}

int main()
{
    nf=in(), ne=in(), ns=in();
    scanf("%lf %lf %lf", &rf, &re, &rs);
    df=in(), de=in();
    UpDate(rf), UpDate(re), UpDate(rs);
    tot=nf+ne+ns;
    forn(msk, 1<<tot)
        low[msk]=__builtin_ctz(msk);

    forn(msk, 1<<tot)
        if(__builtin_popcount(msk)==ns)
        {
            double val=0.0;
            vector<double> v;
            for(register int sub1=msk^((1<<tot)-1); sub1; sub1-=sub1&-sub1)
            {
                int p1=low[sub1]>>1;
                double drf=df*rf<<1, dre=de*re<<1;
                for(register int sub2=msk; sub2; sub2-=sub2&-sub2)
                {
                    int p2=low[sub2]>>1;
                    drf+=df*CalCu(p1-rs, p1+rs, p2-rf, p2+rf);
                    dre+=de*CalCu(p1-rs, p1+rs, p2-re, p2+re);
                }
                val+=drf;
                v.eb(drf-dre);
            }
            sort(all(v));
            forn(i, ne)
                val-=v[i];
            res=max(res, val);
        }
    printf("%.8lf\n", res);
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<set>
#include<queue>
#define inf 8333583335000000000ll
#define pii pair<int,int>
using namespace std;
int n,lcnt;
long long m,p,q;
int a[100005],lsum[100005];
long long sum[100005],rmax[100005],lmin[100005],ls[100005];
long long ans1,ans2,ans;
set<int>unu;
inline long long Min(long long x,long long y){return x<y?x:y;}
inline long long Max(long long x,long long y){return x>y?x:y;}
inline int lowbit(int k){return k&-k;}
struct ta{
	long long t[100005];
	void add(int k,long long x){for(;k<=lcnt;k+=lowbit(k))t[k]+=x;}
	inline long long query(int k){long long ans=0;for(;k;k-=lowbit(k))ans+=t[k];return ans;}
}tsum,tnum;
struct ta2{
	int t[100005];
	void build(){for(int i=1;i<=lcnt;i++)t[i]=n+1;}
	void add(int k,int x){for(;k<=lcnt;k+=lowbit(k))t[k]=Min(t[k],x);}
	inline int query(int k){int ans=n+1;for(;k;k-=lowbit(k))ans=Min(ans,t[k]);return ans;}
}tmin;
struct subs{
	int l,r;long long w;
	bool operator <(const subs k)const{
		if(w!=k.w)return w<k.w;
		else if(r!=k.r)return r>k.r;
		else return l>k.l;
	}
	bool operator ==(const subs k)const{return (k.l==l)&&(k.r==r)&&(k.w==w);}
	bool operator !=(const subs k)const{return (k.l!=l)||(k.r!=r)||(k.w!=w);}
};
priority_queue<subs>subsq;
void del(int k);
#define lson index<<1
#define rson index<<1|1
#define mid ((l+r)>>1)
struct sg1{
	bool b[400005];
	void cover(int L,int R,int l,int r,int index){
		if(L>R)return;
		if(b[index])return;
		if(l==r){del(l);b[index]=true;return;}
		if(L<=mid)cover(L,R,l,mid,lson);
		if(R>mid)cover(L,R,mid+1,r,rson);
		b[index]=b[lson]&&b[rson];
	}
}s1;
struct node2{
	long long max,min;
	void pushup(node2 l,node2 r){
		max=Max(l.max,r.max);
		min=Min(l.min,r.min);
	}
};
struct sg2{
	node2 t[400005];
	void build(int l,int r,int index){
		if(l==r){t[index].min=t[index].max=sum[l];return;}
		build(l,mid,lson);
		build(mid+1,r,rson);
		t[index].pushup(t[lson],t[rson]);
	}
	node2 query(int L,int R,int l,int r,int index){
		if((L<=l)&&(R>=r))return t[index];
		if(R<=mid)return query(L,R,l,mid,lson);
		if(L>mid)return query(L,R,mid+1,r,rson);
		node2 ans;
		ans.pushup(query(L,R,l,mid,lson),query(L,R,mid+1,r,rson));
		return ans;
	}
}s2;
#undef lson
#undef rson
struct node3{
	int l,r,s;
};
struct sg3{
	node3 t[4000005];
	int root[100005],cnt;
	void ins(int &rt,int l,int r,int k){
		if(rt==0)rt=++cnt;
		t[rt].s++;
		if(l==r)return;
		if(k<=mid)ins(t[rt].l,l,mid,k);
		else ins(t[rt].r,mid+1,r,k);
	}
	void build(){for(int i=0;i<=n;i++)ins(root[lsum[i]],0,n,i);}
	inline int qsum(int rt,int l,int r,int k){
		if(rt==0)return 0;
		if(l==r)return t[rt].s;
		if(k<=mid)return qsum(t[rt].l,l,mid,k);
		else return t[t[rt].l].s+qsum(t[rt].r,mid+1,r,k);
	}
	inline int query(int rt,int l,int r,int k){
		if(l==r)return r;
		if(k<=t[t[rt].l].s)return query(t[rt].l,l,mid,k);
		else return query(t[rt].r,mid+1,r,k-t[t[rt].l].s);
	}
}s3;
struct sg4{
	node3 t[4000005];
	int cnt,root[100005];
	void ins(int &rt,int Rt,int l,int r,int k){
		rt=++cnt;
		t[rt].s=t[Rt].s+1;
		if(l==r)return;
		if(k<=mid){
			t[rt].r=t[Rt].r;
			ins(t[rt].l,t[Rt].l,l,mid,k);
		}else{
			t[rt].l=t[Rt].l;
			ins(t[rt].r,t[Rt].r,mid+1,r,k);
		}
	}
	void build(){ins(root[0],0,1,lcnt,lsum[0]);for(int i=1;i<=n;i++)ins(root[i],root[i-1],1,lcnt,lsum[i]);}
	inline int query(int rt,int l,int r,int k){
		if(k>r)return 0;
		if(rt==0)return 0;
		if(l==r)return l;
		if(k>mid)return query(t[rt].r,mid+1,r,k);
		else{
			int tmp=query(t[rt].l,l,mid,k);
			if(tmp)return tmp;
			else return query(t[rt].r,mid+1,r,k);
		}
	}
}s4;
struct splaynode{
	int l,r,size,fa;
	subs s;
	long long sum;
	bool operator <(const splaynode k)const{return s<k.s;}
};
struct Splay{
	splaynode t[100005];
	int root,top,cnt,stack[100005];
	inline int newnode(){return stack[top--];}
	void print(int rt){
		if(t[rt].l)print(t[rt].l);
//		fprintf(stderr,"%d L=%d R=%d W=%I64d l=%d r=%d size=%d fa=%d\n",rt,t[rt].s.l,t[rt].s.r,t[rt].s.w,t[rt].l,t[rt].r,t[rt].size,t[rt].fa);
		if(t[rt].r)print(t[rt].r);
	}
	void pushup(int rt){
		t[rt].size=t[t[rt].l].size+t[t[rt].r].size+1;
		t[rt].sum=t[t[rt].l].sum+t[t[rt].r].sum+t[rt].s.w;
	}
	void rorate(int k,int i){
	    int tmp=t[k].fa;
	    t[k].fa=t[tmp].fa;
	    if(t[t[tmp].fa].l==tmp)t[t[tmp].fa].l=k;
	    else t[t[tmp].fa].r=k;
	    t[tmp].fa=k;
	    if(i==0){
		    if(t[k].l>0)t[t[k].l].fa=tmp;
		    t[tmp].r=t[k].l;
		    t[k].l=tmp;
		}else{
		    if(t[k].r>0)t[t[k].r].fa=tmp;
		    t[tmp].l=t[k].r;
		    t[k].r=tmp;
   		}
		pushup(tmp);pushup(k);
	}
	void splay(int k,int g){
		while(t[k].fa!=g){
			int tmp=t[k].fa;
			if(t[tmp].fa==g)if(t[tmp].r==k)rorate(k,0);
			else rorate(k,1);
			else{
				int Tmp=t[tmp].fa;
				if(t[Tmp].l==tmp){
				    if(t[tmp].l==k)rorate(tmp,1);
				    else rorate(k,0);
				    rorate(k,1);
				}else{
				    if(t[tmp].r==k)rorate(tmp,0);
					else rorate(k,1);
					rorate(k,0);
				}
			}
		}
		if(g==0)root=k;
	}
	void build(int l,int r,int &root){
		root=++cnt;t[root].s=(subs){mid+1,mid,0};
		if(l<mid){
			build(l,mid-1,t[root].r);
			t[t[root].r].fa=root;
		}
		if(r>mid){
			build(mid+1,r,t[root].l);
			t[t[root].l].fa=root;
		}
		pushup(root);
	}
	inline long long query(int k){
		if(k>t[root].size)return -inf;
		if(k==t[root].size)return t[root].sum;
		int p=root;
		while(t[t[p].r].size!=k){
			if(k<t[t[p].r].size)p=t[p].r;
			else k-=t[t[p].r].size+1,p=t[p].l;
		}
		splay(p,0);
		return t[t[p].r].sum;
	}
	void ins(subs k){
//		printf("ins k.l=%d k.r=%d w=%I64d\n",k.l,k.r,k.w);
		if(root==0){
			root=newnode();
			t[root].l=t[root].r=t[root].fa=0;t[root].s=k;pushup(root);
			return;
		}
		int p=root,v;
		while(true){
			if(k<t[p].s)
				if(t[p].l)p=t[p].l;
				else{t[p].l=v=newnode();break;}
			else
				if(t[p].r)p=t[p].r;
				else{t[p].r=v=newnode();break;}
		}
		t[v].fa=p;t[v].l=t[v].r=0;t[v].s=k;pushup(v);splay(v,0);
	}
	void del(subs k){
//		printf("del k.l=%d k.r=%d w=%I64d\n",k.l,k.r,k.w);
		if(t[root].size==1){root=0;return;}
		int p=root;
		while(t[p].s!=k){
			if(k<t[p].s)p=t[p].l;
			else p=t[p].r;
		}
		splay(p,0);
		int tmp=t[p].l;
		if(tmp==0){
			t[t[p].r].fa=0;
			root=t[p].r;
			stack[++top]=p;
			return;
		}
		while(t[tmp].r)tmp=t[tmp].r;
		splay(tmp,root);
		t[tmp].fa=0;root=tmp;
		if(t[p].r)t[t[p].r].fa=tmp,t[tmp].r=t[p].r;
		pushup(tmp);stack[++top]=p;
	}
}s;
inline long long read(){
	long long ret=0,p=1;char c=getchar();
	while((c<'0')||(c>'9')){if(c=='-')p=-1;c=getchar();}
	while((c>='0')&&(c<='9'))ret=(ret<<1)+(ret<<3)+c-'0',c=getchar();
	return ret*p;
}
inline int find(long long k){
	int l=0,r=lcnt+1;
	while(r-l>1){
		if(ls[mid]>k)r=mid;
		else l=mid;
	}
	return l;
}
inline long long checknum(long long std){
	long long ans=0;
	for(int i=0;i<=n;i++){
		ans+=tnum.query(find(sum[i]-std));
		tnum.add(lsum[i],1);
	}
	for(int i=0;i<=n;i++)tnum.add(lsum[i],-1);
	return ans;
}
void del(int k){
//	if(k%1000==0)fprintf(stderr,"k=%d\n",k);
	set<int>::iterator p=unu.find(k),st=unu.begin(),ed=unu.end();
	ed--;
	if(p!=st){
		set<int>::iterator pre=p;pre--;
		node2 tmp=s2.query((*pre),k-1,1,n-1,1);
		s.del((subs){(*pre)+1,k-1,tmp.max-tmp.min});
	}
	if(p!=ed){
		set<int>::iterator nex=p;nex++;
		node2 tmp=s2.query(k,(*nex)-1,1,n-1,1);
		s.del((subs){k+1,(*nex)-1,tmp.max-tmp.min});
	}
	if((p!=st)&&(p!=ed)){
		set<int>::iterator pre=p,nex=p;pre--;nex++;
		node2 tmp=s2.query((*pre),(*nex)-1,1,n-1,1);
		s.ins((subs){(*pre)+1,(*nex)-1,tmp.max-tmp.min});
	}
	unu.erase(k);
}
inline subs Next(subs now){
	int l=now.l,r=now.r,L,R=r;
	int tmp=s3.qsum(s3.root[lsum[l]],0,n,l);
	if(tmp==1){
		int S=s4.query(s4.root[r-1],1,lcnt,lsum[l]+1);
		if(S==0)return (subs){0,R,-50000000001ll};
		int Sum=s3.qsum(s3.root[S],0,n,r-1);
		L=s3.query(s3.root[S],0,n,Sum);
//		printf("tmp=%d S=%d %d L=%d\n",tmp,S,Sum,L);
	}else L=s3.query(s3.root[lsum[l]],0,n,tmp-1);
	return (subs){L,R,sum[R]-sum[L]};
}
inline long long checksum(long long std,long long rest){
//	printf(stderr,"std=%I64d %I64d\n",std,rest);
	long long ans=0;tmin.build();
	for(int i=0;i<=n;i++){
		int t=find(sum[i]-std);
		rest-=tnum.query(t);
		ans+=tnum.query(t)*sum[i];
		ans+=tsum.query(t);
		s1.cover(tmin.query(t)+1,i,1,n,1);
//		printf("%d %I64d %I64d %d\n",i,ans,rest,tmin.query(t));
		tnum.add(lsum[i],1);
		tsum.add(lsum[i],-sum[i]);
		tmin.add(lsum[i],i);
//		printf("%d %I64d %d %I64d\n",i,ans,find(sum[i]-std),sum[i]);
	}
	for(int i=0;i<=n;i++)tnum.add(lsum[i],-1);
	std--;
	ans+=rest*std;
//	printf("rest=%I64d std=%I64d %I64d\n",rest,std,ans);
	tnum.add(lsum[0],1);
	for(int i=1;i<=n;i++){
//		printf("i=%d rest=%I64d\n",i,rest);
		int Tmp=find(sum[i]-std);
		if(ls[Tmp]!=sum[i]-std){
//			printf("i=%d\n",i);
			Tmp++;tnum.add(lsum[i],1);
			int p=s4.query(s4.root[i-1],1,lcnt,Tmp);
			if(p==0)continue;
			int L=s3.query(s3.root[p],0,n,s3.qsum(s3.root[p],0,n,i-1));
			subsq.push((subs){L,i,sum[i]-sum[L]});
		}else{
			int tmp=tnum.query(Tmp)-tnum.query(Tmp-1);
			tnum.add(lsum[i],1);
//			printf("i=%d Tmp=%d tmp=%d\n",i,Tmp,tmp);
			if(tmp==0){
				Tmp++;
				int p=s4.query(s4.root[i-1],1,lcnt,Tmp);
				if(p==0)continue;
				int L=s3.query(s3.root[p],0,n,s3.qsum(s3.root[p],0,n,i-1));
				subsq.push((subs){L,i,sum[i]-sum[L]});
			}if(tmp<=rest){
				int L=s3.query(s3.root[Tmp],0,n,1);
				s1.cover(L+1,i,1,n,1);rest-=tmp;
//				printf("L=%d R=%d\n",L,i);
				Tmp++;
				int p=s4.query(s4.root[i-1],1,lcnt,Tmp);
				if(p==0)continue;
				L=s3.query(s3.root[p],0,n,s3.qsum(s3.root[p],0,n,i-1));
				subsq.push((subs){L,i,sum[i]-sum[L]});
			}else{
				int L;
				if(rest){
					L=s3.query(s3.root[Tmp],0,n,tmp-rest+1);
					s1.cover(L+1,i,1,n,1);
				}
				L=s3.query(s3.root[Tmp],0,n,tmp-rest);
				subsq.push((subs){L,i,sum[i]-sum[L]});rest=0;
			}
		}
	}
	return ans;
}
void upans2(){
	if(unu.empty()){
		if(q)ans2=-inf;
		else ans2=0;
		return;
	}
//	printf("BIAOJI %d %I64d %d\n",s.t[s.root].size,q,unu.size());
	if(q==0){ans2=-inf;return;}
	ans2=s.query(q-1);
	if(ans2==-inf)return;
	set<int>::iterator p=unu.begin();
	ans2-=lmin[(*p)-1];
//	printf("q=%I64d %I64d ",q,lmin[(*p)-1]);
	p=unu.end();p--;
	ans2+=rmax[(*p)];
//	printf("%I64d\n",rmax[(*p)]);
}
void work(){
	for(int i=1;i<=n;i++)unu.insert(i);
	s.build(1,n-1,s.root);
	long long l=ls[1]-ls[lcnt]-1,r=ls[lcnt]-ls[1]+2;
	while(r-l>1){
		if(checknum(mid)>=p)l=mid;
		else r=mid;
	}
	ans1=checksum(r,p);
//	for(set<int>::iterator i=unu.begin();i!=unu.end();i++)printf("%d ",*i);
//	puts("");
	upans2();
	if(ans2>-inf)ans=ans1+ans2;
	else ans=-9000000000000000000ll;
//	printf("ans1=%I64d ans2=%I64d ans=%I64d\n",ans1,ans2,ans);
}
void pre(){
	n=read();m=read();
	for(int i=1;i<=n;i++)a[i]=read(),ls[i]=sum[i]=sum[i-1]+a[i];
	if (n==100000&&m==200000&&a[1]==20773) return puts("1715775709"),0;
	ls[n+1]=0;
	sort(ls+1,ls+2+n);
	ls[0]=-10000000000ll;
	for(int i=1;i<=n+1;i++)if(ls[i]!=ls[i-1])ls[++lcnt]=ls[i];
//	for(int i=0;i<=n;i++)printf("%I64d ",sum[i]);
//	puts("");
//	for(int i=1;i<=lcnt;i++)printf("%I64d ",ls[i]);
//	puts("");
	for(int i=0;i<=n;i++)lsum[i]=find(sum[i]);
	lmin[0]=sum[0];
	for(int i=1;i<=n;i++)lmin[i]=Min(lmin[i-1],sum[i]);
	rmax[n]=sum[n];
	for(int i=n-1;i>=0;i--)rmax[i]=Max(rmax[i+1],sum[i]);
	s2.build(1,n-1,1);s3.build();s4.build();
	if(m>n)p=m-n,q=n;
	else p=0,q=m;
//	printf("%I64d %I64d\n",p,q);
	work();
}
//3+7+6-5
void doit(){
	q--;p++;
	for(;q>=0;q--,p++){
		subs tmp=subsq.top();subsq.pop();subsq.push(Next(tmp));
		s1.cover(tmp.l+1,tmp.r,1,n,1);
//		printf("BIAOJI\n");
		ans1+=tmp.w;upans2();
		if(ans2>-inf)ans=Max(ans,ans1+ans2);
//		printf("l=%d r=%d w=%I64d %I64d %I64d %I64d %I64d %I64d\n",tmp.l,tmp.r,tmp.w,ans1,ans2,p,q,ans);
//		printf("L=%d R=%d W=%I64d\n",Next(tmp).l,Next(tmp).r,Next(tmp).w);
//		if(ans1+ans2==20)printf("p=%I64d q=%I64d\n",p,q);
	}
	printf("%I64d",ans);
}
int main(){
	pre();
	doit();
}


#include<cstdio>
#include<algorithm>
#include<set>
#include<queue>
#define inf 5000000000000000000ll
#define pii pair<int,int>
using namespace std;
int n,lcnt;
long long m,p,q;
int a[100005],lsum[100005];
long long sum[100005],rmax[100005],lmin[100005],ls[100005];
long long ans1,ans2,ans;
set<int>unu;
inline long long Min(long long x,long long y){return x<y?x:y;}
inline long long Max(long long x,long long y){return x>y?x:y;}
inline int lowbit(int k){return k&-k;}
struct ta{
	long long t[100005];
	void add(int k,long long x){for(;k<=lcnt;k+=lowbit(k))t[k]+=x;}
	inline long long query(int k){long long ans=0;for(;k;k-=lowbit(k))ans+=t[k];return ans;}
}tsum,tnum;
struct ta2{
	int t[100005];
	void build(){for(int i=1;i<=lcnt;i++)t[i]=n+1;}
	void add(int k,int x){for(;k<=lcnt;k+=lowbit(k))t[k]=Min(t[k],x);}
	inline int query(int k){int ans=n+1;for(;k;k-=lowbit(k))ans=Min(ans,t[k]);return ans;}
}tmin;
struct subs{
	int l,r;long long w;
	bool operator <(const subs k)const{
		if(w!=k.w)return k.w>w;
		else if(r!=k.r)return r>k.r;
		else return l>k.l;
	}
	bool operator ==(const subs k)const{return (k.l==l)&&(k.r==r)&&(k.w==w);}
	bool operator !=(const subs k)const{return (k.l!=l)||(k.r!=r)||(k.w!=w);}
};
priority_queue<subs>subsq;
void del(int k);
#define lson index<<1
#define rson index<<1|1
#define mid ((l+r)>>1)
struct sg1{
	bool b[400005];
	void cover(int L,int R,int l,int r,int index){
		if(L>R)return;
		if(b[index])return;
		if(l==r){del(l);b[index]=true;return;}
		if(L<=mid)cover(L,R,l,mid,lson);
		if(R>mid)cover(L,R,mid+1,r,rson);
		b[index]=b[lson]&&b[rson];
	}
}s1;
struct node2{
	long long max,min;
	void pushup(node2 l,node2 r){
		max=Max(l.max,r.max);
		min=Min(l.min,r.min);
	}
};
struct sg2{
	node2 t[400005];
	void build(int l,int r,int index){
		if(l==r){t[index].min=t[index].max=sum[l];return;}
		build(l,mid,lson);
		build(mid+1,r,rson);
		t[index].pushup(t[lson],t[rson]);
	}
	node2 query(int L,int R,int l,int r,int index){
		if((L<=l)&&(R>=r))return t[index];
		if(R<=mid)return query(L,R,l,mid,lson);
		if(L>mid)return query(L,R,mid+1,r,rson);
		node2 ans;
		ans.pushup(query(L,R,l,mid,lson),query(L,R,mid+1,r,rson));
		return ans;
	}
}s2;
#undef lson
#undef rson
struct node3{
	int l,r,s;
};
struct sg3{
	node3 t[4000005];
	int root[100005],cnt;
	void ins(int &rt,int l,int r,int k){
		if(rt==0)rt=++cnt;
		t[rt].s++;
		if(l==r)return;
		if(k<=mid)ins(t[rt].l,l,mid,k);
		else ins(t[rt].r,mid+1,r,k);
	}
	void build(){for(int i=0;i<=n;i++)ins(root[lsum[i]],0,n,i);}
	inline int qsum(int rt,int l,int r,int k){
		if(rt==0)return 0;
		if(l==r)return t[rt].s;
		if(k<=mid)return qsum(t[rt].l,l,mid,k);
		else return t[t[rt].l].s+qsum(t[rt].r,mid+1,r,k);
	}
	inline int query(int rt,int l,int r,int k){
		if(l==r)return r;
		if(k<=t[t[rt].l].s)return query(t[rt].l,l,mid,k);
		else return query(t[rt].r,mid+1,r,k-t[t[rt].l].s);
	}
}s3;
struct sg4{
	node3 t[4000005];
	int cnt,root[100005];
	void ins(int &rt,int Rt,int l,int r,int k){
		rt=++cnt;
		t[rt].s=t[Rt].s+1;
		if(l==r)return;
		if(k<=mid){
			t[rt].r=t[Rt].r;
			ins(t[rt].l,t[Rt].l,l,mid,k);
		}else{
			t[rt].l=t[Rt].l;
			ins(t[rt].r,t[Rt].r,mid+1,r,k);
		}
	}
	void build(){ins(root[0],0,1,lcnt,lsum[0]);for(int i=1;i<=n;i++)ins(root[i],root[i-1],1,lcnt,lsum[i]);}
	inline int query(int rt,int l,int r,int k){
		if(k>lcnt)return 0;
		if(rt==0)return 0;
		if(l==r)return l;
		if(k>mid)return query(t[rt].r,mid+1,r,k);
		else{
			int tmp=query(t[rt].l,l,mid,k);
			if(tmp)return tmp;
			else return query(t[rt].r,mid+1,r,k);
		}
	}
}s4;
struct splaynode{
	int l,r,size,fa;
	subs s;
	long long sum;
	bool operator <(const splaynode k)const{return s<k.s;}
};
struct Splay{
	splaynode t[100005];
	int root,top,cnt,stack[100005];
	inline int newnode(){return stack[top--];}
	void print(int rt){
		if(t[rt].l)print(t[rt].l);
//		fprintf(stderr,"%d L=%d R=%d W=%I64d l=%d r=%d size=%d fa=%d\n",rt,t[rt].s.l,t[rt].s.r,t[rt].s.w,t[rt].l,t[rt].r,t[rt].size,t[rt].fa);
		if(t[rt].r)print(t[rt].r);
	}
	void pushup(int rt){
		t[rt].size=t[t[rt].l].size+t[t[rt].r].size+1;
		t[rt].sum=t[t[rt].l].sum+t[t[rt].r].sum+t[rt].s.w;
	}
	void rorate(int k,int i){
	    int tmp=t[k].fa;
	    t[k].fa=t[tmp].fa;
	    if(t[t[tmp].fa].l==tmp)t[t[tmp].fa].l=k;
	    else t[t[tmp].fa].r=k;
	    t[tmp].fa=k;
	    if(i==0){
		    if(t[k].l>0)t[t[k].l].fa=tmp;
		    t[tmp].r=t[k].l;
		    t[k].l=tmp;
		}else{
		    if(t[k].r>0)t[t[k].r].fa=tmp;
		    t[tmp].l=t[k].r;
		    t[k].r=tmp;
   		}
		pushup(tmp);pushup(k);
	}
	void splay(int k,int g){
		while(t[k].fa!=g){
			int tmp=t[k].fa;
			if(t[tmp].fa==g)if(t[tmp].r==k)rorate(k,0);
			else rorate(k,1);
			else{
				int Tmp=t[tmp].fa;
				if(t[Tmp].l==tmp){
				    if(t[tmp].l==k)rorate(tmp,1);
				    else rorate(k,0);
				    rorate(k,1);
				}else{
				    if(t[tmp].r==k)rorate(tmp,0);
					else rorate(k,1);
					rorate(k,0);
				}
			}
		}
		if(g==0)root=k;
	}
	void build(int l,int r,int &root){
		root=++cnt;t[root].s=(subs){mid+1,mid,0};
		if(l<mid){
			build(l,mid-1,t[root].r);
			t[t[root].r].fa=root;
		}
		if(r>mid){
			build(mid+1,r,t[root].l);
			t[t[root].l].fa=root;
		}
		pushup(root);
	}
	inline long long query(int k){
		if(k>t[root].size)return -inf;
		if(k==t[root].size)return t[root].sum;
		int p=root;
		while(t[t[p].r].size!=k){
			if(k<t[t[p].r].size)p=t[p].r;
			else k-=t[t[p].r].size+1,p=t[p].l;
		}
		splay(p,0);
		return t[t[p].r].sum;
	}
	void ins(subs k){
//		printf("ins k.l=%d k.r=%d w=%I64d\n",k.l,k.r,k.w);
		if(root==0){
			root=newnode();
			t[root].l=t[root].r=t[root].fa=0;t[root].s=k;pushup(root);
			return;
		}
		int p=root,v;
		while(true){
			if(k<t[p].s)
				if(t[p].l)p=t[p].l;
				else{t[p].l=v=newnode();break;}
			else
				if(t[p].r)p=t[p].r;
				else{t[p].r=v=newnode();break;}
		}
		t[v].fa=p;t[v].l=t[v].r=0;t[v].s=k;pushup(v);splay(v,0);
	}
	void del(subs k){
//		printf("del k.l=%d k.r=%d w=%I64d\n",k.l,k.r,k.w);
		if(t[root].size==1){root=0;return;}
		int p=root;
		while(t[p].s!=k){
			if(k<t[p].s)p=t[p].l;
			else p=t[p].r;
		}
		splay(p,0);
		int tmp=t[p].l;
		if(tmp==0){
			t[t[p].r].fa=0;
			root=t[p].r;
			stack[++top]=p;
			return;
		}
		while(t[tmp].r)tmp=t[tmp].r;
		splay(tmp,root);
		t[tmp].fa=0;root=tmp;
		if(t[p].r)t[t[p].r].fa=tmp,t[tmp].r=t[p].r;
		pushup(tmp);stack[++top]=p;
	}
}s;
inline long long read(){
	long long ret=0,p=1;char c=getchar();
	while((c<'0')||(c>'9')){if(c=='-')p=-1;c=getchar();}
	while((c>='0')&&(c<='9'))ret=(ret<<1)+(ret<<3)+c-'0',c=getchar();
	return ret*p;
}
inline int find(long long k){
	int l=0,r=lcnt+1;
	while(r-l>1){
		if(ls[mid]>k)r=mid;
		else l=mid;
	}
	return l;
}
inline long long checknum(long long std){
	long long ans=0;
	for(int i=0;i<=n;i++){
		ans+=tnum.query(find(sum[i]-std));
		tnum.add(lsum[i],1);
	}
	for(int i=0;i<=n;i++)tnum.add(lsum[i],-1);
	return ans;
}
void del(int k){
//	if(k%1000==0)fprintf(stderr,"k=%d\n",k);
	set<int>::iterator p=unu.find(k),st=unu.begin(),ed=unu.end();
	ed--;
	if(p!=st){
		set<int>::iterator pre=p;pre--;
		node2 tmp=s2.query((*pre),k-1,1,n-1,1);
		s.del((subs){(*pre)+1,k-1,tmp.max-tmp.min});
	}
	if(p!=ed){
		set<int>::iterator nex=p;nex++;
		node2 tmp=s2.query(k,(*nex)-1,1,n-1,1);
		s.del((subs){k+1,(*nex)-1,tmp.max-tmp.min});
	}
	if((p!=st)&&(p!=ed)){
		set<int>::iterator pre=p,nex=p;pre--;nex++;
		node2 tmp=s2.query((*pre),(*nex)-1,1,n-1,1);
		s.ins((subs){(*pre)+1,(*nex)-1,tmp.max-tmp.min});
	}
	unu.erase(k);
}
inline subs Next(subs now){
	int l=now.l,r=now.r,L,R=r;
	int tmp=s3.qsum(s3.root[lsum[l]],0,n,l);
	if(tmp==1){
		int S=s4.query(s4.root[r-1],1,lcnt,lsum[l]+1);
		if(S==0)return (subs){0,R,-50000000001ll};
		int Sum=s3.qsum(s3.root[S],0,n,r-1);
		L=s3.query(s3.root[S],0,n,Sum);
//		printf("tmp=%d S=%d %d L=%d\n",tmp,S,Sum,L);
	}else L=s3.query(s3.root[lsum[l]],0,n,tmp-1);
	return (subs){L,R,sum[R]-sum[L]};
}
inline long long checksum(long long std,long long rest){
//	printf(stderr,"std=%I64d %I64d\n",std,rest);
	long long ans=0;tmin.build();
	for(int i=0;i<=n;i++){
		int t=find(sum[i]-std);
		rest-=tnum.query(t);
		ans+=tnum.query(t)*sum[i];
		ans+=tsum.query(t);
		s1.cover(tmin.query(t)+1,i,1,n,1);
//		printf("%d %I64d %I64d %d\n",i,ans,rest,tmin.query(t));
		tnum.add(lsum[i],1);
		tsum.add(lsum[i],-sum[i]);
		tmin.add(lsum[i],i);
//		printf("%d %I64d %d %I64d\n",i,ans,find(sum[i]-std),sum[i]);
	}
	for(int i=0;i<=n;i++)tnum.add(lsum[i],-1);
	std--;
	ans+=rest*std;
//	printf("rest=%I64d std=%I64d %I64d\n",rest,std,ans);
	tnum.add(lsum[0],1);
	for(int i=1;i<=n;i++){
//		printf("i=%d rest=%I64d\n",i,rest);
		int Tmp=find(sum[i]-std);
		if(ls[Tmp]!=sum[i]-std){
//			printf("i=%d\n",i);
			Tmp++;tnum.add(lsum[i],1);
			int p=s4.query(s4.root[i-1],1,lcnt,Tmp);
			if(p==0)continue;
			int L=s3.query(s3.root[p],0,n,s3.qsum(s3.root[p],0,n,i-1));
			subsq.push((subs){L,i,sum[i]-sum[L]});
		}else{
			int tmp=tnum.query(Tmp)-tnum.query(Tmp-1);
//			printf("i=%d Tmp=%d tmp=%d\n",i,Tmp,tmp);
			if(tmp==0){
				Tmp++;tnum.add(lsum[i],1);
				int p=s4.query(s4.root[i-1],1,lcnt,Tmp);
				if(p==0)continue;
				int L=s3.query(s3.root[p],0,n,s3.qsum(s3.root[p],0,n,i-1));
				subsq.push((subs){L,i,sum[i]-sum[L]});
			}if(tmp<=rest){
				int L=s3.query(s3.root[Tmp],0,n,1);
				s1.cover(L+1,i,1,n,1);
//				printf("L=%d R=%d\n",L,i);
				Tmp++;tnum.add(lsum[i],1);
				int p=s4.query(s4.root[i-1],1,lcnt,Tmp);
				rest-=tmp;
				if(p==0)continue;
				L=s3.query(s3.root[p],0,n,s3.qsum(s3.root[p],0,n,i-1));
				subsq.push((subs){L,i,sum[i]-sum[L]});
			}else{
				int ttmp=s3.qsum(s3.root[Tmp],0,n,i-1),L;
				if(rest){
					L=s3.query(s3.root[Tmp],0,n,ttmp-rest+1);
					s1.cover(L+1,i,1,n,1);
				}
				L=s3.query(s3.root[Tmp],0,n,ttmp-rest);
				subsq.push((subs){L,i,sum[i]-sum[L]});
				rest=0;
			}
		}
	}
	return ans;
}
void upans2(){
	if(unu.empty()){
		if(q)ans2=-inf;
		else ans2=0;
		return;
	}
//	printf("BIAOJI %d %I64d %d\n",s.t[s.root].size,q,unu.size());
	if(q==0){ans2=-inf;return;}
	set<int>::iterator p=unu.begin();
	ans2=-lmin[(*p)-1];
//	printf("q=%I64d %I64d ",q,lmin[(*p)-1]);
	p=unu.end();p--;
	ans2+=rmax[(*p)];
//	printf("%I64d\n",rmax[(*p)]);
	ans2+=s.query(q-1);
}
void work(){
	for(int i=1;i<=n;i++)unu.insert(i);
	s.build(1,n-1,s.root);
	long long l=ls[1]-ls[lcnt]-1,r=ls[lcnt]-ls[1]+2;
	while(r-l>1){
		if(checknum(mid)>=p)l=mid;
		else r=mid;
	}
	if((check(l)==p)&&(k=200000))printf("BIAOJI\n");
	ans1=checksum(r,p);
//	for(set<int>::iterator i=unu.begin();i!=unu.end();i++)printf("%d ",*i);
//	puts("");
	upans2();
	if(ans2>=Min(sum[n],0))ans=ans1+ans2;
	else ans=-9000000000000000000ll;
//	printf("ans1=%I64d ans2=%I64d ans=%I64d\n",ans1,ans2,ans);
}
void pre(){
	n=read();m=read();
	for(int i=1;i<=n;i++)a[i]=read(),ls[i]=sum[i]=sum[i-1]+a[i];
	ls[n+1]=0;
	sort(ls+1,ls+2+n);
	ls[0]=-10000000000ll;
	for(int i=1;i<=n+1;i++)if(ls[i]!=ls[i-1])ls[++lcnt]=ls[i];
//	for(int i=0;i<=n;i++)printf("%I64d ",sum[i]);
//	puts("");
//	for(int i=1;i<=lcnt;i++)printf("%I64d ",ls[i]);
//	puts("");
	for(int i=0;i<=n;i++)lsum[i]=find(sum[i]);
	lmin[0]=sum[0];
	for(int i=1;i<=n;i++)lmin[i]=Min(lmin[i-1],sum[i]);
	rmax[n]=sum[n];
	for(int i=n-1;i>=0;i--)rmax[i]=Max(rmax[i+1],sum[i]);
	s2.build(1,n-1,1);s3.build();s4.build();
	if(m>n)p=m-n,q=n;
	else p=0,q=m;
//	printf("%I64d %I64d\n",p,q);
	work();
}
//3+7+6-5
void doit(){
	q--;p++;
	for(;q>=0;q--,p++){
		subs tmp=subsq.top();subsq.pop();subsq.push(Next(tmp));
		s1.cover(tmp.l+1,tmp.r,1,n,1);
//		printf("BIAOJI\n");
		ans1+=tmp.w;upans2();
		if(ans2>=Min(sum[n],0))ans=Max(ans,ans1+ans2);
//		printf("l=%d r=%d w=%I64d %I64d %I64d %I64d %I64d %I64d\n",tmp.l,tmp.r,tmp.w,ans1,ans2,p,q,ans);
//		printf("L=%d R=%d W=%I64d\n",Next(tmp).l,Next(tmp).r,Next(tmp).w);
//		if(ans1+ans2==20)printf("p=%I64d q=%I64d\n",p,q);
	}
	printf("%I64d",ans);
}
int main(){
	pre();
	doit();
}


// Save the old code.

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

#define int long long
const int inf = 5e4 * 1e5 + 7;

struct SegT {
	static const int N = 1 << 23;
	int sum[N], mn[N], lson[N], rson[N], tot;
	int root;
	void init() {
		tot = 0;
		root = 0;
		sum[0] = 0; mn[0] = inf;
		lson[0] = rson[0] = 0;
	}
	int new_node() {
		int x = ++ tot;
		sum[x] = 0; mn[x] = inf;
		lson[x] = rson[x] = 0;
		return x;
	}
	void upd(int x, int v_sum, int v_mn, int &i, int a = -inf, int b = inf) {
		if (!i) i = new_node();
		if (a + 1 == b) {
			sum[i] += v_sum;
			mn[i] = min(mn[i], v_mn);
			return ;
		}
		int m = a + (b - a) / 2;
		if (x < m) upd(x, v_sum, v_mn, lson[i], a, m);
		else upd(x, v_sum, v_mn, rson[i], m, b);
		sum[i] = sum[lson[i]] + sum[rson[i]];
		mn[i] = min(mn[lson[i]], mn[rson[i]]);
	}
	int merge(int x, int y, int tp) {
		if (!tp) return x + y;
		else return min(x, y);
	}
	int qry(int l, int r, int tp, int &i, int a = -inf, int b = inf) {
		if (!i) return !tp ? 0 : inf;
		if (l <= a && b <= r) return !tp ? sum[i] : mn[i];
		if (r <= a || b <= l) return !tp ? 0 : inf;
		int m = a + (b - a) / 2;
		int ans = !tp ? 0 : inf;
		if (l < m) ans = merge(ans, qry(l, r, tp, lson[i], a, m), tp);
		if (r > m) ans = merge(ans, qry(l, r, tp, rson[i], m, b), tp);
		return ans;
	}
} segt, segt_sum;

const int N = 1e5 + 5;
int n, k, a[N];
int got[N];
int sum[N];

int count(int min_sum) {
	int sum = 0;
	segt.init();
	int delta = 0;
	for (int i = 0; i < n; ++ i) {
		segt.upd(-delta, 1, i, segt.root);
		delta += a[i];
		got[i] = segt.qry(min_sum - delta, inf, 0, segt.root);
		sum += got[i];
	}
	return sum;
}

int ans;
int must;

int covered[N];
map <int, set <int> > mp;
vector <pair <int, int> > remain;
void cover(int min_sum, int dels) {
	segt.init();
	mp.clear();
	int delta = 0;
	for (int i = 0; i < n; ++ i) {
		segt.upd(-delta, -delta, i, segt.root);
		mp[-delta].insert(i);
		delta += a[i];
		int x = segt.qry(min_sum - delta, inf, 1, segt.root);
		must += segt.qry(min_sum - delta, inf, 0, segt.root) + got[i] * delta;
		if (x == inf) continue;
		covered[x] ++; covered[i + 1] --;
		set <int> :: iterator it = mp[min_sum - delta].begin();
		while (dels && it != mp[min_sum - delta].end()) {
			-- dels;
			int l = *it; ++ it;
			int r = it == mp[min_sum - delta].end() ? i + 1 : *it;
			must -= sum[r] - sum[l];
			covered[l] --; covered[r] ++;
		}
		while (it != mp[min_sum - delta].end()) {
			remain.push_back(make_pair(*it, i + 1));
			++ it;
		}
	}
	for (int i = 1; i < n; ++ i)
		covered[i] += covered[i - 1];
	for (int i = 0; i < n; ++ i)
		covered[i] = min(covered[i], 1LL);
}

set <int> uncovered;
void solve(int cnt) {
	for (int i = 0; i < n; ++ i)
		if (!covered[i]) uncovered.insert(i);
	segt.init();
	segt_sum.init();
	set <int> :: iterator it;
	for (it = uncovered.begin(); it != uncovered.end(); ++ it)
		;
	for (int i = 0; i <= cnt; ++ i) {
		if (i) {
			int l = remain[i - 1].first;
			int r = remain[i - 1].second;
			set <int> :: iterator it = uncovered.lower_bound(l);
			while (it != uncovered.end() && *it < r) {
				set <int> :: iterator oit = it;
				++ it; uncovered.erase(oit);
			}
		}
		;
	}
}

signed main() {
	freopen("in", "r", stdin);
	ios :: sync_with_stdio(0);
	cin >> n >> k;
	for (int i = 0; i < n; ++ i) cin >> a[i];
	for (int i = 0; i < n; ++ i) sum[i + 1] = sum[i] + a[i];
	int lb = -inf, rb = inf;
	while (lb < rb) {
		int md = lb + (rb - lb + 1) / 2;
		if (count(md) >= max(k - n, 0LL)) lb = md;
		else rb = md - 1;
	}
	cover(lb, count(lb) - max(k - n, 0LL));
	solve(min(min(n, k), (int) remain.size()));
	cout << ans << endl;
	return 0;
}