#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#incldue <algorithm>
#define lowbit(x) x&-x

using namespace std;

map <long long ,int>p;
int n,k,tot=0,cnt=0,m;
long long a[100010];
long long num[100010];
long long c[100010];
int rmn[101000];
long long all,ans;

int query(int x)
{
    long long res=0;
    for(int i=x;i;i-=lowbit(i))
        res+=c[i];
    return res;
}

void update(int x,int p)
{
    for(int i=x;i<=m;i+=lowbit(i))
        c[i]+=p;
}

int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%I64d",&a[i]),num[++tot]=a[i];
    sort(num+1,num+tot+1);
    for(int i=1;i<=tot;i++)
        if(num[i]!=num[i-1]) p[num[i]]=++cnt;
        else p[num[i]]=cnt;
    m=cnt;
    for(int i=1;i<=n;i++)
        a[i]=p[a[i]];
    for(int i=n;i>=1;i--)
    {
        int temp=query(a[i]-1);
        rmn[i]=temp;
        all+=rmn[i];
        update(a[i],1);
    }
    memset(c,0,sizeof c);
    if(all>k)
    {
        int l=1,r=2;
        for(;;)
        {
            update(a[l],1);
            while(r<=n&&all>k)
            {
                all-=l-query(a[r]-1)+rmn[r];
                r++;
            }
            ans+=n-r;
            l++;
            if(l==n) break;
            all+=query(a[l]);
        }
    }
    else ans=(n-1)*n/2;
    printf("%d ",ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <algorithm>
#define lowbit(x) x&-x

using namespace std;

map <long long ,int>p;
int n,tot=0,cnt=0,m;
long long k;
long long a[100010];
long long num[100010];
long long c[100010];
long long d[100010];
long long all,ans;

long long query(int x)
{
    long long res=0;
    for(int i=x;i;i-=lowbit(i))
        res+=c[i];
    return res;
}

void update(int x,int p)
{
    for(int i=x;i<=m;i+=lowbit(i))
        c[i]+=p;
}

long long query1(int x)
{
    long long res=0;
    for(int i=x;i;i-=lowbit(i))
        res+=d[i];
    return res;
}

void update1(int x,int p)
{
    for(int i=x;i<=m;i+=lowbit(i))
        d[i]+=p;
}

int main()
{
    scanf("%d%I64d",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%I64d",&a[i]),num[++tot]=a[i];
    sort(num+1,num+tot+1);
    for(int i=1;i<=tot;i++)
        if(num[i]!=num[i-1]) p[num[i]]=++cnt;
        else p[num[i]]=cnt;
    m=cnt;
    for(int i=1;i<=n;i++)
        a[i]=p[a[i]];
    for(int i=n;i>=1;i--)
    {
        all+=query(a[i]-1);
        update(a[i],1);
    }
    memset(c,0,sizeof c);
    for(int i=2;i<=n;i++) update1(a[i],1);
    if(all>k)
    {
        int l=1,r=2;
        for(;;)
        {
            update(a[l],1);
            while(r<n&&all>k)
            {
                all-=l-query(a[r])+query1(a[r]-1);
                update1(a[r],-1);
                r++;
            }
            if(r==n&&all>k) break;
            ans+=n-r+1;
            l++;
            all+=l-1-query(a[l])+query1(a[l]-1);
        }
    }
    else ans=(n-1)*n/2;
    printf("%d\n",ans);
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int Mn = 100010;
typedef long long lint;
int n,m;
lint k;
int a[Mn],b[Mn],p[Mn];
lint sl[Mn],sr[Mn];
lint tl[Mn],tr[Mn];
inline void updatal(int x, int y)
{
    for(;x <= m;x += x&-x)
    sl[x] += y;
}
inline lint getl(int x)
{
    lint res(0);
    for(;x > 0;x -= x&-x)
    res += sl[x];
    return res;
}
inline void updatar(int x, int y)
{
    for(;x <= m;x += x&-x)
    sr[x] += y;
}
inline lint getr(int x)
{
    lint res(0);
    for(;x > 0;x -= x&-x)
    res += sr[x];
    return res;
}
int main()
{
    scanf("%d%I64d",&m,&k);
    n = m;
    for(int i = 1; i <= n; i++)
        scanf("%d",&a[i]),b[i] = a[i];
    sort(b+1,b+n+1);
    m = unique(b+1,b+m)-b-1;
    for(int i = 1;i <= n; i++)
    p[i] = lower_bound(b+1,b+m+1,a[i])-b;
    int l,r;
    l = 1;
    r = 2;
    for(int i = 1; i <= n; i++)
    {
        tl[i] = i-getl(p[i])-1 + tl[i-1];
        updatal(p[i],1);
    }
    for(int i = n; i >= 2; i--)
    {
        tr[i] = getr(p[i]-1) + tr[i+1];
        updatar(p[i],1);
    }
    memset(sl,0,sizeof sl);
    lint mid(0);
    lint ans(0);
    for(int l = 1; l < n; l++)
    {
        mid += getr(p[l]-1);
        updatal(p[l],1);
        while((tl[l] + tr[r] + mid > k || l >= r)&& r <= n)
        {
            updatar(p[r],-1);
            mid -= (l-getl(p[r]));
            r++;
        }
        if(r > n) break;
        if(tl[l] + tr[r] + mid <= k && l < r)
            ans += (n-r+1);
    }
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int Mn = 100010;
typedef long long lint;
int n,m;
lint k;
int a[Mn],b[Mn],p[Mn];
lint sl[Mn],sr[Mn];
lint tl[Mn],tr[Mn];
inline void updatal(int x, int y)
{
    for(;x <= m;x += x&-x)
    sl[x] += y;
}
inline lint getl(int x)
{
    lint res(0);
    for(;x > 0;x -= x&-x)
    res += sl[x];
    return res;
}
inline void updatar(int x, int y)
{
    for(;x <= m;x += x&-x)
    sr[x] += y;
}
inline lint getr(int x)
{
    lint res(0);
    for(;x > 0;x -= x&-x)
    res += sr[x];
    return res;
}
int main()
{
    scanf("%d%I64d",&m,&k);
    n = m;
    for(int i = 1; i <= n; i++)
        scanf("%d",&a[i]),b[i] = a[i];
    sort(b+1,b+n+1);
    m = unique(b+1,b+m+1)-b-1;
    for(int i = 1;i <= n; i++)
    p[i] = lower_bound(b+1,b+m+1,a[i])-b;
    int l,r;
    l = 1;
    r = 2;
    for(int i = n; i >= 2; i--)
    {
        tr[i] = getr(p[i]-1) + tr[i+1];
        updatar(p[i],1);
    }
    memset(sl,0,sizeof sl);
    lint mid(0);
    lint ans(0);
    for(int l = 1; l < n; l++)
    {
        tl[i] = i-getl(p[i])-1 + tl[i-1];
        mid += getr(p[l]-1);
        updatal(p[l],1);
        while((tl[l] + tr[r] + mid > k || l >= r) && r <= n)
        {
            updatar(p[r],-1);
            mid -= (l-getl(p[r]));
            r++;
        }
        if(r > n) break;
        if(tl[l] + tr[r] + mid <= k && l < r)
            ans += (n-r+1);
        //printf("%d %d \n",l,r);
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <map>
#include <queue>
#include<set>
#include<stack>
#include<cstdio>
#include <unordered_map>
#include <functional>
#define PI 3.14159265
using namespace std;

#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 100011;
int pre[maxn << 2];
int sub[maxn << 2];

int n;
int a[maxn];

void PushUp(int* seg, int rt)
{
	seg[rt] = seg[rt << 1] + seg[rt << 1 | 1];
}

void build(int* seg, int l, int r, int rt)
{
	if (l == r)
	{
		seg[l] = 0;
		return;
	}
	int m = (l + r) >> 1;
	build(seg,l, m, rt << 1);
	build(seg,m + 1, r, rt << 1 | 1);
}

void update(int* seg,int p,int c, int l, int r, int rt)
{
	if (l == r)
	{
		seg[rt] += c;
		return;
	}
	int m = (l + r) >> 1;
	if (p <= m) update(seg,p, c, l, m, rt << 1);
	else update(seg,p, c, m + 1, r, rt << 1 | 1);
	PushUp(seg,rt);
}

int query(int* seg, int L, int R, int l, int r, int rt)
{
	if (L <= l && r <= R)
	{
		return seg[rt];
	}
	int m = (l + r) >> 1;
	int ans = 0;
	if (L <= m) ans += query(seg, L, R, l, m, rt << 1);
	if (R > m) ans += query(seg, L, R, m + 1, r, rt << 1 | 1);
	return ans;
}
int main(){
	std::ios_base::sync_with_stdio(false);
	long long k;
	cin >> n>>k;
	vector<int> v;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		v.push_back(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	for (int i = 1; i <= n; i++)
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin()+1;

	int m = v.size()+1;
	build(pre, 1, m, 1);
	build(sub, 1, m, 1);
	long long cur = 0;
	for (int i = 1; i <= n; i++){
		cur +=(long long) query(pre, a[i]+1, m, 1, m, 1);
		update(pre, a[i], 1, 1, m, 1);
	}
	long long ans = 0;
	update(pre, a[n], -1, 1, m, 1);
	update(sub, a[n], 1, 1, m, 1);
	for (int l = n-1,  r=n; l>=1; l--){
		while (r <= n && cur>k){
			update(sub, a[r], -1, 1, m, 1);
			cur -= (long long)query(pre, a[r] + 1, m,1,m,1);
			if(a[r]>1)cur -= (long long)query(sub, 1,a[r]-1, 1, m, 1);
			r++;
		}
		//cout <<cur<<" "<< l << " " << r << " " << n << endl;
		ans += n + 1 - r;
		//l--
		update(pre, a[l], -1, 1, m, 1);
		cur -= (long long)query(pre, a[l] + 1, m, 1, m, 1);
		if(a[l]>1) cur -= (long long)query(sub, 1, a[l]-1, 1, m, 1);
		while (r > l-1 + 1 && cur <= k){
			r--;
			update(sub, a[r], 1, 1, m, 1);
			cur += (long long)query(pre, a[r] + 1, m, 1, m, 1);
			if (a[r]>1)cur += (long long)query(sub, 1, a[r] - 1, 1, m, 1);
		}
	}
	cout << ans << endl;
	//system("PAUSE");
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <map>
#include <queue>
#include<set>
#include<stack>
#include<cstdio>
#include <unordered_map>
#include <functional>
#define PI 3.14159265
using namespace std;

#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 100011;
int pre[maxn << 2];
int sub[maxn << 2];

int n;
int a[maxn];

void PushUp(int* seg, int rt)
{
	seg[rt] = seg[rt << 1] + seg[rt << 1 | 1];
}

void build(int* seg, int l, int r, int rt)
{
	if (l == r)
	{
		seg[l] = 0;
		return;
	}
	int m = (l + r) >> 1;
	build(seg,l, m, rt << 1);
	build(seg,m + 1, r, rt << 1 | 1);
}

void update(int* seg,int p,int c, int l, int r, int rt)
{
	if (l == r)
	{
		seg[rt] += c;
		return;
	}
	int m = (l + r) >> 1;
	if (p <= m) update(seg,p, c, l, m, rt << 1);
	else update(seg,p, c, m + 1, r, rt << 1 | 1);
	PushUp(seg,rt);
}

int query(int* seg, int L, int R, int l, int r, int rt)
{
	if (L <= l && r <= R)
	{
		return seg[rt];
	}
	int m = (l + r) >> 1;
	int ans = 0;
	if (L <= m) ans += query(seg, L, R, l, m, rt << 1);
	if (R > m) ans += query(seg, L, R, m + 1, r, rt << 1 | 1);
	return ans;
}
int main(){
	std::ios_base::sync_with_stdio(false);
	long long k;
	cin >> n>>k;
	vector<int> v;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		v.push_back(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	for (int i = 1; i <= n; i++)
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin()+1;

	int m = v.size()+1;
	build(pre, 1, m, 1);
	build(sub, 1, m, 1);
	long long cur = 0;
	for (int i = 1; i <= n; i++){
		cur +=(long long) query(pre, a[i]+1, m, 1, m, 1);
		update(pre, a[i], 1, 1, m, 1);
	}
	long long ans = 0;
	update(pre, a[n], -1, 1, m, 1);
	update(sub, a[n], 1, 1, m, 1);
	for (int l = n-1,  r=n; l>=1; l--){
		while (r <= n && cur>k){
			update(sub, a[r], -1, 1, m, 1);
			cur -= (long long)query(pre, a[r] + 1, m,1,m,1);
			if(a[r]>1)cur -= (long long)query(sub, 1,a[r]-1, 1, m, 1);
			r++;
		}
		cout <<cur<<" "<< l << " " << r << " " << n << endl;
		ans += n + 1 - r;
		//l--
		update(pre, a[l], -1, 1, m, 1);
		cur -= (long long)query(pre, a[l] + 1, m, 1, m, 1);
		if(a[l]>1) cur -= (long long)query(sub, 1, a[l]-1, 1, m, 1);
		while (r > l-1 + 1 && cur <= k){
			r--;
			update(sub, a[r], 1, 1, m, 1);
			cur += (long long)query(pre, a[r] + 1, m, 1, m, 1);
			if (a[r]>1)cur += (long long)query(sub, 1, a[r] - 1, 1, m, 1);
		}
	}
	cout << ans << endl;
	//system("PAUSE");
	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <map>
#include <queue>
#include<set>
#include<stack>
#include<cstdio>
#include <unordered_map>
#include <functional>
#define PI 3.14159265
using namespace std;

#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 100011;
int pre[maxn << 2];
int sub[maxn << 2];

int n;
int a[maxn];

void PushUp(int* seg, int rt)
{
	seg[rt] = seg[rt << 1] + seg[rt << 1 | 1];
}

void build(int* seg, int l, int r, int rt)
{
	if (l == r)
	{
		seg[l] = 0;
		return;
	}
	int m = (l + r) >> 1;
	build(seg,l, m, rt << 1);
	build(seg,m + 1, r, rt << 1 | 1);
}

void update(int* seg,int p,int c, int l, int r, int rt)
{
	if (l == r)
	{
		seg[rt] += c;
		return;
	}
	int m = (l + r) >> 1;
	if (p <= m) update(seg,p, c, l, m, rt << 1);
	else update(seg,p, c, m + 1, r, rt << 1 | 1);
	PushUp(seg,rt);
}

int query(int* seg, int L, int R, int l, int r, int rt)
{
	if (L <= l && r <= R)
	{
		return seg[rt];
	}
	int m = (l + r) >> 1;
	int ans = 0;
	if (L <= m) ans += query(seg, L, R, l, m, rt << 1);
	if (R > m) ans += query(seg, L, R, m + 1, r, rt << 1 | 1);
	return ans;
}
int main(){
	std::ios_base::sync_with_stdio(false);
	long long k;
	cin >> n>>k;
	vector<int> v;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
		v.push_back(a[i]);
	}
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	for (int i = 1; i <= n; i++)
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin()+1;

	int m = v.size()+1;
	build(pre, 1, m, 1);
	build(sub, 1, m, 1);
	long long cur = 0;
	for (int i = 1; i <= n; i++){
		cur +=(long long) query(pre, a[i]+1, m, 1, m, 1);
		update(pre, a[i], 1, 1, m, 1);
	}
	long long ans = 0;
	update(pre, a[n], -1, 1, m, 1);
	update(sub, a[n], 1, 1, m, 1);
	for (int l = n-1,  r=n; l>=1; l--){
		while (r <= n && cur>k){
			update(sub, a[r], -1, 1, m, 1);
			cur -= (long long)query(pre, a[r] + 1, m,1,m,1);
			if(a[r]>1)cur -= (long long)query(sub, 1,a[r]-1, 1, m, 1);
			r++;
		}
		//cout << l << " " << r << " " << n << endl;
		ans += n + 1 - r;
		//l--
		update(pre, a[l], -1, 1, m, 1);
		cur -= (long long)query(pre, a[l] + 1, m, 1, m, 1);
		if(a[l]>1) cur -= (long long)query(sub, 1, a[l]-1, 1, m, 1);
		while (r > l-1 + 1 && cur < k){
			r--;
			update(sub, a[r], 1, 1, m, 1);
			cur += (long long)query(pre, a[r] + 1, m, 1, m, 1);
			if (a[r]>1)cur += (long long)query(sub, 1, a[r] - 1, 1, m, 1);
		}
	}
	cout << ans << endl;
	//system("PAUSE");
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define lowbit(i) (i&(-i))
#define LL long long
using namespace std;
const int N=100005;
const int M=5000000;
int n,m,a[N];
LL k;
vector<int>v;
struct persistent_tree{
    int lson[M],rson[M],c[M],T[M];
    int tot,m;
    void Init(int t,int _m){
        tot=0;m=_m;
        T[t]=bulid(1,m);
    }
    int bulid(int l,int r){
        int root=tot++;
        c[root]=0;
        if(l!=r){
            int m=(l+r)>>1;
            lson[root]=bulid(l,m);
            rson[root]=bulid(m+1,r);
        }
        return root;
    }
    int update(int root,int pos,int val){
        int newroot=tot++,tmp=newroot;
        c[newroot]=c[root]+val;
        int l=1,r=m;
        while(l<r){
            int mid=(l+r)>>1;
            if(pos<=mid){
                rson[newroot]=rson[root];
                lson[newroot]=tot++;
                newroot=lson[newroot];
                root=lson[root];
                r=mid;
            }
            else{
                lson[newroot]=lson[root];
                rson[newroot]=tot++;
                newroot=rson[newroot];
                root=rson[root];
                l=mid+1;
            }
            c[newroot]=c[root]+val;
        }
        return tmp;
    }
    int query(int root,int l,int r,int L,int R){
        if(R<L) return 0;
        if(l==L&&r==R)
            return c[root];
        int m=(l+r)>>1;
        if(R<=m) return query(lson[root],l,m,L,R);
        else if(L>m) return query(rson[root],m+1,r,L,R);
        else return query(lson[root],l,m,L,m)+query(rson[root],m+1,r,m+1,R);
    }
    void insert(int now,int old,int pos,int val){
        T[now]=update(T[old],pos,val);
    }
}pre,suf;
int main(){
    cin>>n>>k;
    v.push_back(-1);
    for(int i=1;i<=n;i++){
        cin>>a[i];
        v.push_back(a[i]);
    }
    sort(v.begin(),v.end());
    v.resize(unique(v.begin(),v.end())-v.begin());
    for(int i=1;i<=n;i++)
        a[i]=lower_bound(v.begin(),v.end(),a[i])-v.begin();
    m=v.size()-1;
    pre.Init(0,m);
    suf.Init(m+1,m);
    for(int i=1;i<=n;i++)
        pre.insert(i,i-1,a[i],1);
    for(int i=n;i>=1;i--)
        suf.insert(i,i+1,a[i],1);
    LL cur=0,ans=0;
    for(int i=1;i<=n;i++)
        cur+=(LL)pre.query(pre.T[i],1,m,a[i]+1,m);
    //cur为初始状态下的逆序对数
    for(int i=1,j=2;i<=n;i++){
        while(j<=n&&cur>k){
            //将j点删除，便是找到[1,i]中比j大的，[j+1]中比j小的
            cur-=(LL)pre.query(pre.T[i],1,m,a[j]+1,m);
            cur-=(LL)suf.query(suf.T[j],1,m,1,a[j]-1);
            j++;
        }
        ans+=n-j+1;
        if(j==i+1){
            //始终保持j>i,删除j
            cur-=(LL)pre.query(pre.T[i],1,m,a[j]+1,m);
            cur-=(LL)suf.query(suf.T[j],1,m,1,a[j]-1);
            j++;
        }
        //恢复i+1
        //便是找到[1,i]中比i+1大的，[j,m]中比i+1小的
        cur+=(LL)pre.query(pre.T[i],1,m,a[i+1]+1,m);
        cur+=(LL)suf.query(suf.T[j],1,m,1,a[i+1]-1);
    }
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <set>
#include <math.h>

using namespace std;

typedef long long i64;
typedef pair<int,int> pii;

const int mod=1000000007;

int tl[100500],tr[100500];
int size;

void init (int nn)
{
	size = nn;
	memset(tl,0,sizeof(tl));
	memset(tr,0,sizeof(tr));
}

int sum (int* t,int r)
{
	int result = 0;
	for (; r >= 0; r = (r & (r+1)) - 1)
		result += t[r];
	return result;
}

void inc (int* t,int i, int delta)
{
	for (; i < size; i = (i | (i+1)))
		t[i] += delta;
}

int sum (int* t,int l, int r)
{
	return sum (t,r) - sum (t,l-1);
}

int a[100500];

int main()
{
	//freopen("inp.txt","r",stdin);
	int n;
	i64 k;
	cin >> n >> k;
	map<int,int> is;
	for (int i=0;i<n;i++)
	{
		scanf("%d",a+i);
		is[a[i]]=0;
	}
	map<int,int>::iterator it=is.begin();
	int cnt;
	for (cnt=1;it!=is.end();cnt++,it++)
		it->second=cnt;
	a[n]=cnt;
	init(cnt+3);
	for (int i=0;i<n;i++)
		a[i]=is[a[i]];
	i64 kol=0;
	i64 res=0;
	for (int i=n-1;i;i--)
	{
		kol+=sum(tr,a[i]-1);
		inc(tr,a[i],1);
	}
	inc(tl,a[0],1);
	kol+=sum(tr,a[0]-1);
	int l=0,r=1;
	for (;r<n;l++)
	{
		if (r<=l) r=l+1;
		while (r+1<n && kol>k)
		{
			inc(tr,a[r],-1);
			kol-=sum(tl,a[r]+1,cnt)+sum(tr,a[r]-1);
			r++;
		}
		if (kol<=k)
			res+=n-r;
		kol+=sum(tl,a[l+1]+1,cnt)+sum(tr,a[l+1]-1);
		inc(tl,a[l+1],1);
	}
	cout << res << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <set>
#include <math.h>

using namespace std;

typedef long long i64;
typedef pair<int,int> pii;

const int mod=1000000007;

int tl[100500],tr[100500];
int size;

void init (int nn)
{
	size = nn;
	memset(tl,0,sizeof(tl));
	memset(tr,0,sizeof(tr));
}

int sum (int* t,int r)
{
	int result = 0;
	for (; r >= 0; r = (r & (r+1)) - 1)
		result += t[r];
	return result;
}

void inc (int* t,int i, int delta)
{
	for (; i < size; i = (i | (i+1)))
		t[i] += delta;
}

int sum (int* t,int l, int r)
{
	return sum (t,r) - sum (t,l-1);
}

int a[100500];

int main()
{
	//freopen("inp.txt","r",stdin);
	int n;
	i64 k;
	cin >> n >> k;
	map<int,int> is;
	for (int i=0;i<n;i++)
	{
		scanf("%d",a+i);
		is[a[i]]=0;
	}
	map<int,int>::iterator it=is.begin();
	int cnt;
	for (cnt=1;it!=is.end();cnt++,it++)
		it->second=cnt;
	a[n]=cnt;
	init(cnt+3);
	for (int i=0;i<n;i++)
		a[i]=is[a[i]];
	i64 kol=0;
	i64 res=0;
	for (int i=n-1;i;i--)
	{
		kol+=sum(tr,a[i]-1);
		inc(tr,a[i],1);
	}
	inc(tl,a[0],1);
	kol+=sum(tr,a[0]-1);
	int l=0,r=1;
	for (;r<n;l++)
	{
		while (r<=l)
		{
			inc(tr,a[r],-1);
			kol-=sum(tl,a[r]+1,cnt)+sum(tr,a[r]-1);
			r++;
		}
		while (r+1<n && kol>k)
		{
			inc(tr,a[r],-1);
			kol-=sum(tl,a[r]+1,cnt)+sum(tr,a[r]-1);
			r++;
		}
		if (kol<=k)
			res+=n-r;
		kol+=sum(tl,a[l+1]+1,cnt)+sum(tr,a[l+1]-1);
		inc(tl,a[l+1],1);
	}
	cout << res << endl;
	return 0;
}


#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <string.h>
#include <set>
#include <math.h>

using namespace std;

typedef long long i64;
typedef pair<int,int> pii;

const int mod=1000000007;

int t[100500];
int size;

void init (int nn)
{
	size = nn;
	memset(t,0,sizeof(t));
}

int sum (int r)
{
	int result = 0;
	for (; r >= 0; r = (r & (r+1)) - 1)
		result += t[r];
	return result;
}

void inc (int i, int delta)
{
	for (; i < size; i = (i | (i+1)))
		t[i] += delta;
}

int sum (int l, int r)
{
	return sum (r) - sum (l-1);
}

int a[100500];

int main()
{
	//freopen("inp.txt","r",stdin);
	int n;
	i64 k;
	cin >> n >> k;
	map<int,int> is;
	for (int i=0;i<n;i++)
	{
		scanf("%d",a+i);
		is[a[i]]=0;
	}
	map<int,int>::iterator it=is.begin();
	int cnt;
	for (cnt=1;it!=is.end();cnt++,it++)
		it->second=cnt;
	a[n]=cnt;
	init(cnt+3);
	for (int i=0;i<n;i++)
		a[i]=is[a[i]];
	i64 kol=0;
	i64 res=0;
	int l=0,r=0;
	inc(a[0],1);
	for (;l<n;l++)
	{
		while (r<n && kol<=k)
		{
			r++;
			kol+=sum(a[r]+1,cnt);
			inc(a[r],1);
		}
		res+=r-l-1;
		kol-=sum(a[l]-1);
		inc(a[l],-1);
	}
	cout << res << endl;
	return 0;
}


#pragma comment(linker, "/STACK:256000000")
#define _CRT_SECURE_NO_WARNINGS

#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <bitset>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int INF = 1000 * 1000 * 1000;
const double EPS = 1e-9;

#define ALL(v) v.begin(), v.end()
#define SZ(v) (int) v.size()
#define SQR(x) ((x) * (x))
#define MP make_pair

const int MAXN = 100100;

struct Node {
    int key, priority;

    int q;

    Node *l, *r;

    Node(int key) : key(key), priority((rand() << 15) ^ rand()), q(1), l(0), r(0) {}
};

typedef Node* pt;

struct Treap {
    pt root;

    Treap() {
        root = 0;
    }

    int calc(pt t) {
        if (t == 0) {
            return 0;
        }
        return t->q;
    }

    void recalc(pt &t) {
        if (t != 0) {
            t->q = 1 + calc(t->l) + calc(t->r);
        }
    }

    pt merge(pt tl, pt tr) {
        if (tl == 0) {
            return tr;
        }
        if (tr == 0) {
            return tl;
        }
        if (tl->priority > tr->priority) {
            tl->r = merge(tl->r, tr);
            recalc(tl);
            return tl;
        }
        tr->l = merge(tl, tr->l);
        recalc(tr);
        return tr;
    }

    void split(pt t, pt &tl, pt &tr, int key) {
        if (t == 0) {
            tl = tr = 0;
            return;
        }
        if (key >= t->key) {
            split(t->r, t->r, tr, key);
            tl = t;
        } else {
            split(t->l, tl, t->l, key);
            tr = t;
        }
        recalc(tl);
        recalc(tr);
    }

    void insert(int key) {
        pt lt, rt;
        split(root, lt, rt, key);
        root = merge(lt, merge(new Node(key), rt));
    }

    void remove(int key) {
        pt lt, mt, rt;
        split(root, lt, rt, key);
        split(lt, lt, mt, key - 1);
        if (calc(mt) == 1) {
            root = merge(lt, rt);
        } else {
            mt = merge(mt->l, mt->r);
            root = merge(lt, merge(mt, rt));
        }
    }

    void print(pt t) {
        if (t == 0) {
            return;
        }
        print(t->l);
        printf("%d ", t->key);
        print(t->r);
    }
};

int a[MAXN];

int N;
ll K;

ll calcInversions() {
    ll res = 0;
    Treap t;
    pt l, r;
    for (int i = 0; i < N; ++i) {
        t.split(t.root, l, r, a[i]);
        res += t.calc(r);
        t.root = t.merge(l, r);
        t.insert(a[i]);
    }
    return res;
}

int getInversions(Treap lt, Treap rt, int key) {
    int res = 0;
    pt l, r;

    lt.split(lt.root, l, r, key);
    res += lt.calc(r);
    lt.root = lt.merge(l, r);

    rt.split(rt.root, l, r, key - 1);
    res += rt.calc(l);
    rt.root = rt.merge(l, r);

    return res;
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif

    scanf("%d%I64d", &N, &K);

    for (int i = 0; i < N; ++i) {
        scanf("%d", &a[i]);
    }

    ll inv = calcInversions();

    Treap lt, rt;

    ll ans = 0;

    int lf = 0, rg = 1;

    lt.insert(a[0]);

    for (int i = 1; i < N; ++i) {
        rt.insert(a[i]);
    }

    while (lf < N) {
        if (lf == rg) {
            inv -= getInversions(lt, rt, a[rg]);
            rt.remove(a[rg++]);
        }
        while (rg < N && inv > K) {
            inv -= getInversions(lt, rt, a[rg]);
            rt.remove(a[rg++]);
        }
        if (inv <= K) {
            ans += N - rg;
        }
//      cout << lf << " " << rg << " " << inv << " " << ans << endl;
        inv += getInversions(lt, rt, a[++lf]);
        lt.insert(a[lf]);
    }

    printf("%I64d", ans);

    return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,a[100010];
int b[100010],lb;
long long int k;

int big[100010],small[100010];
int lowbit(int x){return x&-x;}
void addbig(int x,int y){
	while(x){
		big[x]+=y;
		x-=lowbit(x);
	}
}
int querybig(int x){
	int res=0;
	while(x<=lb){
		res+=big[x];
		x+=lowbit(x);
	}
	return res;
}
void addsmall(int x,int y){
	while(x<=lb){
		small[x]+=y;
		x+=lowbit(x);
	}
}
int querysmall(int x){
	int res=0;
	while(x){
		res+=small[x];
		x-=lowbit(x);
	}
	return res;
}

int main(){
	scanf("%d%I64d",&n,&k);
	for(int i=0; i<n; i++){
		scanf("%d",&a[i]);
		b[i]=a[i];
	}
	sort(b,b+n);
	lb=unique(b,b+n)-b;
	for(int i=0; i<n; i++)
		a[i]=lower_bound(b,b+lb,a[i])-b+1;

	long long int w=0;
	memset(big,0,sizeof(big));
	for(int i=0; i<n; i++){
		w+=querybig(a[i]+1);
		addbig(a[i],1);
	}
	if(w<=k){
		printf("%I64d\n",n*(n-1LL)/2);
		return 0;
	}
	long long int ans=0;
	memset(big,0,sizeof(big));
	memset(small,0,sizeof(small));
	for(int i=1; i<n; i++)
		addsmall(a[i],1);
	int l=0,r=1;
	for(; l<n; l++){
		if(l>0){
			w+=querybig(a[l]+1);
			w+=querysmall(a[l]-1);
		}
		addbig(a[l],1);
		while(w>k && r<n){
			w-=querybig(a[r]+1);
			w-=querysmall(a[r]-1);
			addsmall(a[r],-1);
			r++;
		}
		if(r>=n)break;
		ans+=n-r;
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,a[100010];
int b[100010],lb;
long long int k;

int big[100010],small[100010];
int lowbit(int x){return x&-x;}
void addbig(int x,int y){
	while(x){
		big[x]+=y;
		x-=lowbit(x);
	}
}
int querybig(int x){
	int res=0;
	while(x<=n){
		res+=big[x];
		x+=lowbit(x);
	}
	return res;
}
void addsmall(int x,int y){
	while(x<=n){
		small[x]+=y;
		x+=lowbit(x);
	}
}
int querysmall(int x){
	int res=0;
	while(x){
		res+=small[x];
		x-=lowbit(x);
	}
	return res;
}

int main(){
	scanf("%d%I64d",&n,&k);
	for(int i=0; i<n; i++){
		scanf("%d",&a[i]);
		b[i]=a[i];
	}
	sort(b,b+n);
	lb=unique(b,b+n)-b;
	for(int i=0; i<n; i++)
		a[i]=lower_bound(b,b+lb,a[i])-b+1;

	long long int w=0;
	memset(big,0,sizeof(big));
	for(int i=0; i<n; i++){
		w+=querybig(a[i]+1);
		addbig(a[i],1);
	}
	if(w<=k){
		printf("%I64d\n",n*(n-1LL)/2);
		return 0;
	}
	long long int ans=0;
	memset(big,0,sizeof(big));
	memset(small,0,sizeof(small));
	for(int i=1; i<n; i++)
		addsmall(a[i],1);
	int l=0,r=1;
	for(; l<n; l++){
		w+=querybig(a[l]+1);
		w+=querysmall(a[l]-1);
		addbig(a[l],1);
		while(w>k && r<n){
			w-=querybig(a[r]+1);
			w-=querysmall(a[r]-1);
			addsmall(a[r],-1);
			r++;
		}
		if(r>=n)break;
		ans+=n-r;
	}
	printf("%I64d\n",ans);
	return 0;
}


#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <math.h>
#define N 100100
using namespace std;
map <int, int> m;
map <int, int>::iterator it;
int a[N], d1[4*N], d2[4*N], p;
long long s[N];
void add1(int i, int v)
{
    for(i+=p; i; d1[i]+=v, i>>=1);
}
int get1(int i, int j)
{
    int r;
    for(r=0, i+=p, j+=p; i<=j; i>>=1, j>>=1)
    {
        if(i&1) r+=d1[i++];
        if(~j&1) r+=d1[j--];
    }
    return r;
}
void add2(int i, int v)
{
    for(i+=p; i; d2[i]+=v, i>>=1);
}
int get2(int i, int j)
{
    int r;
    for(r=0, i+=p, j+=p; i<=j; i>>=1, j>>=1)
    {
        if(i&1) r+=d2[i++];
        if(~j&1) r+=d2[j--];
    }
    return r;
}
int main()
{
    int i, j, n;
    long long r, k, x;
    for(scanf("%d%I64d", &n, &x), i=0; i<n; scanf("%d", &a[i]), m[a[i]]=1, i++);
    for(i=0, it=m.begin(); it!=m.end(); it->second=i++, it++);
    for(i=0; i<n; a[i]=m[a[i]], i++);
    for(p=1; p<n; p*=2);
    for(k=0, i=n-1; i>=0; k+=get2(0, a[i]-1), add2(a[i], 1), i--);
    for(r=0, add1(a[0], 1), add2(a[0], -1), j=1, i=0; i<n-1; r+=n-j, i++, k+=get1(a[i], n-1)+get2(0, a[i]-1), add1(a[i], 1))
        for(; j<n && (j<=i || k>x); k-=get1(a[j]+1, n-1)+get2(0, a[j]-1), add2(a[j], -1), j++);
    printf("%I64d\n", r);
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#include <math.h>
#define N 100100
using namespace std;
map <int, int> m;
map <int, int>::iterator it;
int a[N], d1[4*N], d2[4*N], p;
long long s[N];
void add1(int i, int v)
{
    for(i+=p; i; d1[i]+=v, i>>=1);
}
int get1(int i, int j)
{
    int r;
    for(r=0, i+=p, j+=p; i<=j; i>>=1, j>>=1)
    {
        if(i&1) r+=d1[i++];
        if(~j&1) r+=d1[j--];
    }
    return r;
}
void add2(int i, int v)
{
    for(i+=p; i; d2[i]+=v, i>>=1);
}
int get2(int i, int j)
{
    int r;
    for(r=0, i+=p, j+=p; i<=j; i>>=1, j>>=1)
    {
        if(i&1) r+=d2[i++];
        if(~j&1) r+=d2[j--];
    }
    return r;
}
int main()
{
    int i, j, n;
    long long r, k, x;
    for(scanf("%d%I64d", &n, &x), i=0; i<n; scanf("%d", &a[i]), m[a[i]]=1, i++);
    for(i=0, it=m.begin(); it!=m.end(); it->second=i++, it++);
    for(i=0; i<n; a[i]=m[a[i]], i++);
    for(p=1; p<n; p*=2);
    for(k=0, i=n-1; i>=0; k+=get2(0, a[i]-1), add2(a[i], 1), i--);
    for(r=0, add1(a[0], 1), add2(a[0], -1), j=1, i=0; i<n-1; r+=n-j, i++, k+=get1(a[i]+1, n-1)+get2(0, a[i]-1), add1(a[i], 1))
        for(; j<n && (j<=i || k>x); k-=get1(a[j]+1, n-1)+get2(0, a[j]-1), add2(a[j], -1), j++);
    printf("%I64d\n", r);
    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <ctime>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <math.h>
#include <queue>
#include <memory.h>
#include <iostream>
#include <stack>
#include <complex>
#include <list>

using namespace std;

void ASS(bool bb)
{
    if (!bb)
    {
        ++*(int*)0;
    }
}

#define FOR(i, x) for (int i = 0; i < (int)(x); i++)
#define CL(x) memset(x, 0, sizeof(x))
#define CLX(x, y) memset(x, y, sizeof(x))

#pragma comment(linker, "/STACK:106777216")

typedef vector<int> vi;

typedef long long LL;

const int N = 1 << 17;

struct RSQ {
    int a[N * 2];
    void Zero() {
        CL(a);
    }
    void Add(int pos, int dx) {
        for (int i = pos + N; i; i >>= 1)
            a[i] += dx;
    }
    int Sum(int L, int R) const {
        int res = 0;
        for (L += N, R += N; L < R; L >>= 1, R >>= 1) {
            if (L & 1)
                res += a[L++];
            if (R & 1)
                res += a[--R];
        }
        return res;
    }
};

struct RSQInv {
    int a[N * 2];
    void Zero() {
        CL(a);
    }
    int Val(int pos) const {
        int res = 0;
        for (int i = pos + N; i; i >>= 1)
            res += a[i];
        return res;
    }
    void Add(int L, int R, int dx) {
        for (L += N, R += N; L < R; L >>= 1, R >>= 1) {
            if (L & 1)
                a[L++] += dx;
            if (R & 1)
                a[--R] += dx;
        }
    }
};

RSQ ra;
RSQ rb;
//RSQInv rbInv;

LL Cnt(const vi& a) {
    static RSQ r;
    r.Zero();
    LL res = 0;
    FOR(i, a.size()) {
        res += r.Sum(a[i] + 1, N);
        r.Add(a[i], 1);
    }
    return res;
}

LL Solve(const vi& a, const LL k) {
    ra.Zero();
    rb.Zero();
    int n = (int)a.size();
    int R = n - 1;
    ra.Add(a[0], 1);
    rb.Add(a[n - 1], 1);
    LL c = a[0] > a[n - 1];
    if (c > k) {
        cout << 0 << endl;
        return 0;
    }

    while (R > 1) {
        int nR = R - 1;
        LL nc = c + (a[0] > a[nR]);
        nc += rb.Sum(0, a[nR]);
        if (nc > k)
            break;
        R = nR;
        c = nc;
        rb.Add(a[nR], 1);
    }
    if (R == 1)
        return n * (n - 1) / 2;

    LL res = n - R;
    for (int L = 1; L < n - 1; L++) {
        c += ra.Sum(a[L] + 1, N);
        ra.Add(a[L], 1);
        c += rb.Sum(0, a[L]);

        while ((c > k || R <= L) && ++R <= n - 1) {
            c -= ra.Sum(a[R] + 1, N);
            c -= rb.Sum(0, a[R]);
            rb.Add(a[R], -1);
        }
        if (c > k)
            break;
        res += n - R;
    }
    return res;
}

int main()
{
    int n;
    LL k;
    cin >> n;
    cin >> k;
    vi a(n);
    FOR(i, n)
        cin >> a[i];
    {
        vi b = a;
        sort(b.begin(), b.end());
        b.resize(unique(b.begin(), b.end()) - b.begin());
        FOR(i, n)
            a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
    }

    LL res = Solve(a, k);
    cout << res << endl;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <ctime>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <math.h>
#include <queue>
#include <memory.h>
#include <iostream>
#include <stack>
#include <complex>
#include <list>

using namespace std;

void ASS(bool bb)
{
    if (!bb)
    {
        ++*(int*)0;
    }
}

#define FOR(i, x) for (int i = 0; i < (int)(x); i++)
#define CL(x) memset(x, 0, sizeof(x))
#define CLX(x, y) memset(x, y, sizeof(x))

#pragma comment(linker, "/STACK:106777216")

typedef vector<int> vi;

typedef long long LL;

const int N = 1 << 17;

struct RSQ {
    int a[N * 2];
    void Zero() {
        CL(a);
    }
    void Add(int pos, int dx) {
        for (int i = pos + N; i; i >>= 1)
            a[i] += dx;
    }
    int Sum(int L, int R) const {
        int res = 0;
        for (L += N, R += N; L < R; L >>= 1, R >>= 1) {
            if (L & 1)
                res += a[L++];
            if (R & 1)
                res += a[--R];
        }
        return res;
    }
};

RSQ ra;
RSQ rb;

LL Solve(const vi& a, const LL k) {
    ra.Zero();
    rb.Zero();
    int n = (int)a.size();
    int R = n - 1;
    ra.Add(a[0], 1);
    rb.Add(a[n - 1], 1);
    LL c = a[0] > a[n - 1];
    if (c > k) {
        cout << 0 << endl;
        return 0;
    }

    while (R > 1) {
        int nR = R - 1;
        LL nc = c + (a[0] > a[nR]);
        nc += rb.Sum(0, a[nR]);
        if (nc > k)
            break;
        R = nR;
        c = nc;
        rb.Add(a[nR], 1);
    }
    if (R == 1)
        return n * (LL)(n - 1) / 2;

    LL res = n - R;
    for (int L = 1; L < n - 1; L++) {
        c += ra.Sum(a[L] + 1, N);
        ra.Add(a[L], 1);
        c += rb.Sum(0, a[L]);

        while ((c > k || R <= L) && ++R <= n - 1) {
            c -= ra.Sum(a[R - 1] + 1, N);
            c -= rb.Sum(0, a[R - 1]);
            rb.Add(a[R - 1], -1);
        }
        if (c > k)
            break;
        res += n - R;
    }
    return res;
}

int main()
{
    int n;
    LL k;
    cin >> n;
    cin >> k;
    vi a(n);
    FOR(i, n)
        cin >> a[i];
    {
        vi b = a;
        sort(b.begin(), b.end());
        b.resize(unique(b.begin(), b.end()) - b.begin());
        FOR(i, n)
            a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
    }

    LL res = Solve(a, k);
    cout << res << endl;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <ctime>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <math.h>
#include <queue>
#include <memory.h>
#include <iostream>
#include <stack>
#include <complex>
#include <list>

using namespace std;

void ASS(bool bb)
{
    if (!bb)
    {
        ++*(int*)0;
    }
}

#define FOR(i, x) for (int i = 0; i < (int)(x); i++)
#define CL(x) memset(x, 0, sizeof(x))
#define CLX(x, y) memset(x, y, sizeof(x))

#pragma comment(linker, "/STACK:106777216")

typedef vector<int> vi;

typedef long long LL;

const int N = 1 << 17;

struct RSQ {
    int a[N * 2];
    void Zero() {
        CL(a);
    }
    void Add(int pos, int dx) {
        for (int i = pos + N; i; i >>= 1)
            a[i] += dx;
    }
    int Sum(int L, int R) const {
        int res = 0;
        for (L += N, R += N; L < R; L >>= 1, R >>= 1) {
            if (L & 1)
                res += a[L++];
            if (R & 1)
                res += a[--R];
        }
        return res;
    }
};

RSQ ra;
RSQ rb;

LL Solve(const vi& a, const LL k) {
    ra.Zero();
    rb.Zero();
    int n = (int)a.size();
    int R = n - 1;
    ra.Add(a[0], 1);
    rb.Add(a[n - 1], 1);
    LL c = a[0] > a[n - 1];
    if (c > k) {
        return 0;
    }

    while (R > 1) {
        int nR = R - 1;
        LL nc = c + (a[0] > a[nR]);
        nc += rb.Sum(0, a[nR]);
        if (nc > k)
            break;
        R = nR;
        c = nc;
        rb.Add(a[nR], 1);
    }
    if (R == 1)
        return n * (LL)(n - 1) / 2;

    LL res = n - R;
    for (int L = 1; L < n - 1; L++) {
        c += ra.Sum(a[L] + 1, N);
        ra.Add(a[L], 1);
        c += rb.Sum(0, a[L]);

        while ((c > k || R <= L) && ++R <= n - 1) {
            c -= ra.Sum(a[R - 1] + 1, N);
            c -= rb.Sum(0, a[R - 1]);
            rb.Add(a[R - 1], -1);
        }
        if (c > k)
            break;
        res += n - R;
    }
    return res;
}

int main()
{
    int n;
    LL k;
    cin >> n;
    cin >> k;
    vi a(n);
    FOR(i, n)
        cin >> a[i];
    {
        vi b = a;
        sort(b.begin(), b.end());
        b.resize(unique(b.begin(), b.end()) - b.begin());
        FOR(i, n)
            a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
    }

    LL res = Solve(a, k);
    cout << res << endl;

    return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <ctime>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <math.h>
#include <queue>
#include <memory.h>
#include <iostream>
#include <stack>
#include <complex>
#include <list>

using namespace std;

void ASS(bool bb)
{
    if (!bb)
    {
        ++*(int*)0;
    }
}

#define FOR(i, x) for (int i = 0; i < (int)(x); i++)
#define CL(x) memset(x, 0, sizeof(x))
#define CLX(x, y) memset(x, y, sizeof(x))

#pragma comment(linker, "/STACK:106777216")

typedef vector<int> vi;

typedef long long LL;

const int N = 1 << 17;

struct RSQ {
    int a[N * 2];
    void Zero() {
        CL(a);
    }
    void Add(int pos, int dx) {
        for (int i = pos + N; i; i >>= 1)
            a[i] += dx;
    }
    int Sum(int L, int R) const {
        int res = 0;
        for (L += N, R += N; L < R; L >>= 1, R >>= 1) {
            if (L & 1)
                res += a[L++];
            if (R & 1)
                res += a[--R];
        }
        return res;
    }
};

struct RSQInv {
    int a[N * 2];
    void Zero() {
        CL(a);
    }
    int Val(int pos) const {
        int res = 0;
        for (int i = pos + N; i; i >>= 1)
            res += a[i];
        return res;
    }
    void Add(int L, int R, int dx) {
        for (L += N, R += N; L < R; L >>= 1, R >>= 1) {
            if (L & 1)
                a[L++] += dx;
            if (R & 1)
                a[--R] += dx;
        }
    }
};

RSQ ra;
RSQ rb;
//RSQInv rbInv;

LL Cnt(const vi& a) {
    static RSQ r;
    r.Zero();
    LL res = 0;
    FOR(i, a.size()) {
        res += r.Sum(a[i] + 1, N);
        r.Add(a[i], 1);
    }
    return res;
}

LL Solve(const vi& a, const LL k) {
    ra.Zero();
    rb.Zero();
    int n = (int)a.size();
    int R = n - 1;
    ra.Add(a[0], 1);
    rb.Add(a[n - 1], 1);
    LL c = a[0] > a[n - 1];
    if (c > k) {
        cout << 0 << endl;
        return 0;
    }

    while (R > 1) {
        int nR = R - 1;
        LL nc = c + (a[0] > a[nR]);
        nc += rb.Sum(0, a[nR]);
        if (nc > k)
            break;
        R = nR;
        c = nc;
        rb.Add(a[nR], 1);
    }
    if (R == 1)
        return n * (n - 1) / 2;

    LL res = n - R;
    for (int L = 1; L < n - 1; L++) {
        c += ra.Sum(a[L] + 1, N);
        ra.Add(a[L], 1);
        c += rb.Sum(0, a[L]);

        while ((c > k || R <= L) && ++R <= n - 1) {
            c -= ra.Sum(a[R - 1] + 1, N);
            c -= rb.Sum(0, a[R - 1]);
            rb.Add(a[R - 1], -1);
        }
        if (c > k)
            break;
        res += n - R;
    }
    return res;
}

int main()
{
    int n;
    LL k;
    cin >> n;
    cin >> k;
    vi a(n);
    FOR(i, n)
        cin >> a[i];
    {
        vi b = a;
        sort(b.begin(), b.end());
        b.resize(unique(b.begin(), b.end()) - b.begin());
        FOR(i, n)
            a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
    }

    LL res = Solve(a, k);
    cout << res << endl;

    return 0;
}


#include <iostream>
#include <iomanip>
#include <set>
#include <cstdio>
#include <cstring>
#include <stack>
#include <string>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <bitset>
using namespace std;
#define mp make_pair
#define ull unsigned long long
#define ll long long

static ll m1[200000], m2[200000], fen1[200000], fen2[200000];

void chng1(ll dest, ll val, ll size){
	for (ll i = dest; i <= size; i = (i | (i - 1)) + 1)
		fen1[i] += val;
}

ll getf1(ll dest){
	ll res = 0;
	for (ll i = dest; i > 0; i = (i & (i - 1)))
		res += fen1[i];
	return res;
}

void chng2(ll dest, ll val, ll size){
	for (ll i = dest; i <= size; i = (i | (i - 1)) + 1)
		fen2[i] += val;
}

ll getf2(ll dest){
	ll res = 0;
	for (ll i = dest; i > 0; i = (i & (i - 1)))
		res += fen2[i];
	return res;
}

int main(){
	ios_base::sync_with_stdio(0);
	ll n, k;
	cin >> n >> k;
	for (ll i = 0; i < n; ++i){
		cin >> m1[i];
		m2[i] = m1[i];
	}
	sort(m2, m2 + n);
	for (ll i = 0; i < n; ++i)
		m1[i] = lower_bound(m2, m2 + n, m1[i]) - m2 + 1;
	ll a = 1, res = 0, cur = 0;
	for (ll i = 1; i < n; ++i){
		chng2(m1[i], 1, n + 1);
		cur += getf2(n + 1) - getf2(m1[i]);
	}
	for (ll i = 0; i < n; ++i){
		chng1(m1[i], 1, n + 1);
		cur += getf1(n + 1) - getf1(m1[i]);
		cur += getf2(m1[i] - 1);
		while (a < n && cur > k){
			cur -= getf1(n + 1) - getf1(m1[a]);
			chng2(m1[i], -1, n + 1);
			cur -= getf2(m1[a] - 1);
			++a;
		}
		res += n - a;
	}
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <set>
#include <cstdio>
#include <cstring>
#include <stack>
#include <string>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <bitset>
using namespace std;
#define mp make_pair
#define ull unsigned long long
#define ll long long

static ll m1[200000], m2[200000], fen1[200000], fen2[200000];

void chng3(ll dest, ll val, ll size){
	for (ll i = dest; i <= size; i = (i | (i - 1)) + 1)
		fen1[i] += val;
}

ll getf3(ll dest){
	ll res = 0;
	for (ll i = dest; i > 0; i = (i & (i - 1)))
		res += fen1[i];
	return res;
}

void chng4(ll dest, ll val, ll size){
	for (ll i = dest; i <= size; i = (i | (i - 1)) + 1)
		fen2[i] += val;
}

ll getf4(ll dest){
	ll res = 0;
	for (ll i = dest; i > 0; i = (i & (i - 1)))
		res += fen2[i];
	return res;
}

int main(){
	ios_base::sync_with_stdio(0);
	ll n, k;
	cin >> n >> k;
	for (ll i = 0; i < n; ++i){
		cin >> m1[i];
		m2[i] = m1[i];
	}
	sort(m2, m2 + n);
	for (ll i = 0; i < n; ++i)
		m1[i] = lower_bound(m2, m2 + n, m1[i]) - m2 + 1;
	ll a = 1, res = 0, cur = 0;
	for (ll i = 1; i < n; ++i){
		chng4(m1[i], 1, n + 1);
		cur += getf4(n + 1) - getf4(m1[i]);
	}
	for (ll i = 0; i < n; ++i){
		chng3(m1[i], 1, n + 1);
		cur += getf3(n + 1) - getf3(m1[i]);
		cur += getf4(m1[i] - 1);
		while (a < n && cur > k){
			cur -= getf3(n + 1) - getf3(m1[a]);
			chng4(m1[a], -1, n + 1);
			cur -= getf4(m1[a] - 1);
			++a;
		}
		res += n - a;
	}
	cout << res << endl;
	return 0;
}


#include <iostream>
#include <iomanip>
#include <set>
#include <cstdio>
#include <cstring>
#include <stack>
#include <string>
#include <map>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <bitset>
using namespace std;
#define mp make_pair
#define ull unsigned long long
#define ll long long

static ll m1[200000], m2[200000], fen1[200000], fen2[200000];

void chng3(ll dest, ll val, ll size){
	for (ll i = dest; i <= size; i = (i | (i - 1)) + 1)
		fen1[i] += val;
}

ll getf3(ll dest){
	ll res = 0;
	for (ll i = dest; i > 0; i = (i & (i - 1)))
		res += fen1[i];
	return res;
}

void chng4(ll dest, ll val, ll size){
	for (ll i = dest; i <= size; i = (i | (i - 1)) + 1)
		fen2[i] += val;
}

ll getf4(ll dest){
	ll res = 0;
	for (ll i = dest; i > 0; i = (i & (i - 1)))
		res += fen2[i];
	return res;
}

int main(){
	ios_base::sync_with_stdio(0);
	ll n, k;
	cin >> n >> k;
	for (ll i = 0; i < n; ++i){
		cin >> m1[i];
		m2[i] = m1[i];
	}
	sort(m2, m2 + n);
	for (ll i = 0; i < n; ++i)
		m1[i] = lower_bound(m2, m2 + n, m1[i]) - m2 + 1;
	ll a = 1, res = 0, cur = 0;
	for (ll i = 1; i < n; ++i){
		chng4(m1[i], 1, n + 1);
		cur += getf4(n + 1) - getf4(m1[i]);
	}
	for (ll i = 0; i < n; ++i){
		chng3(m1[i], 1, n + 1);
		cur += getf3(n + 1) - getf3(m1[i]);
		cur += getf4(m1[i] - 1);
		while (a < n && (cur > k || a <= i)){
			cur -= getf3(n + 1) - getf3(m1[a]);
			chng4(m1[a], -1, n + 1);
			cur -= getf4(m1[a] - 1);
			++a;
		}
		res += n - a;
	}
	cout << res << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <utility>
#include <functional>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <ctime>
#include <cmath>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double, double>
#define fi first
#define se second
#define forn(i,a,b) for( int i = (a); i < (b); i++ )
#define rep(i,n) forn(i,0,n)
#define forever for(;;)

const int MXN = 100100;
const int MXK = 10;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const int mod = 1000000007;

typedef long long int64;

int n;
int a[MXN];

template<class T>
struct Index: public vector<T> {
    using vector<T>::erase;
    using vector<T>::begin;
    using vector<T>::end;
    void doit() {
        sort(begin(), end());
        erase(unique(begin(), end()), end());
    }
    int get(T x) {
        return lower_bound(begin(), end(), x) - begin();
    }
};

Index<int> idx;

struct TA {
    int a[MXN], n;
    int all;
    void init(int _n) {
        n = _n;
        memset(a, 0, sizeof a);
        all = 0;
    }
    void add(int p, int x) {
        for (p++; p <= n; p += p & -p)
            a[p - 1] += x;
        all += x;
    }
    int sum(int p) {
        int r = 0;
        for (p++; p; p -= p & -p)
            r += a[p - 1];
        return r;
    }
    int lower(int x) { //<x
        return sum(x - 1);
    }
    int upper(int x) { //>x
        return all - sum(x);
    }
};

int64 k;

TA tL, tR;
int64 cntInv;
void addR(int i) { //make i -> tR
    int x = a[i];
    cntInv += tR.lower(x);
    cntInv += tL.upper(x);
    tR.add(x, 1);
}
void addL(int i) {
    int x = a[i];
    cntInv += tL.upper(x);
    cntInv += tR.lower(x);
    tL.add(x, 1);
}
void subR(int i) {
    int x = a[i];
    cntInv -= tR.lower(x);
    cntInv -= tL.upper(x);
    tR.add(x, -1);
}

int main() {
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        idx.push_back(a[i]);
    }
    idx.doit();
    for (int i = 0; i < n; ++i) {
        a[i] = idx.get(a[i]);
    }
    tL.init(idx.size());
    tR.init(idx.size());
    cntInv = 0;
    int R = n;
    while (R > 0) {
        addR(R - 1);
        if (cntInv > k) {
            subR(R - 1);
            break;
        }
        --R;
    }
    int64 ans = 0;
    for (int L = 0; L < n; ++L) {
        //make R>L
        while (R <= L) {
            subR(R);
            ++R;
        }
        addL(L);
        while (R < n && cntInv > k) {
            subR(R);
            R++;
        }
        ans += n - R;
    }
    cout << ans << endl;
    return 0;
}


#include<cstdio>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long ll;

const int N_MAX=100000;

template<class T>
class Fenwick_tree{
	int n;
	T a[N_MAX];

public:
	void build(int n){
		this->n=n;
		rep(i,n) a[i]=0;
	}

	void add(int i,T v){
		for(;i<n;i|=i+1) a[i]+=v;
	}

	T sum(int i,int j){
		if(i==0){
			T s=0;
			for(;j>=0;j=(j&(j+1))-1) s+=a[j];
			return s;
		}
		return sum(0,j)-sum(0,i-1);
	}
};

void compress(int n,int *a){
	static int X[100000];
	rep(i,n) X[i]=a[i];
	sort(X,X+n);
	int m=unique(X,X+n)-X;
	rep(i,n) a[i]=lower_bound(X,X+m,a[i])-X;
}

ll count_inversion(int n,const int *a){
	static Fenwick_tree<int> F;
	F.build(n);
	ll res=0;
	rep(i,n){
		res+=F.sum(a[i]+1,n-1);
		F.add(a[i],1);
	}
	return res;
}

int main(){
	int n;
	ll K; scanf("%d%lld",&n,&K);
	static int a[100000];
	rep(i,n) scanf("%d",a+i);

	compress(n,a);

	ll total=count_inversion(n,a);

	static Fenwick_tree<int> L,R; // L : a[0..i], R : a[j..n-1]
	L.build(n);
	R.build(n);
	L.add(a[0],1);
	for(int i=1;i<n;i++) R.add(a[i],1);

	ll ans=0,cnt=0;
	for(int i=0,j=1;;){
		while(j<=i || (j<n && total-cnt>K)){
			R.add(a[j],-1);
			cnt+=L.sum(a[j]+1,n-1);
			cnt+=R.sum( 0 ,a[j]-1);
			j++;
		}
		ans+=n-j;

		i++;
		if(i==n) break;
		cnt-=L.sum(a[i]+1,n-1);
		cnt-=R.sum( 0 ,a[i]-1);
		L.add(a[i],1);
	}

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

	return 0;
}


#include<cstdio>
#include<algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef long long ll;

const int N_MAX=100000;

template<class T>
class Fenwick_tree{
	int n;
	T a[N_MAX];

public:
	void build(int n){
		this->n=n;
		rep(i,n) a[i]=0;
	}

	void add(int i,T v){
		for(;i<n;i|=i+1) a[i]+=v;
	}

	T sum(int i,int j){
		if(i==0){
			T s=0;
			for(;j>=0;j=(j&(j+1))-1) s+=a[j];
			return s;
		}
		return sum(0,j)-sum(0,i-1);
	}
};

void compress(int n,int *a){
	static int X[100000];
	rep(i,n) X[i]=a[i];
	sort(X,X+n);
	int m=unique(X,X+n)-X;
	rep(i,n) a[i]=lower_bound(X,X+m,a[i])-X;
}

ll count_inversion(int n,const int *a){
	static Fenwick_tree<int> F;
	F.build(n);
	ll res=0;
	rep(i,n){
		res+=F.sum(a[i]+1,n-1);
		F.add(a[i],1);
	}
	return res;
}

int main(){
	int n;
	ll K; scanf("%d%lld",&n,&K);
	static int a[100000];
	rep(i,n) scanf("%d",a+i);

	compress(n,a);

	ll total=count_inversion(n,a);

	static Fenwick_tree<int> Li,Ri,Lj,Rj;
	Li.build(n); Ri.build(n);
	Lj.build(n); Rj.build(n);
	Li.add(a[0],1);
	Lj.add(a[0],1);
	for(int i=1;i<n;i++){
		Ri.add(a[i],1);
		Rj.add(a[i],1);
	}

	ll ans=0,cnt=0;
	for(int i=0,j=1;;){
		while(j<=i || (j<n && total-cnt>K)){
			Lj.add(a[j], 1);
			Rj.add(a[j],-1);
			cnt+=Li.sum(a[j]+1,n-1);
			cnt+=Rj.sum( 0 ,a[j]-1);
			j++;
		}
		ans+=n-j;

		i++;
		if(i==n) break;
		cnt-=Li.sum(a[i]+1,n-1);
		cnt-=Rj.sum( 0 ,a[i]-1);
		Li.add(a[i], 1);
		Ri.add(a[i],-1);
	}

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

	return 0;
}


//
//  main.cpp
//  E. Little Elephant and Inversions
//
//  Created by Shashank Saurabh on 3/23/18.
//  Copyright (c) 2018 Shashank Saurabh. All rights reserved.
//

#include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

#define HASH_COUNT 500000

void update(int *st,int n,int pos,int val){
    pos+=n;
    st[pos]+=val;
    for(;pos>1;pos>>=1){
        st[pos>>1] = st[pos] + st[pos^1];
    }
}

int querry (int *st,int n,int l,int r){
    int res = 0;
    if(l<=r && r>=0)res+=st[r+n];
    for (l+=n,r+=n; l<r; l>>=1,r>>=1) {
        if(l&1)res+=st[l++];
        if(r&1)res+=st[--r];
    }
    return res;
}

int hashFunction1(long pos){return (int)pos%499999;}

void clearHashTable(long **ht){for (int i=0; i<HASH_COUNT; i++) {ht[i][0] = -1;ht[i][1] =0;}}

void update(long **ht,long pos ,int val){
    int ct = hashFunction1(pos);
    while (ht[ct][0] !=-1 && ht[ct][0] !=pos)ct=((ct+1)%HASH_COUNT);
    ht[ct][0]=pos;
    ht[ct][1]=val;
}

int querry(long **ht,long pos){
    int ct = hashFunction1(pos);
    while (ht[ct][0] !=-1 && ht[ct][0] !=pos)ct=((ct+1)%HASH_COUNT);
    return (int)ht[ct][1];
}

int main(int argc, const char * argv[]) {
    //freopen("/Users/shashanksaurabh/Desktop/Journey/E. Little Elephant and Inversions/E. Little Elephant and Inversions/input.txt", "r", stdin);
    //freopen("/Users/shashanksaurabh/Desktop/Journey/E. Little Elephant and Inversions/E. Little Elephant and Inversions/output.txt", "w", stdout);
    int n ,k,ic=0,lt=0,counter=0;
    long prev=-1;
    long long ans=-1;
    cin>>n>>k;
    int *st1=new int[2*n];
    int *st2=new int[2*n];
    long *arr = new long[n];
    long *arr2 = new long[n];
    long **ht = new long*[HASH_COUNT];
    for (int i=0; i<HASH_COUNT; i++) {
        ht[i]=new long[2];
    }
    for (int i=0; i<n; i++){cin>>arr[i];arr2[i]=arr[i];}
    for (int i=0; i<2*n; i++) {st1[i]=0;st2[i]=0;}
    sort(arr2,arr2+n);
    clearHashTable(ht);
    for (int i=0; i<n; i++) {
        if(prev!=arr2[i]){
            update(ht,arr2[i],counter);
            prev = arr2[i];
            ++counter;
        }
    }
    for (int i=0; i<n; i++) {arr[i]=querry(ht,arr[i]);}
    update(st1,n,(int)arr[0],1);
    update(st2,n,(int)arr[n-1],1);
    ic+=querry(st1, n,(int)arr[0]+1,n-1)+ querry(st2, n, 0, (int)arr[0]-1);
    for (int i=1; i<n-1; i++) {
        update(st1,n,(int)arr[i],1);
        ic+=querry(st1, n,(int)arr[i]+1,n-1)+ querry(st2, n, 0, (int)arr[i]-1);
        if(ic>k){ans=i;lt=i;break;}
    }
    //for(int i=n;i<2*n;i++)cout<<st1[i]<<"\t";
    //cout<<endl;
    //for(int i=n;i<2*n;i++)cout<<st2[i]<<"\t";
    if(ans == -1){
        ans = n*(n-1)/2;
    }
    else{
        update(st1, n, (int)arr[lt], -1);
        ic-=(querry(st1,n,(int)arr[lt]+1,n-1)+ querry(st2,n,0,(int)arr[lt]-1));
        --lt;
        for (int i=n-2;i>lt&&i>1; --i) {
            update(st2, n, (int)arr[i], 1);
            ic+=querry(st1, n, (int)arr[i]+1, n-1) + querry(st2, n, 0, (int)arr[i]-1);
            while (ic>k && lt>0) {
                update(st1, n, (int)arr[lt], -1);
                ic-=(querry(st1,n,(int)arr[lt]+1,n-1)+ querry(st2,n,0,(int)arr[lt]-1));
                --lt;
            }
            if (ic<=k) {
                ans+=lt+1;
            }
            else break;
        }

    }
    cout<<ans<<endl;
    return 0;
}


//
//  main.cpp
//  E. Little Elephant and Inversions
//
//  Created by Shashank Saurabh on 3/23/18.
//  Copyright (c) 2018 Shashank Saurabh. All rights reserved.
//

#include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

#define HASH_COUNT 500000

void update(int *st,int n,int pos,int val){
    pos+=n;
    st[pos]+=val;
    for(;pos>1;pos>>=1){
        st[pos>>1] = st[pos] + st[pos^1];
    }
}

int querry (int *st,int n,int l,int r){
    int res = 0;
    if(l<=r && r>=0)res+=st[r+n];
    for (l+=n,r+=n; l<r; l>>=1,r>>=1) {
        if(l&1)res+=st[l++];
        if(r&1)res+=st[--r];
    }
    return res;
}

int hashFunction1(long pos){return (int)pos%499999;}

void clearHashTable(long **ht){for (int i=0; i<HASH_COUNT; i++) {ht[i][0] = -1;ht[i][1] =0;}}

void update(long **ht,long pos ,int val){
    int ct = hashFunction1(pos);
    while (ht[ct][0] !=-1 && ht[ct][0] !=pos)ct=((ct+1)%HASH_COUNT);
    ht[ct][0]=pos;
    ht[ct][1]=val;
}

int querry(long **ht,long pos){
    int ct = hashFunction1(pos);
    while (ht[ct][0] !=-1 && ht[ct][0] !=pos)ct=((ct+1)%HASH_COUNT);
    return (int)ht[ct][1];
}

int main(int argc, const char * argv[]) {
    //freopen("/Users/shashanksaurabh/Desktop/Journey/E. Little Elephant and Inversions/E. Little Elephant and Inversions/input.txt", "r", stdin);
    //freopen("/Users/shashanksaurabh/Desktop/Journey/E. Little Elephant and Inversions/E. Little Elephant and Inversions/output.txt", "w", stdout);
    int n ,lt=0,counter=0;
    long prev=-1;
    long long ans=-1,ic=0,k;
    cin>>n>>k;
    int *st1=new int[2*n];
    int *st2=new int[2*n];
    long *arr = new long[n];
    long *arr2 = new long[n];
    long **ht = new long*[HASH_COUNT];
    for (int i=0; i<HASH_COUNT; i++) {
        ht[i]=new long[2];
    }
    for (int i=0; i<n; i++){cin>>arr[i];arr2[i]=arr[i];}
    for (int i=0; i<2*n; i++) {st1[i]=0;st2[i]=0;}
    sort(arr2,arr2+n);
    clearHashTable(ht);
    for (int i=0; i<n; i++) {
        if(prev!=arr2[i]){
            update(ht,arr2[i],counter);
            prev = arr2[i];
            ++counter;
        }
    }
    for (int i=0; i<n; i++) {arr[i]=querry(ht,arr[i]);}
    update(st1,n,(int)arr[0],1);
    update(st2,n,(int)arr[n-1],1);
    ic+=querry(st1, n,(int)arr[0]+1,n-1)+ querry(st2, n, 0, (int)arr[0]-1);
    for (int i=1; i<n-1; i++) {
        update(st1,n,(int)arr[i],1);
        ic+=querry(st1, n,(int)arr[i]+1,n-1)+ querry(st2, n, 0, (int)arr[i]-1);
        if(ic>k){ans=i;lt=i;break;}
    }
    if(ans == -1){
        ans = n*(n-1)/2;
    }
    else{
        update(st1, n, (int)arr[lt], -1);
        ic-=(querry(st1,n,(int)arr[lt]+1,n-1)+ querry(st2,n,0,(int)arr[lt]-1));
        --lt;
        for (int i=n-2;i>lt&&i>1; --i) {
            update(st2, n, (int)arr[i], 1);
            ic+=querry(st1, n, (int)arr[i]+1, n-1) + querry(st2, n, 0, (int)arr[i]-1);
            while (ic>k && lt>0) {
                update(st1, n, (int)arr[lt], -1);
                ic-=(querry(st1,n,(int)arr[lt]+1,n-1)+ querry(st2,n,0,(int)arr[lt]-1));
                --lt;
            }
            if (ic<=k) {
                ans+=lt+1;
            }
            else break;
        }

    }
    cout<<ans<<endl;
    return 0;
}


//
//  main.cpp
//  E. Little Elephant and Inversions
//
//  Created by Shashank Saurabh on 3/23/18.
//  Copyright (c) 2018 Shashank Saurabh. All rights reserved.
//

#include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

#define HASH_COUNT 500000

void update(int *st,int n,int pos,int val){
    pos+=n;
    st[pos]+=val;
    for(;pos>1;pos>>=1){
        st[pos>>1] = st[pos] + st[pos^1];
    }
}

int querry (int *st,int n,int l,int r){
    int res = 0;
    if(l<=r && r>=0)res+=st[r+n];
    for (l+=n,r+=n; l<r; l>>=1,r>>=1) {
        if(l&1)res+=st[l++];
        if(r&1)res+=st[--r];
    }
    return res;
}

int hashFunction1(long pos){return (int)pos%499999;}

void clearHashTable(long **ht){for (int i=0; i<HASH_COUNT; i++) {ht[i][0] = -1;ht[i][1] =0;}}

void update(long **ht,long pos ,int val){
    int ct = hashFunction1(pos);
    while (ht[ct][0] !=-1 && ht[ct][0] !=pos)ct=((ct+1)%HASH_COUNT);
    ht[ct][0]=pos;
    ht[ct][1]=val;
}

int querry(long **ht,long pos){
    int ct = hashFunction1(pos);
    while (ht[ct][0] !=-1 && ht[ct][0] !=pos)ct=((ct+1)%HASH_COUNT);
    return (int)ht[ct][1];
}

int main(int argc, const char * argv[]) {
    //freopen("/Users/shashanksaurabh/Desktop/Journey/E. Little Elephant and Inversions/E. Little Elephant and Inversions/input.txt", "r", stdin);
    //freopen("/Users/shashanksaurabh/Desktop/Journey/E. Little Elephant and Inversions/E. Little Elephant and Inversions/output.txt", "w", stdout);
    int n ,lt=0,counter=0;
    long prev=-1;
    long long ans=-1,ic=0,k;
    cin>>n>>k;
    int *st1=new int[2*n];
    int *st2=new int[2*n];
    long *arr = new long[n];
    long *arr2 = new long[n];
    long **ht = new long*[HASH_COUNT];
    for (int i=0; i<HASH_COUNT; i++) {
        ht[i]=new long[2];
    }
    for (int i=0; i<n; i++){cin>>arr[i];arr2[i]=arr[i];}
    for (int i=0; i<2*n; i++) {st1[i]=0;st2[i]=0;}
    sort(arr2,arr2+n);
    clearHashTable(ht);
    for (int i=0; i<n; i++) {
        if(prev!=arr2[i]){
            update(ht,arr2[i],counter);
            prev = arr2[i];
            ++counter;
        }
    }
    for (int i=0; i<n; i++) {arr[i]=querry(ht,arr[i]);}
    update(st1,n,(int)arr[0],1);
    update(st2,n,(int)arr[n-1],1);
    ic+=(long long)querry(st1, n,(int)arr[0]+1,n-1)+ (long long)querry(st2, n, 0, (int)arr[0]-1);
    for (int i=1; i<n-1; i++) {
        update(st1,n,(int)arr[i],1);
        ic+=(long long)querry(st1, n,(int)arr[i]+1,n-1)+ (long long)querry(st2, n, 0, (int)arr[i]-1);
        if(ic>k){ans=i;lt=i;break;}
    }
    if(ans == -1){
            ans = ((long long)n*(long long)(n-1))/2;
    }
    else{
        update(st1, n, (int)arr[lt], -1);
        ic-=((long long)querry(st1,n,(int)arr[lt]+1,n-1)+ (long long)querry(st2,n,0,(int)arr[lt]-1));
        --lt;
        for (int i=n-2;i>lt&&i>1; --i) {
            update(st2, n, (int)arr[i], 1);
            ic+=(long long)querry(st1, n, (int)arr[i]+1, n-1) + (long long)querry(st2, n, 0, (int)arr[i]-1);
            while (ic>k && lt>0) {
                update(st1, n, (int)arr[lt], -1);
                ic-=((long long)querry(st1,n,(int)arr[lt]+1,n-1)+ (long long)querry(st2,n,0,(int)arr[lt]-1));
                --lt;
            }
            if (ic<=k) {
                ans+=lt+1;
            }
            else break;
        }

    }
    cout<<ans<<endl;
    return 0;
}


//
//  main.cpp
//  E. Little Elephant and Inversions
//
//  Created by Shashank Saurabh on 3/23/18.
//  Copyright (c) 2018 Shashank Saurabh. All rights reserved.
//

#include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

#define HASH_COUNT 500000

void update(int *st,int n,int pos,int val){
    pos+=n;
    st[pos]+=val;
    for(;pos>1;pos>>=1){
        st[pos>>1] = st[pos] + st[pos^1];
    }
}

int querry (int *st,int n,int l,int r){
    int res = 0;
    if(l<=r && r>=0)res+=st[r+n];
    for (l+=n,r+=n; l<r; l>>=1,r>>=1) {
        if(l&1)res+=st[l++];
        if(r&1)res+=st[--r];
    }
    return res;
}

int hashFunction1(long pos){return (int)pos%499999;}

void clearHashTable(long **ht){for (int i=0; i<HASH_COUNT; i++) {ht[i][0] = -1;ht[i][1] =0;}}

void update(long **ht,long pos ,int val){
    int ct = hashFunction1(pos);
    while (ht[ct][0] !=-1 && ht[ct][0] !=pos)ct=((ct+1)%HASH_COUNT);
    ht[ct][0]=pos;
    ht[ct][1]=val;
}

int querry(long **ht,long pos){
    int ct = hashFunction1(pos);
    while (ht[ct][0] !=-1 && ht[ct][0] !=pos)ct=((ct+1)%HASH_COUNT);
    return (int)ht[ct][1];
}

int main(int argc, const char * argv[]) {
    //freopen("/Users/shashanksaurabh/Desktop/Journey/E. Little Elephant and Inversions/E. Little Elephant and Inversions/input.txt", "r", stdin);
    //freopen("/Users/shashanksaurabh/Desktop/Journey/E. Little Elephant and Inversions/E. Little Elephant and Inversions/output.txt", "w", stdout);
    int n ,lt=0,counter=0;
    long prev=-1;
    long long ans=-1,ic=0,k;
    cin>>n>>k;
    int *st1=new int[2*n];
    int *st2=new int[2*n];
    long *arr = new long[n];
    long *arr2 = new long[n];
    long **ht = new long*[HASH_COUNT];
    for (int i=0; i<HASH_COUNT; i++) {
        ht[i]=new long[2];
    }
    for (int i=0; i<n; i++){cin>>arr[i];arr2[i]=arr[i];}
    for (int i=0; i<2*n; i++) {st1[i]=0;st2[i]=0;}
    sort(arr2,arr2+n);
    clearHashTable(ht);
    for (int i=0; i<n; i++) {
        if(prev!=arr2[i]){
            update(ht,arr2[i],counter);
            prev = arr2[i];
            ++counter;
        }
    }
    for (int i=0; i<n; i++) {arr[i]=querry(ht,arr[i]);}
    update(st1,n,(int)arr[0],1);
    update(st2,n,(int)arr[n-1],1);
    ic+=(long long)querry(st1, n,(int)arr[0]+1,n-1)+ (long long)querry(st2, n, 0, (int)arr[0]-1);
    if(ic>k){
        cout<<0;
    }
    else{
        for (int i=1; i<n-1; i++) {
            update(st1,n,(int)arr[i],1);
            ic+=(long long)querry(st1, n,(int)arr[i]+1,n-1)+ (long long)querry(st2, n, 0, (int)arr[i]-1);
            if(ic>k){ans=i;lt=i;break;}
        }
        if(ans == -1){
            ans = ((long long)n*(long long)(n-1))/2;
        }
        else{
            update(st1, n, (int)arr[lt], -1);
            ic-=((long long)querry(st1,n,(int)arr[lt]+1,n-1)+ (long long)querry(st2,n,0,(int)arr[lt]-1));
            --lt;
            for (int i=n-2;i>lt&&i>1; --i) {
                update(st2, n, (int)arr[i], 1);
                ic+=(long long)querry(st1, n, (int)arr[i]+1, n-1) + (long long)querry(st2, n, 0, (int)arr[i]-1);
                while (ic>k && lt>0) {
                    update(st1, n, (int)arr[lt], -1);
                    ic-=((long long)querry(st1,n,(int)arr[lt]+1,n-1)+ (long long)querry(st2,n,0,(int)arr[lt]-1));
                    --lt;
                }
                if (ic<=k) {
                    ans+=lt+1;
                }
                else break;
            }

        }
        cout<<ans<<endl;
    }
    return 0;
}


//
//  main.cpp
//  E. Little Elephant and Inversions
//
//  Created by Shashank Saurabh on 3/23/18.
//  Copyright (c) 2018 Shashank Saurabh. All rights reserved.
//

#include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

#define HASH_COUNT 500000

void update(int *st,int n,int pos,int val){
    pos+=n;
    st[pos]+=val;
    for(;pos>1;pos>>=1){
        st[pos>>1] = st[pos] + st[pos^1];
    }
}

int querry (int *st,int n,int l,int r){
    int res = 0;
    if(l<=r && r>=0)res+=st[r+n];
    for (l+=n,r+=n; l<r; l>>=1,r>>=1) {
        if(l&1)res+=st[l++];
        if(r&1)res+=st[--r];
    }
    return res;
}

int hashFunction1(long pos){return (int)pos%499999;}

void clearHashTable(long **ht){for (int i=0; i<HASH_COUNT; i++) {ht[i][0] = -1;ht[i][1] =0;}}

void update(long **ht,long pos ,int val){
    int ct = hashFunction1(pos);
    while (ht[ct][0] !=-1 && ht[ct][0] !=pos)ct=((ct+1)%HASH_COUNT);
    ht[ct][0]=pos;
    ht[ct][1]=val;
}

int querry(long **ht,long pos){
    int ct = hashFunction1(pos);
    while (ht[ct][0] !=-1 && ht[ct][0] !=pos)ct=((ct+1)%HASH_COUNT);
    return (int)ht[ct][1];
}

int main(int argc, const char * argv[]) {
    //freopen("/Users/shashanksaurabh/Desktop/Journey/E. Little Elephant and Inversions/E. Little Elephant and Inversions/input.txt", "r", stdin);
    //freopen("/Users/shashanksaurabh/Desktop/Journey/E. Little Elephant and Inversions/E. Little Elephant and Inversions/output.txt", "w", stdout);
    int n ,lt=0,counter=0;
    long prev=-1;
    long long ans=-1,ic=0,k;
    cin>>n>>k;
    int *st1=new int[2*n];
    int *st2=new int[2*n];
    long *arr = new long[n];
    long *arr2 = new long[n];
    long **ht = new long*[HASH_COUNT];
    for (int i=0; i<HASH_COUNT; i++) {
        ht[i]=new long[2];
    }
    for (int i=0; i<n; i++){cin>>arr[i];arr2[i]=arr[i];}
    for (int i=0; i<2*n; i++) {st1[i]=0;st2[i]=0;}
    sort(arr2,arr2+n);
    clearHashTable(ht);
    for (int i=0; i<n; i++) {
        if(prev!=arr2[i]){
            update(ht,arr2[i],counter);
            prev = arr2[i];
            ++counter;
        }
    }
    for (int i=0; i<n; i++) {arr[i]=querry(ht,arr[i]);}
    update(st1,n,(int)arr[0],1);
    update(st2,n,(int)arr[n-1],1);
    ic+=(long long)querry(st1, n,(int)arr[0]+1,n-1)+ (long long)querry(st2, n, 0, (int)arr[0]-1);
    for (int i=1; i<n-1; i++) {
        update(st1,n,(int)arr[i],1);
        ic+=(long long)querry(st1, n,(int)arr[i]+1,n-1)+ (long long)querry(st2, n, 0, (int)arr[i]-1);
        if(ic>k){ans=i;lt=i;break;}
    }
    if(ans == -1){
        ans = n*(n-1)/2;
    }
    else{
        update(st1, n, (int)arr[lt], -1);
        ic-=((long long)querry(st1,n,(int)arr[lt]+1,n-1)+ (long long)querry(st2,n,0,(int)arr[lt]-1));
        --lt;
        for (int i=n-2;i>lt&&i>1; --i) {
            update(st2, n, (int)arr[i], 1);
            ic+=(long long)querry(st1, n, (int)arr[i]+1, n-1) + (long long)querry(st2, n, 0, (int)arr[i]-1);
            while (ic>k && lt>0) {
                update(st1, n, (int)arr[lt], -1);
                ic-=((long long)querry(st1,n,(int)arr[lt]+1,n-1)+ (long long)querry(st2,n,0,(int)arr[lt]-1));
                --lt;
            }
            if (ic<=k) {
                ans+=lt+1;
            }
            else break;
        }

    }
    cout<<ans<<endl;
    return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>

#define inf 1<<25
#define maxn 1000001
#define ll long long
#define lson l,mid,2*id
#define rson mid+1,r,2*id+1

using namespace std;

struct Fenwick
{
	int d[maxn];
    void add(int id,int val)
    {
        id++;
        while(id<=maxn)
        {
            d[id]+= val;
            id += (id) & (-id);
        }
    }
    int sum(int id)
    {
        int ret = 0;id++;
        while(id>0)
        {
            ret += d[id];
            id -= (id) & (-id);
        }
        return ret;
    }
    void clear()
    {
        memset(d,0,sizeof d);
    }
	int ssum(int l,int r)
	{
		return sum(r)-sum(l-1);
	}
}ft1,ft2;

int a[maxn],b[maxn];
ll s,s1;

int main()
{
	ll n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i-1]=a[i];
	}
	sort(b,b+n);
	int m=unique(b,b+n)-b;
	for(int i=1;i<=n;i++)
		a[i]=lower_bound(b,b+m,a[i])-b+1;
	for(int i=1;i<=n;i++)
	{
		ft1.add(a[i],1);
		s+=i-ft1.sum(a[i]);
	}
	if(s<=k)
	{
		cout<<n*(n-1)/2<<endl;
		return 0;
	}
	ft2.add(a[n],1);
	ft1.add(a[n],-1);
	for(int r=n-1,l=n-1;r>0;r--)
	{
		while(l>0 && s>k)
		{
			s-=l-ft1.sum(a[l]);
			s-=ft2.sum(a[l]-1);
			ft1.add(a[l],-1);
			l--;
		}
		if(l==0)
			break;
		s1+=l;
		s+=ft2.sum(a[r]-1);
		s+=l-ft1.sum(a[r]);
		ft2.add(a[r],1);
	}
	cout<<s1<<endl;
    return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>

#define inf 1<<25
#define maxn 1000001
#define ll long long
#define lson l,mid,2*id
#define rson mid+1,r,2*id+1

using namespace std;

struct Fenwick
{
	int d[maxn];
    void add(int id,int val)
    {
        id++;
        while(id<=maxn)
        {
            d[id]+= val;
            id += (id) & (-id);
        }
    }
    int sum(int id)
    {
        int ret = 0;id++;
        while(id>0)
        {
            ret += d[id];
            id -= (id) & (-id);
        }
        return ret;
    }
    void clear()
    {
        memset(d,0,sizeof d);
    }
	int ssum(int l,int r)
	{
		return sum(r)-sum(l-1);
	}
}ft1,ft2;

int a[maxn],b[maxn];
ll s,s1;

int main()
{
	ll n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i]=a[i];
	}
	sort(b,b+n);
	int m=unique(b,b+n)-b;
	for(int i=1;i<=n;i++)
		a[i]=lower_bound(b,b+m,a[i])-b+1;
	for(int i=1;i<=n;i++)
	{
		ft1.add(a[i],1);
		s+=i-ft1.sum(a[i]);
	}
	if(s<=k)
	{
		cout<<n*(n-1)/2<<endl;
		return 0;
	}
	ft2.add(a[n],1);
	ft1.add(a[n],-1);
	for(int r=n,l=n-1;;)
	{
		while(l>0 && s>k)
		{
			s-=l-ft1.sum(a[l]);
			s-=ft2.sum(a[l]-1);
			ft1.add(a[l],-1);
			l--;
		}
		if(l==0)
			break;
		s1+=l;
r--;
		s+=ft2.sum(a[r]-1);
		s+=l-ft1.sum(a[r]);
		ft2.add(a[r],1);
	}
	cout<<s1<<endl;
    return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>

#define inf 1<<25
#define maxn 1000001
#define ll long long
#define lson l,mid,2*id
#define rson mid+1,r,2*id+1

using namespace std;

struct Fenwick
{
	int d[maxn];
    void add(int id,int val)
    {
        id++;
        while(id<=maxn)
        {
            d[id]+= val;
            id += (id) & (-id);
        }
    }
    int sum(int id)
    {
        int ret = 0;id++;
        while(id>0)
        {
            ret += d[id];
            id -= (id) & (-id);
        }
        return ret;
    }
    void clear()
    {
        memset(d,0,sizeof d);
    }
	int ssum(int l,int r)
	{
		return sum(r)-sum(l-1);
	}
}ft1,ft2;

int a[maxn],b[maxn];
ll s,s1;

int main()
{
	ll n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i]=a[i];
	}
	sort(b,b+n);
	int m=unique(b,b+n)-b;
	for(int i=1;i<=n;i++)
		a[i]=lower_bound(b,b+m,a[i])-b+1;
	for(int i=1;i<=n;i++)
	{
		ft1.add(a[i],1);
		s+=i-ft1.sum(a[i]);
	}
	if(s<=k)
	{
		cout<<n*(n-1)/2<<endl;
		return 0;
	}
	ft2.add(a[n],1);
	ft1.add(a[n],-1);
	for(int r=n-1,l=n-1;r>0;r--)
	{
		while(l>0 && s>k)
		{
			s-=l-ft1.sum(a[l]);
			s-=ft2.sum(a[l]-1);
			ft1.add(a[l],-1);
			l--;
		}
		if(l==0)
			break;
		s1+=l;
		s+=ft2.sum(a[r]-1);
		s+=l-ft1.sum(a[r]);
		ft2.add(a[r],1);
	}
	cout<<s<<endl;
    return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>

#define inf 1<<25
#define maxn 1000001
#define ll long long
#define lson l,mid,2*id
#define rson mid+1,r,2*id+1

using namespace std;

struct Fenwick
{
	int d[maxn];
    void add(int id,int val)
    {
        id++;
        while(id<=maxn)
        {
            d[id]+= val;
            id += (id) & (-id);
        }
    }
    int sum(int id)
    {
        int ret = 0;id++;
        while(id>0)
        {
            ret += d[id];
            id -= (id) & (-id);
        }
        return ret;
    }
    void clear()
    {
        memset(d,0,sizeof d);
    }
	int ssum(int l,int r)
	{
		return sum(r)-sum(l-1);
	}
}ft1,ft2;

int a[maxn],b[maxn];
ll s,s1;

int main()
{
	ll n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i]=a[i];
	}
	sort(b,b+n);
	int m=unique(b,b+n)-b;
	for(int i=1;i<=n;i++)
		a[i]=lower_bound(b,b+m,a[i])-b+1;
	for(int i=1;i<=n;i++)
	{
		ft1.add(a[i],1);
		s+=i-ft1.sum(a[i]);
	}
	if(s<=k)
	{
		cout<<n*(n-1)/2<<endl;
		return 0;
	}
	ft2.add(a[n],1);
	ft1.add(a[n],-1);
	for(int r=n-1,l=n-1;r>0;r--)
	{
		while(l>0 && s>k)
		{
			s-=l-ft1.sum(a[l]);
			s-=ft2.sum(a[l]-1);
			ft1.add(a[l],-1);
			l--;
		}
		if(l==0)
			break;
		s1+=l;
		s+=ft2.sum(a[r]-1);
		s+=l-ft1.sum(a[r]);
		ft2.add(a[r],1);
	}
	cout<<s1<<endl;
    return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>

#define inf 1<<25
#define maxn 1000001
#define ll long long
#define lson l,mid,2*id
#define rson mid+1,r,2*id+1

using namespace std;

struct Fenwick
{
	int d[maxn];
    void add(int id,int val)
    {
        //id++;
        while(id<=maxn)
        {
            d[id]+= val;
            id += (id) & (-id);
        }
    }
    int sum(int id)
    {
        int ret = 0;id++;
        while(id>0)
        {
            ret += d[id];
            id -= (id) & (-id);
        }
        return ret;
    }
    void clear()
    {
        memset(d,0,sizeof d);
    }
	int ssum(int l,int r)
	{
		return sum(r)-sum(l-1);
	}
}ft1,ft2;

int a[maxn],b[maxn];
ll s,s1;

int main()
{
	ll n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i]=a[i];
	}
	sort(b,b+n);
	int m=unique(b,b+n)-b;
	for(int i=1;i<=n;i++)
		a[i]=lower_bound(b,b+m,a[i])-b+1;
	for(int i=1;i<=n;i++)
	{
		ft1.add(a[i],1);
		s+=i-ft1.sum(a[i]);
	}
	if(s<=k)
	{
		cout<<n*(n-1)/2<<endl;
		return 0;
	}
	ft2.add(a[n],1);
	ft1.add(a[n],-1);
	for(int r=n,l=n-1;;)
	{
		while(l>0 && s>k)
		{
			s-=l-ft1.sum(a[l]);
			s-=ft2.sum(a[l]-1);
			ft1.add(a[l],-1);
			l--;
		}
		if(l==0)
			break;
		s1+=l;
r--;
		s+=ft2.sum(a[r]-1);
		s+=l-ft1.sum(a[r]);
		ft2.add(a[r],1);
	}
	cout<<s1<<endl;
    return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>

#define inf 1<<25
#define maxn 1000001
#define ll long long
#define lson l,mid,2*id
#define rson mid+1,r,2*id+1

using namespace std;

struct Fenwick
{
	int d[maxn];
    void add(int id,int val)
    {
        id++;
        while(id<=maxn)
        {
            d[id]+= val;
            id += (id) & (-id);
        }
    }
    int sum(int id)
    {
        int ret = 0;id++;
        while(id>0)
        {
            ret += d[id];
            id -= (id) & (-id);
        }
        return ret;
    }
    void clear()
    {
        memset(d,0,sizeof d);
    }
	int ssum(int l,int r)
	{
		return sum(r)-sum(l-1);
	}
}ft1,ft2;

int a[maxn],b[maxn];
ll s,s1;

int main()
{
	ll n,k;
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		b[i]=a[i];
	}
	sort(b,b+n);
	int m=unique(b,b+n)-b;
	for(int i=1;i<=n;i++)
		a[i]=lower_bound(b,b+m,a[i])-b+1;
	for(int i=1;i<=n;i++)
	{
		ft1.add(a[i],1);
		s+=i-ft1.sum(a[i]);
	}
	if(s<=k)
	{
		cout<<n*(n-1)/2<<endl;
		return 0;
	}
	ft2.add(a[n],1);
	ft1.add(a[n],-1);
	for(int r=n,l=n-1;r>0;r--)
	{
		while(l>0 && s>k)
		{
			s-=l-ft1.sum(a[l]);
			s-=ft2.sum(a[l]-1);
			ft1.add(a[l],-1);
			l--;
		}
		if(l==0)
			break;
		s1+=l;
r--;
		s+=ft2.sum(a[r]-1);
		s+=l-ft1.sum(a[r]);
		ft2.add(a[r],1);
	}
	cout<<s1<<endl;
    return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m;
long long k;
int a[100020],b[100020];
int c1[100020],c2[100020];
void R(int *a,int x,int y)
{
	for(;x<=m;x+=x&-x)
		a[x]+=y;
}
int G(int *a,int x){
	int _=0;
	for(;x;x-=x&-x)
		_+=a[x];
	return _;
}
long long s,z;
int main()
{
	scanf("%d%I64d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d",a+i),b[m++]=a[i];
	sort(b,b+m);
	m=unique(b,b+m)-b;
	for(int i=1;i<=n;i++)
		a[i]=lower_bound(b,b+m,a[i])-b+1;
	for(int i=1;i<=n;i++)
	{
		R(c1,a[i],1);
		s+=i-G(c1,a[i]);
	}
	if(s<=k)
		return cout<<(long long)n*(n-1)/2,0;
	R(c1,a[n],-1);
	R(c2,a[n],1);
	cerr << s << endl;
	for(int r=n,l=n-1;;)
	{
		while(l>0&&s>k)
		{
			s-=l-G(c1,a[l]);
			s-=G(c2,a[l]-1);
			R(c1,a[l],-1);
			l--;
		}
		if(l==0)
			break;
		z+=l;
		r--;
		s+=l-G(c1,a[r]);
		s+=G(c2,a[r]);
		R(c2,a[r],1);
	}
	printf("%I64d",z);
	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdio>
#include <set>
#include <map>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

#ifdef ONLINE_JUDGE

#define fin cin
#define fout cout

#else

fstream fin("input.txt", fstream::in);
fstream fout("output.txt", fstream::out);

#endif

struct TNode {
    TNode *left, *right;
    int x, y;
    int size;

    TNode::TNode(int _x = 0);
};

TNode *null;

void init() {
    null = new TNode();
    null->left = null->right = null;
    null->size = 0;
}

int uniform() {
    return rand() * RAND_MAX + rand();
}

TNode::TNode(int _x)
    : left(null)
    , right(null)
    , x(_x)
    , y(uniform())
    , size(1)
{
}

void relax(TNode *root) {
    root->size = 1 + root->left->size + root->right->size;
}

void split(TNode *root, int x, TNode **left, TNode **right) {
    if (root == null) {
        *left = null;
        *right = null;
        return;
    }
    if (root->x <= x) {
        split(root->right, x, left, right);
        root->right = *left;
        *left = root;
    } else {
        split(root->left, x, left, right);
        root->left = *right;
        *right = root;
    }
    relax(root);
}

TNode *merge(TNode *left, TNode *right) {
    if (left == null) {
        return right;
    }
    if (right == null) {
        return left;
    }

    TNode *root;
    if (left->y > right->y) {
        left->right = merge(left->right, right);
        root = left;
    } else {
        right->left = merge(left, right->left);
        root = right;
    }
    relax(root);
    return root;
}

TNode *insert(TNode *root, int x) {
    TNode *left, *right;
    split(root, x, &left, &right);
    return merge(left, merge(new TNode(x), right));
}

TNode *erase(TNode *root, int x) {
    TNode *left, *middle1, *middle2, *right;
    split(root, x, &left, &right);
    split(left, x - 1, &middle1, &middle2);
    middle2 = merge(middle2->left, middle2->right);
    return merge(middle1, merge(middle2, right));
}

int lessThan(TNode *root, int x) {
    TNode *left, *right;
    split(root, x - 1, &left, &right);
    int ans = left->size;
    root = merge(left, right);
    return ans;
}

int moreThan(TNode *root, int x) {
    return root->size - lessThan(root, x + 1);
}

int main() {
	fout.precision(10);
	fout << fixed;

    init();

    int N;
    long long K;
    fin >> N >> K;
    vector<int> a(N);
    for (int i = 0; i < N; ++i) {
        fin >> a[i];
    }
    int L = 0, R = N;
    long long inv = 0;
    TNode *left = null, *right = null;
    left = insert(left, a[0]);
    while (R - L > 1) {
        --R;
        long long newInv = inv;
        newInv += moreThan(left, a[R]) + lessThan(right, a[R]);
        if (newInv <= K) {
            inv = newInv;
            right = insert(right, a[R]);
        } else {
            ++R;
            break;
        }
    }
    long long ans = N - R;
    for (L = 1; L < N; ++L) {
        inv += moreThan(left, a[L]) + lessThan(right, a[L]);
        left = insert(left, a[L]);
        while (inv > K && R < N || L >= R) {
            inv -= moreThan(left, a[R]) + lessThan(right, a[R]);
            right = erase(right, a[R]);
            ++R;
        }
        ans += N - R;
    }

    fout << ans << '\n';

	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdio>
#include <set>
#include <map>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

#ifdef ONLINE_JUDGE

#define fin cin
#define fout cout

#else

fstream fin("input.txt", fstream::in);
fstream fout("output.txt", fstream::out);

#endif

struct TNode {
    TNode *left, *right;
    int x, y;
    int size;

    TNode::TNode(int _x = 0);
};

TNode *null;

void init() {
    null = new TNode();
    null->left = null->right = null;
    null->size = 0;
}

int uniform() {
    return rand() * RAND_MAX + rand();
}

TNode::TNode(int _x)
    : left(null)
    , right(null)
    , x(_x)
    , y(uniform())
    , size(1)
{
}

void relax(TNode *root) {
    root->size = 1 + root->left->size + root->right->size;
}

void split(TNode *root, int x, TNode **left, TNode **right) {
    if (root == null) {
        *left = null;
        *right = null;
        return;
    }
    if (root->x <= x) {
        split(root->right, x, left, right);
        root->right = *left;
        *left = root;
    } else {
        split(root->left, x, left, right);
        root->left = *right;
        *right = root;
    }
    relax(root);
}

TNode *merge(TNode *left, TNode *right) {
    if (left == null) {
        return right;
    }
    if (right == null) {
        return left;
    }

    TNode *root;
    if (left->y > right->y) {
        left->right = merge(left->right, right);
        root = left;
    } else {
        right->left = merge(left, right->left);
        root = right;
    }
    relax(root);
    return root;
}

TNode *insert(TNode *root, int x) {
    TNode *left, *right;
    split(root, x, &left, &right);
    return merge(left, merge(new TNode(x), right));
}

TNode *erase(TNode *root, int x) {
    TNode *left, *middle1, *middle2, *right;
    split(root, x, &left, &right);
    split(left, x - 1, &middle1, &middle2);
    middle2 = merge(middle2->left, middle2->right);
    return merge(middle1, merge(middle2, right));
}

int lessThan(TNode *root, int x) {
    TNode *left, *right;
    split(root, x - 1, &left, &right);
    int ans = left->size;
    root = merge(left, right);
    return ans;
}

int moreThan(TNode *root, int x) {
    return root->size - lessThan(root, x + 1);
}

int main() {
	fout.precision(10);
	fout << fixed;

    init();

    int N;
    long long K;
    fin >> N >> K;
    vector<int> a(N);
    for (int i = 0; i < N; ++i) {
        fin >> a[i];
    }
    int L = 0, R = N;
    long long inv = 0;
    TNode *left = null, *right = null;
    left = insert(left, a[0]);
    while (R - L > 1) {
        --R;
        long long newInv = inv;
        newInv += moreThan(left, a[R]) + lessThan(right, a[R]);
        if (newInv <= K) {
            inv = newInv;
            right = insert(right, a[R]);
        } else {
            ++R;
            break;
        }
    }
    long long ans = N - R;
    for (L = 1; L < N; ++L) {
        inv += moreThan(left, a[L]) + lessThan(right, a[L]);
        left = insert(left, a[L]);
        while (inv > K && R < N || L >= R) {
            inv -= moreThan(left, a[R]) + lessThan(right, a[R]);
            ++R;
        }
        ans += N - R;
    }

    fout << ans << '\n';

	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdio>
#include <set>
#include <map>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

#ifdef ONLINE_JUDGE

#define fin cin
#define fout cout

#else

fstream fin("input.txt", fstream::in);
fstream fout("output.txt", fstream::out);

#endif

struct TNode {
    TNode *left, *right;
    int x, y;
    int size;

    TNode::TNode(int _x = 0);
};

TNode *null;

void init() {
    null = new TNode();
    null->left = null->right = null;
    null->size = 0;
}

int uniform() {
    return rand() * RAND_MAX + rand();
}

TNode::TNode(int _x)
    : left(null)
    , right(null)
    , x(_x)
    , y(uniform())
    , size(1)
{
}

void relax(TNode *root) {
    root->size = 1 + root->left->size + root->right->size;
}

void split(TNode *root, int x, TNode **left, TNode **right) {
    if (root == null) {
        *left = null;
        *right = null;
        return;
    }
    if (root->x <= x) {
        split(root->right, x, left, right);
        root->right = *left;
        *left = root;
    } else {
        split(root->left, x, left, right);
        root->left = *right;
        *right = root;
    }
    relax(root);
}

TNode *merge(TNode *left, TNode *right) {
    if (left == null) {
        return right;
    }
    if (right == null) {
        return left;
    }

    TNode *root;
    if (left->y > right->y) {
        left->right = merge(left->right, right);
        root = left;
    } else {
        right->left = merge(left, right->left);
        root = right;
    }
    relax(root);
    return root;
}

TNode *insert(TNode *root, int x) {
    TNode *left, *right;
    split(root, x, &left, &right);
    return merge(left, merge(new TNode(x), right));
}

TNode *erase(TNode *root, int x) {
    TNode *left, *middle, *right;
    split(root, x, &left, &right);
    split(left, x - 1, &left, &middle);
    middle = merge(middle->left, middle->right);
    return merge(left, merge(middle, right));
}

int lessThan(TNode *root, int x) {
    TNode *left, *right;
    split(root, x - 1, &left, &right);
    int ans = left->size;
    root = merge(left, right);
    return ans;
}

int moreThan(TNode *root, int x) {
    return root->size - lessThan(root, x + 1);
}

int main() {
	fout.precision(10);
	fout << fixed;

    init();

    int N;
    long long K;
    fin >> N >> K;
    vector<int> a(N);
    for (int i = 0; i < N; ++i) {
        fin >> a[i];
    }
    int L = 0, R = N;
    long long inv = 0;
    TNode *left = null, *right = null;
    left = insert(left, a[0]);
    while (R - L > 1) {
        --R;
        long long newInv = inv;
        newInv += moreThan(left, a[R]) + lessThan(right, a[R]);
        if (newInv <= K) {
            inv = newInv;
            right = insert(right, a[R]);
        } else {
            ++R;
            break;
        }
    }
    long long ans = N - R;
    for (L = 1; L < N; ++L) {
        inv += moreThan(left, a[L]) + lessThan(right, a[L]);
        left = insert(left, a[L]);
        while (inv > K && R < N || L >= R) {
            inv -= moreThan(left, a[R]) + lessThan(right, a[R]);
            ++R;
        }
        ans += N - R;
    }

    fout << ans << '\n';

	return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdio>
#include <set>
#include <map>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

#ifdef ONLINE_JUDGE

#define fin cin
#define fout cout

#else

fstream fin("input.txt", fstream::in);
fstream fout("output.txt", fstream::out);

#endif

struct TNode {
    TNode *left, *right;
    int x, y;
    int size;

    TNode::TNode(int _x = 0);
};

TNode *null;

void init() {
    null = new TNode();
    null->left = null->right = null;
    null->size = 0;
}

int uniform() {
    return rand() * RAND_MAX + rand();
}

TNode::TNode(int _x)
    : left(null)
    , right(null)
    , x(_x)
    , y(uniform())
    , size(1)
{
}

void relax(TNode *root) {
    root->size = 1 + root->left->size + root->right->size;
}

void split(TNode *root, int x, TNode **left, TNode **right) {
    if (root == null) {
        *left = null;
        *right = null;
        return;
    }
    if (root->x <= x) {
        split(root->right, x, left, right);
        root->right = *left;
        *left = root;
    } else {
        split(root->left, x, left, right);
        root->left = *right;
        *right = root;
    }
    relax(root);
}

TNode *merge(TNode *left, TNode *right) {
    if (left == null) {
        return right;
    }
    if (right == null) {
        return left;
    }

    TNode *root;
    if (left->y > right->y) {
        left->right = merge(left->right, right);
        root = left;
    } else {
        right->left = merge(left, right->left);
        root = right;
    }
    relax(root);
    return root;
}

TNode *insert(TNode *root, int x) {
    TNode *left, *right;
    split(root, x, &left, &right);
    return merge(left, merge(new TNode(x), right));
}

TNode *erase(TNode *root, int x) {
    TNode *left, *middle, *right;
    split(root, x, &left, &right);
    split(left, x, &left, &middle);
    middle = merge(middle->left, middle->right);
    return merge(left, merge(middle, right));
}

int lessThan(TNode *root, int x) {
    TNode *left, *right;
    split(root, x - 1, &left, &right);
    int ans = left->size;
    root = merge(left, right);
    return ans;
}

int moreThan(TNode *root, int x) {
    return root->size - lessThan(root, x + 1);
}

int main() {
	fout.precision(10);
	fout << fixed;

    init();

    int N;
    long long K;
    fin >> N >> K;
    vector<int> a(N);
    for (int i = 0; i < N; ++i) {
        fin >> a[i];
    }
    int L = 0, R = N;
    long long inv = 0;
    TNode *left = null, *right = null;
    left = insert(left, a[0]);
    while (R - L > 1) {
        --R;
        long long newInv = inv;
        newInv += moreThan(left, a[R]) + lessThan(right, a[R]);
        if (newInv <= K) {
            inv = newInv;
            right = insert(right, a[R]);
        } else {
            ++R;
            break;
        }
    }
    long long ans = N - R;
    for (L = 1; L < N; ++L) {
        inv += moreThan(left, a[L]) + lessThan(right, a[L]);
        left = insert(left, a[L]);
        while (inv > K && R < N || L >= R) {
            inv -= moreThan(left, a[R]) + lessThan(right, a[R]);
            ++R;
        }
        ans += N - R;
    }

    fout << ans << '\n';

	return 0;
}