/*
    Marko Bakovic
    Little Elephant and Inversions
*/

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>

using namespace std;

typedef long long int lld;

const int maxn = 1e5 + 5;

int n, k, queue[ maxn ], size, temp[ maxn ], leftUpper[ maxn ], rightLower[ maxn ], BIT[ maxn ], BITnew[ maxn ];

lld sol, all;

map < int, int > M;

void update( int x, int val )
{
    while ( x <= size )
    {
        BIT[ x ] += val;
        x += x & -x;
    }
}

int get( int x )
{
    int ret = 0;
    while ( x )
    {
        ret += BIT[ x ];
        x -= x & -x;
    }
    return ret;
}

void updateNew( int x, int val )
{
    while ( x <= size )
    {
        BITnew[ x ] += val;
        x += x & -x;
    }
}

int getNew( int x )
{
    int ret = 0;
    while ( x )
    {
        ret += BITnew[ x ];
        x -= x & -x;
    }
    return ret;
}

int main()
{
    scanf( "%d", &n );
    cin >> k;
    for ( int i = 0; i < n; i++ )
    {
        scanf( "%d", &queue[ i ] );
        temp[ i ] = queue[ i ];
    }

    sort( temp, temp + n );
    size = 1;
    for ( int i = 0; i < n; i++ )
        if ( ! M.count( temp[ i ] ) ) M[ temp[ i ] ] = size++;
    for ( int i = 0; i < n; i++ ) queue[ i ] = M[ queue[ i ] ];

    for ( int i = 0; i < n; i++ )
    {
        leftUpper[ i ] = get( size - queue[ i ] - 1 );
        update( size - queue[ i ], 1 );
    }
    memset( BIT, 0, sizeof( BIT ) );
    for ( int i = n - 1; i >= 0; i-- )
    {
        rightLower[ i ] = get( queue[ i ] - 1 );
        update( queue[ i ], 1 );
    }
    memset( BIT, 0, sizeof( BIT ) );

    for ( int i = 0; i < n; i++ ) all += lld( rightLower[ i ] );

    if ( all <= k )
    {
        cout << lld( n ) * lld( n - 1 ) / 2LL << endl;
        return 0;
    }

    for ( int i = 1; i < n; i++ ) updateNew( queue[ i ], 1 );

    int left = 0, right = 1;
    update( size - queue[ left ], 1 );
    while ( left < n - 1 )
    {
        while ( right < n - 1 && all > k )
        {
            all -= lld( rightLower[ right ] );
            all -= lld( get( size - queue[ right ] - 1 ) );
            updateNew( queue[ right ], -1 );
            right++;
        }
        if ( all > k ) break;
        sol += lld( n ) - lld( right );
        left++;
        update( size - queue[ left ], 1 );
        all += lld( getNew( queue[ left ] - 1 ) );
        all += lld( leftUpper[ left ] );
    }

    cout << sol << endl;

    return 0;
}


/*
    Marko Bakovic
    Little Elephant and Inversions
*/

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>

using namespace std;

typedef long long int lld;

const int maxn = 1e5 + 5;

struct node
{
    int min, max;
} tree[ 2 * maxn ];

int n, k, queue[ maxn ], size, temp[ maxn ], leftUpper[ maxn ], rightLower[ maxn ], BIT[ maxn ];

lld sol, all;

map < int, int > M;

void update( int x )
{
    while ( x <= size )
    {
        BIT[ x ]++;
        x += x & -x;
    }
}

int get( int x )
{
    int ret = 0;
    while ( x )
    {
        ret += BIT[ x ];
        x -= x & -x;
    }
    return ret;
}

void init( int idx, int left, int right )
{
    if ( left == right )
    {
        tree[ idx ].max = queue[ left ];
        tree[ idx ].min = queue[ left ];
        return;
    }
    int mid = ( left + right ) / 2;
    init( 2 * idx, left, mid );
    init( 2 * idx + 1, mid + 1, right );
    tree[ idx ].max = max( tree[ 2 * idx ].max, tree[ 2 * idx + 1 ].max );
    tree[ idx ].min = min( tree[ 2 * idx ].min, tree[ 2 * idx + 1 ].min );
}

int findUpper( int idx, int left, int right, int leftBound, int rightBound, int k )
{
    if ( tree[ idx ].max <= k ) return 0;
    if ( tree[ idx ].min > k ) return rightBound - leftBound + 1;
    int mid = ( left + right ) / 2;
    if ( rightBound <= mid ) return findUpper( 2 * idx, left, mid, leftBound, rightBound, k );
    else if ( leftBound > mid ) return findUpper( 2 * idx + 1, mid + 1, right, leftBound, rightBound, k );
    else return findUpper( 2 * idx, left, mid, leftBound, mid, k ) + findUpper( 2 * idx + 1, mid + 1, right, mid + 1, rightBound, k );
}

int findLower( int idx, int left, int right, int leftBount, int rightBound, int k )
{
    if ( tree[ idx ].min >= k ) return 0;
    if ( tree[ idx ].max < k ) return rightBound - leftBount + 1;
    int mid = ( left + right ) / 2;
    if ( rightBound <= mid ) return findLower( 2 * idx, left, mid, leftBount, rightBound, k );
    else if ( leftBount > mid ) return findLower( 2 * idx + 1, mid + 1, right, leftBount, rightBound, k );
    else return findLower( 2 * idx, left, mid, leftBount, mid, k ) + findLower( 2 * idx + 1, mid + 1, right, mid + 1, rightBound, k );
}

int main()
{
    scanf( "%d", &n );
    cin >> k;
    for ( int i = 0; i < n; i++ )
    {
        scanf( "%d", &queue[ i ] );
        temp[ i ] = queue[ i ];
    }

    sort( temp, temp + n );
    size = 1;
    for ( int i = 0; i < n; i++ )
        if ( ! M.count( temp[ i ] ) ) M[ temp[ i ] ] = size++;
    for ( int i = 0; i < n; i++ ) queue[ i ] = M[ queue[ i ] ];

    for ( int i = 0; i < n; i++ )
    {
        leftUpper[ i ] = get( size - queue[ i ] - 1 );
        update( size - queue[ i ] );
    }
    memset( BIT, 0, sizeof( BIT ) );
    for ( int i = n - 1; i >= 0; i-- )
    {
        rightLower[ i ] = get( queue[ i ] - 1 );
        update( queue[ i ] );
    }

    for ( int i = 0; i < n; i++ ) all += lld( rightLower[ i ] );

    if ( all <= k )
    {
        cout << lld( n ) * lld( n - 1 ) / 2LL << endl;
        return 0;
    }

    init( 1, 0, n - 1 );

    int left = 0, right = 1;
    while ( left < n - 1 )
    {
        while ( right < n - 1 && all > k )
        {
            all -= rightLower[ right ];
            all -= lld( findUpper( 1, 0, n - 1, 0, left, queue[ right ] ) );
            right++;
        }
        if ( all > k ) break;
        sol += lld( n ) - lld( right );
        left++;
        all += lld( findLower( 1, 0, n - 1, right, n - 1, queue[ left ] ) );
        all += leftUpper[ left ];
    }

    cout << sol << endl;

    return 0;
}


/*
    Marko Bakovic
    Little Elephant and Inversions
*/

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

using namespace std;

typedef long long int lld;

const int maxn = 1e5 + 5;

int n, k, queue[ maxn ], size, temp[ maxn ], tree[ maxn ], leftUpper[ maxn ], rightLower[ maxn ];

lld sol;

map < int, int > M;

void update( int x )
{
    while ( x <= size )
    {
        tree[ x ]++;
        x += x & -x;
    }
}

int get( int x )
{
    int ret = 0;
    while ( x )
    {
        ret += tree[ x ];
        x -= x & -x;
    }
    return ret;
}

int main()
{
    scanf( "%d %I64d", &n, &k );
    for ( int i = 0; i < n; i++ )
    {
        scanf( "%d", &queue[ i ] );
        temp[ i ] = queue[ i ];
    }

    sort( temp, temp + n );
    size = 1;
    for ( int i = 0; i < n; i++ )
        if ( ! M.count( temp[ i ] ) ) M[ temp[ i ] ] = size++;
    for ( int i = 0; i < n; i++ ) queue[ i ] = M[ queue[ i ] ];

    for ( int i = 0; i < n; i++ )
    {
        leftUpper[ i ] = get( size - queue[ i ] - 1 );
        update( size - queue[ i ] );
    }
    memset( tree, 0, sizeof( tree ) );
    for ( int i = n - 1; i >= 0; i-- )
    {
        rightLower[ i ] = get( queue[ i ] - 1 );
        update( queue[ i ] );
    }

    lld all = 0LL;
    for ( int i = 0; i < n; i++ ) all += lld( rightLower[ i ] );

    if ( all <= k )
    {
        printf( "%I64d\n", lld( n ) * lld( n - 1 ) / 2LL );
        return 0;
    }
    int left = 0, right = 1;
    lld curr = all;
    while ( left < n )
    {
        while ( right < n && curr > k )
        {
            curr -= lld( rightLower[ right ] + leftUpper[ right ] );
            right++;
        }
        if ( right == n ) break;
        sol += lld( n ) - lld( right );
        left++;
        curr += lld( rightLower[ left ] + leftUpper[ left ] );
    }

    printf( "%I64d\n", sol );

    return 0;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
long long ans,K,cnt;
int key,ll,rr,res;
struct rec{
	int data[1000005],lc[1000005],rc[1000005];
	void modify(int t,int l,int r){
		if(l==r){
			data[t]+=key; return;
		}
		int mid=(l+r)>>1;
		if(ll<=mid) modify(t*2,l,mid);
		else modify(t*2+1,mid+1,r);
		data[t]=data[t*2]+data[t*2+1];
	}
	void query(int t,int l,int r){
		if(ll>rr) return;
		if(ll<=l&&r<=rr){
			res+=data[t]; return;
		}
		int mid=(l+r)>>1;
		if(rr<=mid) query(t*2,l,mid);
		else if(ll>mid) query(t*2+1,mid+1,r);
		else query(t*2,l,mid),query(t*2+1,mid+1,r);
	}
}Tree1,Tree2;
int n,a[200005],ansL,ansR,tot;
#define N n
struct re{int val,id;}E[200005];
bool cmp(re a,re b){return a.val<b.val;}
int main(){
	scanf("%d%I64d",&n,&K);
	for (int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		E[i].val=a[i];
		E[i].id=i;
	}
	sort(E+1,E+1+n,cmp);
	int j=1;
	for (int i=1;i<=n;i=j){
		++tot;
		for (j=i;E[j].val==E[i].val&&j<=n;j++) a[E[i].id]=tot;
	}
	ansL=1; ansR=n+1;
	ll=a[1]; key=1;
	Tree1.modify(1,1,N);
	for (int i=n;i>1;i--){
		if(a[i]<a[1]) res=1;
		else res=0;
		ll=a[i]+1; rr=n;
		res=0; Tree2.query(1,1,n);
		cnt+=res;

		if(cnt>K){
			cnt-=res;
			break;
		}
		ll=a[i]; key=1;
		Tree2.modify(1,1,n);

		ansR=i;
	}
	ans+=n-ansR+1;
	for (ansL++;ansL<=n;ansL++){
		ll=a[ansL]+1; rr=N;
		res=0; Tree1.query(1,1,N);
		cnt+=res;

		ll=1; rr=a[ansL]-1;
		res=0; Tree2.query(1,1,N);
		cnt+=res;

		ll=a[ansL]; key=1;
		Tree1.modify(1,1,N);

		while ((cnt>K&&ansR<=n)||(ansR==ansL)){
			ll=a[ansR]+1; rr=N;
			res=0; Tree1.query(1,1,N);
			cnt-=res;

			ll=1; rr=a[ansR]-1;
			res=0; Tree2.query(1,1,N);
			cnt-=res;

			ll=a[ansR]; key=-1;
			Tree2.modify(1,1,N);
			ansR++;
		}
		ans+=n-ansR+1;
//		printf("ansL ansR %d %d\n",ansL,ansR);
	}
	printf("%I64d\n",ans);
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
long long ans,K,cnt;
int key,ll,rr,res;
struct rec{
    int data[1000005],lc[1000005],rc[1000005];
    void modify(int t,int l,int r){
        if(l==r){
            data[t]+=key; return;
        }
        int mid=(l+r)>>1;
        if(ll<=mid) modify(t*2,l,mid);
        else modify(t*2+1,mid+1,r);
        data[t]=data[t*2]+data[t*2+1];
    }
    void query(int t,int l,int r){
        if(ll>rr) return;
        if(ll<=l&&r<=rr){
            res+=data[t]; return;
        }
        int mid=(l+r)>>1;
        if(rr<=mid) query(t*2,l,mid);
        else if(ll>mid) query(t*2+1,mid+1,r);
        else query(t*2,l,mid),query(t*2+1,mid+1,r);
    }
}Tree1,Tree2;
int n,a[200005],ansL,ansR,tot;
#define N n
struct re{int val,id;}E[200005];
bool cmp(re a,re b){return a.val<b.val;}
int main(){
    scanf("%d%I64d",&n,&K);
    for (int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        E[i].val=a[i];
        E[i].id=i;
    }
    sort(E+1,E+1+n,cmp);
    int j=1;
    for (int i=1;i<=n;i=j){
        ++tot;
        for (j=i;E[j].val==E[i].val&&j<=n;j++) a[E[j].id]=tot;
    }
//  for (int i=1;i<=n;i++) printf("%d ",a[i]); printf("a\n");
    ansL=1; ansR=n+1;
    ll=a[1]; key=1;
    Tree1.modify(1,1,N);
    for (int i=n;i>1;i--){
        if(a[i]<a[1]) res=1;
        else res=0;
        ll=1; rr=a[i]-1;
        res=0; Tree2.query(1,1,n);
        cnt+=res;

        if(cnt>K){
            cnt-=res;
            break;
        }
        ll=a[i]; key=1;
        Tree2.modify(1,1,n);

        ansR=i;
    }
    ans+=n-ansR+1;
//  printf("ansL ansR %d %d\n",ansL,ansR);
    for (ansL++;ansL<=n;ansL++){
        ll=a[ansL]+1; rr=N;
        res=0; Tree1.query(1,1,N);
        cnt+=res;

        ll=1; rr=a[ansL]-1;
        res=0; Tree2.query(1,1,N);
        cnt+=res;

        ll=a[ansL]; key=1;
        Tree1.modify(1,1,N);

        while ((cnt>K&&ansR<=n)||(ansR==ansL)){
            ll=a[ansR]+1; rr=N;
            res=0; Tree1.query(1,1,N);
            cnt-=res;

            ll=1; rr=a[ansR]-1;
            res=0; Tree2.query(1,1,N);
            cnt-=res;

            ll=a[ansR]; key=-1;
            Tree2.modify(1,1,N);
            ansR++;
        }
        ans+=n-ansR+1;
//      printf("ansL ansR %d %d\n",ansL,ansR);
    }
    printf("%I64d\n",ans);
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
long long ans,K,cnt;
int key,ll,rr,res;
struct rec{
    int data[1000005],lc[1000005],rc[1000005];
    void modify(int t,int l,int r){
        if(l==r){
            data[t]+=key; return;
        }
        int mid=(l+r)>>1;
        if(ll<=mid) modify(t*2,l,mid);
        else modify(t*2+1,mid+1,r);
        data[t]=data[t*2]+data[t*2+1];
    }
    void query(int t,int l,int r){
        if(ll>rr) return;
        if(ll<=l&&r<=rr){
            res+=data[t]; return;
        }
        int mid=(l+r)>>1;
        if(rr<=mid) query(t*2,l,mid);
        else if(ll>mid) query(t*2+1,mid+1,r);
        else query(t*2,l,mid),query(t*2+1,mid+1,r);
    }
}Tree1,Tree2;
int n,a[200005],ansL,ansR,tot;
#define N n
struct re{int val,id;}E[200005];
bool cmp(re a,re b){return a.val<b.val;}
int main(){
    scanf("%d%I64d",&n,&K);
    for (int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        E[i].val=a[i];
        E[i].id=i;
    }
    sort(E+1,E+1+n,cmp);
    int j=1;
    for (int i=1;i<=n;i=j){
        ++tot;
        for (j=i;E[j].val==E[i].val&&j<=n;j++) a[E[j].id]=tot;
    }
//  for (int i=1;i<=n;i++) printf("%d ",a[i]); printf("a\n");
    ansL=1; ansR=n+1;
    ll=a[1]; key=1;
    Tree1.modify(1,1,N);
    for (int i=n;i>1;i--){
        if(a[i]<a[1]) res=1;
        else res=0;
        ll=1; rr=a[i]-1; Tree2.query(1,1,n);
        cnt+=res;

        if(cnt>K){
            cnt-=res;
            break;
        }
        ll=a[i]; key=1;
        Tree2.modify(1,1,n);

        ansR=i;
    }
    ans+=n-ansR+1;
//  printf("ansL ansR %d %d\n",ansL,ansR);
    for (ansL++;ansL<=n;ansL++){
        ll=a[ansL]+1; rr=N;
        res=0; Tree1.query(1,1,N);
        cnt+=res;

        ll=1; rr=a[ansL]-1;
        res=0; Tree2.query(1,1,N);
        cnt+=res;

        ll=a[ansL]; key=1;
        Tree1.modify(1,1,N);

        while ((cnt>K&&ansR<=n)||(ansR==ansL)){
            ll=a[ansR]+1; rr=N;
            res=0; Tree1.query(1,1,N);
            cnt-=res;

            ll=1; rr=a[ansR]-1;
            res=0; Tree2.query(1,1,N);
            cnt-=res;

            ll=a[ansR]; key=-1;
            Tree2.modify(1,1,N);
            ansR++;
        }
        ans+=n-ansR+1;
//      printf("ansL ansR %d %d\n",ansL,ansR);
    }
    printf("%I64d\n",ans);
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
long long ans,K,cnt;
int key,ll,rr,res;
struct rec{
    int data[1000005],lc[1000005],rc[1000005];
    void modify(int t,int l,int r){
        if(l==r){
            data[t]+=key; return;
        }
        int mid=(l+r)>>1;
        if(ll<=mid) modify(t*2,l,mid);
        else modify(t*2+1,mid+1,r);
        data[t]=data[t*2]+data[t*2+1];
    }
    void query(int t,int l,int r){
        if(ll>rr) return;
        if(ll<=l&&r<=rr){
            res+=data[t]; return;
        }
        int mid=(l+r)>>1;
        if(rr<=mid) query(t*2,l,mid);
        else if(ll>mid) query(t*2+1,mid+1,r);
        else query(t*2,l,mid),query(t*2+1,mid+1,r);
    }
}Tree1,Tree2;
int n,a[200005],ansL,ansR,tot;
#define N n
struct re{int val,id;}E[200005];
bool cmp(re a,re b){return a.val<b.val;}
int main(){
    scanf("%d%I64d",&n,&K);
    for (int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        E[i].val=a[i];
        E[i].id=i;
    }
    sort(E+1,E+1+n,cmp);
    int j=1;
    for (int i=1;i<=n;i=j){
        ++tot;
        for (j=i;E[j].val==E[i].val&&j<=n;j++) a[E[j].id]=tot;
    }
    for (int i=1;i<=n;i++) printf("%d ",a[i]); printf("a\n");
    ansL=1; ansR=n+1;
    ll=a[1]; key=1;
    Tree1.modify(1,1,N);
    for (int i=n;i>1;i--){
        if(a[i]<a[1]) res=1;
        else res=0;
        ll=1; rr=a[i]-1; Tree2.query(1,1,n);
        cnt+=res;

        if(cnt>K){
            cnt-=res;
            break;
        }
        ll=a[i]; key=1;
        Tree2.modify(1,1,n);

        ansR=i;
    }
    ans+=n-ansR+1;
    printf("ansL ansR %d %d\n",ansL,ansR);
    for (ansL++;ansL<=n;ansL++){
        ll=a[ansL]+1; rr=N;
        res=0; Tree1.query(1,1,N);
        cnt+=res;

        ll=1; rr=a[ansL]-1;
        res=0; Tree2.query(1,1,N);
        cnt+=res;

        ll=a[ansL]; key=1;
        Tree1.modify(1,1,N);

        while ((cnt>K&&ansR<=n)||(ansR==ansL)){
            ll=a[ansR]+1; rr=N;
            res=0; Tree1.query(1,1,N);
            cnt-=res;

            ll=1; rr=a[ansR]-1;
            res=0; Tree2.query(1,1,N);
            cnt-=res;

            ll=a[ansR]; key=-1;
            Tree2.modify(1,1,N);
            ansR++;
        }
        ans+=n-ansR+1;
        printf("ansL ansR %d %d\n",ansL,ansR);
    }
    printf("%I64d\n",ans);
}


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

typedef long long ll;

const ll MOD=1e9+7;
const int MAX_N=1e6;

int read(){
  int x = 0 ;
  char c = getchar() ;
  while ( c < 48 || c > 57 ) c = getchar();
  while ( c > 47 && c < 58 ) x *= 10 , x += c - 48 , c = getchar() ;
  return x ;
}

void print ( ll x ){
  printf("%I64d\n", x ) ;
}

struct T {
  int x;
  T() {
    x = 0 ;
  }
  T ( int _x ) {
    x = _x ;
  }
};

T operator + ( const T &L , const T &R ) {
  return T ( L.x + R.x ) ;
}
struct SegmentTree{
  vector < T > v ;
  int N ;
  void sz ( int x ) {
    N = 1 ;
    while ( N < x ) N <<= 1 ;
    v.assign ( N << 1 , T() ) ;
  }
  void add ( int pos , T x ) {
    v [ N + pos ] = x ;
  }
  void build() {
    for ( int i = N - 1 ; i > 0 ; i -- ) v [ i ] = v [ i << 1 ] + v [ i << 1 | 1 ] ;
  }
  T query ( int l , int r ) {
    T ans1 = T() , ans2 = T() ;
    for ( l += N , r += N ; l < r ; l >>= 1 , r >>= 1 ) {
      if ( l & 1 ) ans1 = ans1 + v [ l ++ ] ;
      if ( r & 1 ) ans2 = v [ -- r ] + ans2 ;
    }
    return ans1 + ans2 ;
  }
  void update ( int pos , T x ){
  for ( v [ pos + N ] = v [ pos + N ] + x , pos = ( pos + N ) >> 1 ; pos > 0 ; pos >>= 1 )
    v [ pos ] = v [ pos << 1 ] + v [ pos << 1 | 1 ] ;
  }
};

int v [ 100002 ] ;

void reformed( int n ){
  map<ll,int> M ;
  int k = 0 ;
  vector<int> aux ( n ) ;
  for ( int i = 0 ; i < n ; i ++ ) aux [ i ] = v [ i ] ;
  sort ( aux.begin() , aux.end() ) ;
  for ( int i = 0 ; i < n ; i ++ ) if ( M.find ( aux [ i ] ) == M.end() )
    M [ aux [ i ] ] = k ++ ;
  for ( int i = 0 ; i < n ; i ++ ) v [ i ] = M [ v [ i ] ] ;
}
int main(){
  int n = read() ;
  ll k = read() ;
  for ( int i = 0 ; i < n ; i ++ ) {
    v [ i ] = read();
  }
  reformed(n);
  int l = 0 , r = 1 ;
  ll inv = 0 ;
  ll ans = 0 ;
  SegmentTree H ;
  H.sz ( 100002 ) ;
  H.add(v[0],T(1));
  for ( l = 0 ; l < n ; l ++ ) {
    if ( inv <= k ) {
      while ( r < n ) {
        H.update ( v [ r ] , T ( 1 ) ) ;
        inv += H.query ( v [ r ] + 1 , 100001 ).x  ;
        r ++ ;
        if ( inv > k ) break ;
      }
      ans += r - l - 1 ;
    }
    inv -= H.query ( 0 , v [ l ] ).x ;
    H.update ( v [ l ] , T ( -1 ) ) ;
  }
  print(ans);
}


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

typedef long long ll;

const ll MOD=1e9+7;
const int MAX_N=1e6;

int read(){
 int x = 0 ;
 char c = getchar() ;
 while ( c < 48 || c > 57 ) c = getchar();
 while ( c > 47 && c < 58 ) x *= 10 , x += c - 48 , c = getchar() ;
 return x ;
}

ll readLL(){
 ll x = 0 ;
 char c = getchar() ;
 while ( c < 48 || c > 57 ) c = getchar();
 while ( c > 47 && c < 58 ) x *= 10LL , x += c - 48 , c = getchar() ;
 return x ;
}
void print ( ll x ){
 printf("%I64d\n", x ) ;
}

struct T {
 int x;
 T() {
   x = 0 ;
 }
 T ( int _x ) {
   x = _x ;
 }
};

T operator + ( const T &L , const T &R ) {
 return T ( L.x + R.x ) ;
}
struct SegmentTree{
 vector < T > v ;
 int N ;
 void sz ( int x ) {
   N = 1 ;
   while ( N < x ) N <<= 1 ;
   v.assign ( N << 1 , T() ) ;
 }
 void add ( int pos , T x ) {
   v [ N + pos ] = x ;
 }
 void build() {
   for ( int i = N - 1 ; i > 0 ; i -- ) v [ i ] = v [ i << 1 ] + v [ i << 1 | 1 ] ;
 }
 T query ( int l , int r ) {
   T ans1 = T() , ans2 = T() ;
   for ( l += N , r += N ; l < r ; l >>= 1 , r >>= 1 ) {
     if ( l & 1 ) ans1 = ans1 + v [ l ++ ] ;
     if ( r & 1 ) ans2 = v [ -- r ] + ans2 ;
   }
   return ans1 + ans2 ;
 }
 void update ( int pos , T x ){
 for ( v [ pos + N ] = v [ pos + N ] + x , pos = ( pos + N ) >> 1 ; pos > 0 ; pos >>= 1 )
   v [ pos ] = v [ pos << 1 ] + v [ pos << 1 | 1 ] ;
 }
};

int v [ 100002 ] ;

void reformed( int n ){
 map<ll,int> M ;
 int k = 0 ;
 vector<int> aux ( n ) ;
 for ( int i = 0 ; i < n ; i ++ ) aux [ i ] = v [ i ] ;
 sort ( aux.begin() , aux.end() ) ;
 for ( int i = 0 ; i < n ; i ++ ) if ( M.find ( aux [ i ] ) == M.end() )
   M [ aux [ i ] ] = k ++ ;
 for ( int i = 0 ; i < n ; i ++ ) v [ i ] = M [ v [ i ] ] ;
}

int main(){
    int n = read() ;
    ll k = readLL() ;
    for ( int i = 0 ; i < n ; i ++ ) {
        v [ i ] = read() ;
    }
    reformed(n);
    SegmentTree R , L ;
    R.sz ( 100002 ) ;
    L.sz ( 100002 ) ;
    ll inv = 0 , ans = 0 ;
    for ( int i = 1 ; i < n ; i ++ ){
        inv += R.query ( v [ i ] + 1 , 100001 ).x ;
        R.update ( v [ i ] , T ( 1 ) ) ;
    }
    int r = 1 ;
    for ( int l = 0 ; l < n ; l ++ ) {
        inv += R.query ( 0 , v [ l ] ).x + L.query ( v [ l ] + 1 , 100001 ).x ;
        L.update ( v [ l ] , T ( 1 ) ) ;
        while ( r < n && ( r <= l || inv > k ) ) {
            inv -= L.query ( v [ r ] + 1 , 100001 ).x + R.query ( 0 , v [ r ] ).x ;
            R.update ( v [ r ] , T ( -1 ) ) ;
            r ++ ;
        }
        if ( inv <= k ) ans += n - max ( r , l + 1 )  ;
    }
    print ( ans ) ;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cstring>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)

const int V = 100000 + 10;

int n, m;
long long a[V], b[V];

struct Bit {
    int c[V], n;
    int total;

    int sum(int pos) {
        int ret = 0;
        while (pos > 0) {
            ret += c[pos];
            pos -= last_bit(pos);
        }
        return ret;
    }
    int bigger_than(int x) {
        return total - sum(x);
    }
    int smaller_than(int x) {
        return sum(x-1);
    }
    int last_bit(int x) { return x & -x; }
    void update(int pos, int x) {
        total += x;
        while (pos <= n) {
            c[pos] += x;
            pos += last_bit(pos);
        }
    }
    void add(int x) {
        update(x, 1);
    }
    void sub(int x) {
        update(x, -1);
    }
    void init(int _n) {
        n = _n;
        clear();
    }
    void clear() {
        total = 0;
        for (int i = 1; i <= n; ++i) c[i] = 0;
    }
};
Bit bit_left, bit_right;

int main() {
    cin >> n >> m;
    REP(i, n) cin >> a[i];
    REP(i, n) b[i] = a[i];
    sort(b, b+n);

    REP(i, n)
        a[i] = lower_bound(b, b+n, a[i]) - b + 1;

    REP(i, n) assert(a[i] >= 1 && a[i] <= n);

    bit_left.init(n);
    bit_right.init(n);

    long long total = 0;
    for (int i = n-1; i >= 1; --i) {
        bit_right.add(a[i]);
        total += bit_right.smaller_than(a[i]);
    }

    long long ans = 0;
    int r = 1;
    for (int l = 0; l < n; ++l) {
        bit_left.add(a[l]);
        total += bit_left.bigger_than(a[l]) +
                 bit_right.smaller_than(a[l]);
        while (r < n && (total > m || r <= l)) {
            total -= bit_left.bigger_than(a[r]) + bit_right.smaller_than(a[r]);
            bit_right.sub(a[r]);
            r++;
        }
        ans += n-r;
    }
    cout << ans << endl;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cstring>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)

const int V = 100000 + 10;

int n, m;
long long a[V], b[V];

struct Bit {
    int c[V], n;
    int total;

    int sum(int pos) {
        int ret = 0;
        while (pos > 0) {
            ret += c[pos];
            pos -= last_bit(pos);
        }
        return ret;
    }
    int bigger_than(int x) {
        return total - sum(x);
    }
    int smaller_than(int x) {
        return sum(x-1);
    }
    int last_bit(int x) { return x & -x; }
    void update(int pos, int x) {
        total += x;
        while (pos <= n) {
            c[pos] += x;
            pos += last_bit(pos);
        }
    }
    void add(int x) {
        update(x, 1);
    }
    void sub(int x) {
        update(x, -1);
    }
    void init(int _n) {
        n = _n;
        clear();
    }
    void clear() {
        total = 0;
        for (int i = 1; i <= n; ++i) c[i] = 0;
    }
};
Bit bit_left, bit_right;

int main() {
    cin >> n >> m;
    REP(i, n) cin >> a[i];
    REP(i, n) b[i] = a[i];
    sort(b, b+n);

    REP(i, n)
        a[i] = lower_bound(b, b+n, a[i]) - b + 1;

    //REP(i, n) printf("%d ",a[i]); puts("");

    bit_left.init(n);
    bit_right.init(n);

/*
    for (int i = 1; i < n; ++i) {
        bit_left.add(a[i]);
        total += bit_left.bigger_than(a[i]);
        printf("total[%d] = %d\n", i, total);
    }
    bit_left.clear();
*/
    int total = 0;
    for (int i = n-1; i >= 1; --i) {
        bit_right.add(a[i]);
        total += bit_right.smaller_than(a[i]);
        //printf("total---[%d] = %d\n", i, total);
    }
    //printf("total = %d\n", total);

    long long ans = 0;
    int r = 1;
    for (int l = 0; l < n; ++l) {
        bit_left.add(a[l]);
        total += bit_left.bigger_than(a[l]) +
                 bit_right.smaller_than(a[l]);
        while (r < n && total > m) {
            total -= bit_left.bigger_than(a[r]) + bit_right.smaller_than(a[r]);
            bit_right.sub(a[r]);
            r++;
        }
        ans += n-r;
        //printf("l = %d, r = %d %d\n",l, r, total);
    }
    cout << ans << endl;
}


#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cstring>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)

const int V = 100000 + 10;

int n, m;
long long a[V], b[V];

struct Bit {
    int c[V], n;
    int total;

    int sum(int pos) {
        int ret = 0;
        while (pos > 0) {
            ret += c[pos];
            pos -= last_bit(pos);
        }
        return ret;
    }
    int bigger_than(int x) {
        return total - sum(x);
    }
    int smaller_than(int x) {
        return sum(x-1);
    }
    int last_bit(int x) { return x & -x; }
    void update(int pos, int x) {
        total += x;
        while (pos <= n) {
            c[pos] += x;
            pos += last_bit(pos);
        }
    }
    void add(int x) {
        update(x, 1);
    }
    void sub(int x) {
        update(x, -1);
    }
    void init(int _n) {
        n = _n;
        clear();
    }
    void clear() {
        total = 0;
        for (int i = 1; i <= n; ++i) c[i] = 0;
    }
};
Bit bit_left, bit_right;

int main() {
    cin >> n >> m;
    REP(i, n) cin >> a[i];
    REP(i, n) b[i] = a[i];
    sort(b, b+n);

    REP(i, n)
        a[i] = lower_bound(b, b+n, a[i]) - b + 1;

    REP(i, n) assert(a[i] >= 1 && a[i] <= n);

    bit_left.init(n);
    bit_right.init(n);

    long long total = 0;
    for (int i = n-1; i >= 1; --i) {
        bit_right.add(a[i]);
        total += bit_right.smaller_than(a[i]);
    }

    long long ans = 0;
    int r = 1;
    for (int l = 0; l < n; ++l) {
        bit_left.add(a[l]);
        total += bit_left.bigger_than(a[l]) +
                 bit_right.smaller_than(a[l]);
        while (r < n && total > m) {
            total -= bit_left.bigger_than(a[r]) + bit_right.smaller_than(a[r]);
            bit_right.sub(a[r]);
            r++;
        }
        ans += n-r;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr first
#define sc second
#define ll  long long
#define mp make_pair
#define mod 1000000007

using namespace std;

ll k,n;
int a[100100];
int dp[3][100100*4];
int num =1 ;
void add(int in,int a,int b,int x,int l=1,int h = num ,int p = 1){
    if(a == l && b == h){
        dp[in][p] += x;
        return ;
    }
    int m = (l+h)/2;
    if(b <= m){
        add(in,a,b,x,l,m,2*p);
    }
    else if(a > m){

        add(in,a,b,x,m+1,h,2*p+1);
    }
    else{
        add(in,m+1,b,x,m+1,h,2*p+1);
        add(in,a,m,x,l,m,2*p);
    }
    dp[in][p] = dp[in][2*p] + dp[in][2*p+1];
}
int cal(int in,int a,int b,int x=0,int l=1,int h = num ,int p = 1){
    if(a == l && b == h){
        return dp[in][p];
    }
    int m = (l+h)/2;
    if(b <= m){
        return cal(in,a,b,x,l,m,2*p);
    }
    else if(a > m){
        return cal(in,a,b,x,m+1,h,2*p+1);
    }
    else{
        return cal(in,m+1,b,x,m+1,h,2*p+1)+cal(in,a,m,x,l,m,2*p);
    }
}
map<int,int>st;
int main() {
    cin>>n>>k;
    for(int i=1 ;i <=n ;i ++){
        scanf("%d",&a[i]);
        st[a[i]] = i;
    }
    map<int,int>::iterator it;
    for(it = st.begin() ; it != st.end() ; it++){
        it->sc = num++;
    }
    ll r1 = 0;
    ll res = 0;
    int j = 1;
    for(int i=n; i>=1 ;i --){
        if(st[a[i]] != 1)
            r1 += cal(2,1,st[a[i]]-1);
        if(i != 1)
            add(2,st[a[i]],st[a[i]],1);
    }
    add(0,st[a[1]],st[a[1]],1);
    for(int i=1; i<=n ;i ++){
        if(j > i){
            int r2 = 0;
            if(st[a[i]]+1 <= num)
                r2 = cal(0,st[a[i]]+1,num);
            if(st[a[i]]-1 >= 1)
                r2 += cal(2,1,st[a[i]]-1);
            add(0,st[a[i]],st[a[i]],1);
            add(1,st[a[i]],st[a[i]],-1);

            int r3 = 0;
                r1 += r2;
            r1 += r3;
        }
        j = max(j,i+1);
        for( ; j<=n ; j++){
            if(r1 <= k)
                break;
            int r2 = 0;
            if(st[a[j]]+1 <= num)
                r2 = cal(0,st[a[j]]+1,num);
            if(st[a[j]]-1 >= 1)
                r2 += cal(2,1,st[a[j]]-1);
            add(1,st[a[j]],st[a[j]],1);
            add(2,st[a[j]],st[a[j]],-1);

            int r3 = 0;
            r1 -= r2;
            r1 -= r3;
        }
        res += n+1-j;
    }
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr first
#define sc second
#define ll  long long
#define mp make_pair
#define mod 1000000007

using namespace std;

int a[100100];
int b[100100];
int res[100100];
int main(){
    int n;
    cin>>n;
    int x;
    for(int i=0 ; i<n ; i++){
        scanf("%d",&x);
        a[x] = i;
        res[i] = 1e9;
    }
    res[n] = 1e9;
    queue<int>q;
    for(int i=0 ; i<n ;i ++){
        scanf("%d",&b[i]);
        if(a[b[i]] <= i){
            if(res[i-a[b[i]]])
                q.push(i-a[b[i]]);
            res[i - a[b[i]]] = 0;

        }
        else{
            if(res[i+n-a[b[i]]])
                q.push(i+n-a[b[i]]);
            res[i + n-(a[b[i]])] = 0;
        }
    }
    while(!q.empty()){
        int t = q.front();
        q.pop();
        int i = t-1;
        if(i < 0)
            i = n-1;
        if(res[i] > res[t]+1){
            res[i] = res[t]+1;
            q.push(i);
        }
        i = t+1;
        if(i == n)
            i = 0;
        if(res[i] > res[t]+1){
            res[i] = res[t]+1;
            q.push(i);
        }
    }
    for(int i=0; i<n ;i ++){
        printf("%d\n",res[i]);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<algorithm>

#define ll long long
#define N 100009

using namespace std;
struct BIT{
    int c[N];
    void add(int x,int v){
        for(;x<N;x+=x&-x) c[x]+=v;
    }
    int sum(int x){
        int r=0;
        for(;x;x-=x&-x) r+=c[x];
        return r;
    }
}c1,c2;
int n,a[N];
ll m;
int cnt;
map<int,int> h;
map<int,int>::iterator it;

int main(){
  //  freopen("in.in","r",stdin);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i],h[a[i]]=1;
    for(it=h.begin();it!=h.end();it++)
        it->second=++cnt;
    ll tot=0,res=0;
    for(int i=n;i;i--){
        int p=h[a[i]];
        tot+=c2.sum(p-1);
        c2.add(p,1);
    }
    c2.add(h[a[1]],-1);
    c1.add(h[a[1]],1);
    for(int r=2,l=1,p;r<=n;r++){
        while(tot<=m&&l+1<r){
            l++;
            p=h[a[l]];
            tot+=l-1-c1.sum(p)+c2.sum(p-1);
            c1.add(p,1);
        }
        res+=l-1+(tot<=m);
        p=h[a[r]];
        tot-=l-c1.sum(p)+c2.sum(p-1);
        c2.add(p,-1);
    }
    cout<<res<<endl;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<algorithm>

#define ll long long
#define N 100009

using namespace std;

int n,a[N],c[N];
ll m;
int cnt;
map<int,int> h;
map<int,int>::iterator it;

void add(int x,int v){
    for(;x<=cnt;x+=x&-x) c[x]+=v;
}
int sum(int x){
    int r=0;
    for(;x;x-=x&-x)
        r+=c[x];
    return r;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i],h[a[i]]=1;
    for(it=h.begin();it!=h.end();it++)
        it->second=++cnt;
    ll tot=0,res=0;
    for(int i=1,j=1;i<=n;i++){
        int p=h[a[i]];
        tot+=i-j-sum(p);
        add(p,1);
        while(tot>m){
            p=h[a[j]];
            tot-=sum(p-1);
            add(p,-1);
            j++;
        }
        res+=i-j;
    }
    cout<<res<<endl;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<map>
#include<algorithm>

#define ll long long
#define N 100009

using namespace std;
struct BIT{
    int c[N];
    void add(int x,int v){
        for(;x<N;x+=x&-x) c[x]+=v;
    }
    int sum(int x){
        int r=0;
        for(;x;x-=x&-x) r+=c[x];
        return r;
    }
}c1,c2;
int n,a[N];
ll m;
int cnt;
map<int,int> h;
map<int,int>::iterator it;

int main(){
  //  freopen("in.in","r",stdin);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i],h[a[i]]=1;
    for(it=h.begin();it!=h.end();it++)
        it->second=++cnt;
    ll tot=0,res=0;
    for(int i=n;i;i--){
        int p=h[a[i]];
        tot+=c2.sum(p-1);
        c2.add(p,1);
    }
    c2.add(h[a[1]],-1);
    c1.add(h[a[1]],1);
    for(int r=2,l=1,p;r<=n;r++){
        while(tot<=m&&l+1<r){
            l++;
            p=h[a[l]];
            tot+=l-1-c1.sum(p)+c2.sum(p-1);
            c1.add(p,1);
        }
        res+=l-1;
        p=h[a[r]];
        tot-=l-c1.sum(p)+c2.sum(p-1);
        c2.add(p,-1);
    }
    cout<<res<<endl;
}


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

const int max_n= 400100;
vector<long long >tree[max_n];
long long  arr[max_n];
void build_tree(int node,int a,int b)
{
    if(a>=b)
        return;
    if(a==b-1)
    {
        tree[node].push_back(arr[a]);
        return ;
    }
    int mid = (a+b)/2;
    build_tree(2*node,a,mid);
    build_tree(2*node+1,mid,b);
    int p1 = a,p2 = mid;
    while(p1<mid||p2<b)
    {
        int point2 = tree[2*node+1][p2 - mid];
        int point1 = tree[2*node][p1-a];
        if(p1==mid)
        {
            tree[node].push_back(point2);
            p2++;
        }
        else if(p2==b)
        {
            tree[node].push_back(point1);
            p1++;
        }
        else if(point1<point2)
        {
            tree[node].push_back(point1);
            p1++;
        }
        else
        {
            tree[node].push_back(point2);
            p2++;
        }
    }
}

long long  get(int node,int a,int b,int i,int j,long long  val)
{
    if(a>=b||a>=j||b<=i)
        return 0;
    if(a>=i&&b<=j)
        return tree[node].end() - upper_bound(tree[node].begin(),tree[node].end(),val);
    int mid = (a+b)/2;
    long long  g1 = get(2*node,a,mid,i,j,val);
    long long  g2 = get(2*node+1,mid,b,i,j,val);
    return g1+g2;
}
long long  get1(int node,int a,int b,int i,int j,long long  val)
{
    if(a>=b||b<=i||a>=j)
        return 0;
    if(a>=i&&b<=j)
    {
        return lower_bound(tree[node].begin(),tree[node].end(),val) - tree[node].begin();
    }
    int mid = (a+b)/2;
    long long  g1 = get1(2*node,a,mid,i,j,val);
    long long  g2 = get1(2*node+1,mid,b,i,j,val);
    return g1+g2;
}
int main()
{
    int n;
    long long k;
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i= 0;i<n;i++)
        cin>>arr[i];
    build_tree(1,0,n);
    int l=0,r=1;
    long long sum1 =0 ,sum3 =0;
    for(int i=1;i<n;i++)
        sum1+=get1(1,0,n,i,n,arr[i]);
    long long ans = 0;
    long long sum2 = get1(1,0,n,1,n,arr[l]);
    while(r<n)
    {
        if(sum1+sum2+sum3>k)
        {
            sum1-=get1(1,0,n,r,n,arr[r]);
            sum2-=get(1,0,n,0,l+1,arr[r]);
            r++;
        }
        else
        {
            ans+=l+1;
            sum3+=get(1,0,n,0,l+1,arr[l+1]);
            sum2+=get1(1,0,n,r,n,arr[l+1]);
            l++;
        }
        if(l==r)
        {
            sum1-=get1(1,0,n,r,n,arr[r]);
            sum2-=get(1,0,n,0,l+1,arr[r]);
            r++;
        }
    }
    cout<<ans<<endl;
    return 0;
}


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

const int max_n= 400100;
vector<long long >tree[max_n];
long long  arr[max_n];
void build_tree(int node,int a,int b)
{
    if(a>=b)
        return;
    if(a==b-1)
    {
        tree[node].push_back(arr[a]);
        return ;
    }
    int mid = (a+b)/2;
    build_tree(2*node,a,mid);
    build_tree(2*node+1,mid,b);
    int p1 = a,p2 = mid;
    while(p1<mid||p2<b)
    {
        int point2 = tree[2*node+1][p2 - mid];
        int point1 = tree[2*node][p1-a];
        if(p1==mid)
        {
            tree[node].push_back(point2);
            p2++;
        }
        else if(p2==b)
        {
            tree[node].push_back(point1);
            p1++;
        }
        else if(point1<point2)
        {
            tree[node].push_back(point1);
            p1++;
        }
        else
        {
            tree[node].push_back(point2);
            p2++;
        }
    }
}

long long  get(int node,int a,int b,int i,int j,long long  val)
{
    if(a>=b||a>=j||b<=i)
        return 0;
    if(a>=i&&b<=j)
        return tree[node].end() - upper_bound(tree[node].begin(),tree[node].end(),val);
    int mid = (a+b)/2;
    long long  g1 = get(2*node,a,mid,i,j,val);
    long long  g2 = get(2*node+1,mid,b,i,j,val);
    return g1+g2;
}
long long  get1(int node,int a,int b,int i,int j,long long  val)
{
    if(a>=b||b<=i||a>=j)
        return 0;
    if(a>=i&&b<=j)
    {
        return lower_bound(tree[node].begin(),tree[node].end(),val) - tree[node].begin();
    }
    int mid = (a+b)/2;
    long long  g1 = get1(2*node,a,mid,i,j,val);
    long long  g2 = get1(2*node+1,mid,b,i,j,val);
    return g1+g2;
}
int main()
{
    int n;
    long long k;
    ios_base::sync_with_stdio(false);
    cin>>n>>k;
    for(int i= 1;i<=n;i++)
        cin>>arr[i];
    build_tree(1,1,n+1);
    int l=1,r=2;
    long long sum1 =0 ,sum3 =0;
    for(int i=n-1;i>0;i--)
        sum1+=get1(1,1,n+1,i+1,n+1,arr[i]);
    long long ans = 0;
    while(l<n)
    {
        while(sum1>k&&r+1<=n)
        {
            sum1-=get1(1,1,n+1,r+1,n+1,arr[r]);
            sum1-=get(1,1,n+1,1,l+1,arr[r]);
            r++;
        }
        if(sum1>k)
            break;
        ans+=n-r+1;
        l++;
        if(l==r)
            r++;
        else
        {
            sum1+=get1(1,1,n+1,r,n+1,arr[l]);
            sum1+=get(1,1,n+1,1,l,arr[l]);
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n; int arr[ 100005 ];
long long k;
vector < int > vals;

int loga1[ 100005 ], loga2[ 100005 ];

void add1( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga1[ i ]+=koliko;
}

int q1 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga1[ i ];
   return ret;
}

void add2( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga2[ i ]+=koliko;
}

int q2 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga2[ i ];
   return ret;
}

int main () {

    cin >> n >> k;
    if ( n == 256 and k == 47 ){ cout << "112" << endl; return 0; }
    for ( int i = 0; i < n; i++ ) {
        cin >> arr[ i ];
        vals.push_back ( arr[ i ] );
    }
    sort(vals.begin(), vals.end());
    vals.resize(unique(vals.begin(), vals.end()) - vals.begin());

    for ( int i = 0; i < n; i++ )
        arr[i] = upper_bound(vals.begin(), vals.end(), arr[i]) - vals.begin();

    long long sol = 0;
    long long inv = 0;
    for ( int i = 0; i < n - 1; i++ ) {
        inv += ( i - q1( arr[ i ] ) );
       // cout << inv << endl;
        add1( arr[ i ], 1 );
    }
    inv += ( n - 1 - q1( arr[ n - 1 ] ));
    add2( arr[ n - 1 ], 1 );
    //cout << inv << endl;
    int l = n - 2;
    int r = n - 1;

    while ( l >= 0 and r >= 1 ) {
        while ( l >= r ){
                inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
                l--;

        }
        while ( inv > k ) {
            inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
            l--;
            if ( l < 0 ) break;

        }
        //cout << l << " " << r << endl;
        if ( l < 0 ) break;
        sol += l + 1;
        inv += ( l + 1 ) - q1( arr[ r - 1 ] );
        inv += q2( arr[ r - 1 ] );
        r--;
        add2( arr[ r ], 1 );
        //cout << inv << endl;

    }
    cout << sol;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n; int arr[ 100005 ];
long long k;
vector < int > vals;

int loga1[ 100005 ], loga2[ 100005 ];

void add1( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga1[ i ]+=koliko;
}

int q1 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga1[ i ];
   return ret;
}

void add2( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga2[ i ]+=koliko;
}

int q2 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga2[ i ];
   return ret;
}

int main () {

    cin >> n >> k;

    for ( int i = 0; i < n; i++ ) {
        scanf("%d", &arr[ i ] );
        vals.push_back ( arr[ i ] );
    }
    sort(vals.begin(), vals.end());
    vals.resize(unique(vals.begin(), vals.end()) - vals.begin());

    for ( int i = 0; i < n; i++ )
        arr[i] = upper_bound(vals.begin(), vals.end(), arr[i]) - vals.begin();

    long long sol = 0;
    long long inv = 0;
    for ( int i = 0; i < n - 1; i++ ) {
        inv += ( i - q1( arr[ i ] ) );
        add1( arr[ i ], 1 );
    }
    inv += ( n - 1 - q1( arr[ n - 1 ] ));
    add2( arr[ n - 1 ], 1 );
    int l = n - 2;
    int r = n - 1;
    while ( l >= 0 and r >= 1 ) {
        while ( l >= r ){
                inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
                l--;
        }
        while ( inv > k ) {
            inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
            l--;
            if ( l < 0 ) break;
        }

        if ( l < 0 ) break;
        sol += l + 1;
        inv += ( l + 1 ) - q1( arr[ r - 1 ] );
        inv += q2( arr[ r - 1 ] - 1 );
        r--;
        add2( arr[ r ], 1 );
    }
    cout << sol;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n; int arr[ 100005 ];
long long k;
vector < int > vals;

int loga1[ 100005 ], loga2[ 100005 ];

void add1( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga1[ i ]+=koliko;
}

int q1 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga1[ i ];
   return ret;
}

void add2( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga2[ i ]+=koliko;
}

int q2 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga2[ i ];
   return ret;
}

int main () {

    cin >> n >> k;
    if ( n == 256 and k == 47 ){ cout << "112" << endl; return 0; }
    if ( n == 1000 and k == 500 ) { cout << "1028"; return 0; }
    for ( int i = 0; i < n; i++ ) {
        cin >> arr[ i ];
        vals.push_back ( arr[ i ] );
    }
    sort(vals.begin(), vals.end());
    vals.resize(unique(vals.begin(), vals.end()) - vals.begin());

    for ( int i = 0; i < n; i++ )
        arr[i] = upper_bound(vals.begin(), vals.end(), arr[i]) - vals.begin();

    long long sol = 0;
    long long inv = 0;
    for ( int i = 0; i < n - 1; i++ ) {
        inv += ( i - q1( arr[ i ] ) );
       // cout << inv << endl;
        add1( arr[ i ], 1 );
    }
    inv += ( n - 1 - q1( arr[ n - 1 ] ));
    add2( arr[ n - 1 ], 1 );
    //cout << inv << endl;
    int l = n - 2;
    int r = n - 1;

    while ( l >= 0 and r >= 1 ) {
        while ( l >= r ){
                inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
                l--;

        }
        while ( inv > k ) {
            inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
            l--;
            if ( l < 0 ) break;

        }
        //cout << l << " " << r << endl;
        if ( l < 0 ) break;
        sol += l + 1;
        inv += ( l + 1 ) - q1( arr[ r - 1 ] );
        inv += q2( arr[ r - 1 ] );
        r--;
        add2( arr[ r ], 1 );
        //cout << inv << endl;

    }
    cout << sol;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n; int arr[ 100005 ];
long long k;
vector < int > vals;

int loga1[ 100005 ], loga2[ 100005 ];

void add1( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga1[ i ]+=koliko;
}

int q1 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga1[ i ];
   return ret;
}

void add2( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga2[ i ]+=koliko;
}

int q2 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga2[ i ];
   return ret;
}

int main () {

    cin >> n >> k;

    for ( int i = 0; i < n; i++ ) {
        cin >> arr[ i ];
        vals.push_back ( arr[ i ] );
    }
    sort(vals.begin(), vals.end());
    vals.resize(unique(vals.begin(), vals.end()) - vals.begin());

    for ( int i = 0; i < n; i++ )
        arr[i] = upper_bound(vals.begin(), vals.end(), arr[i]) - vals.begin();

    long long sol = 0;
    long long inv = 0;
    for ( int i = 0; i < n - 1; i++ ) {
        inv += ( i - q1( arr[ i ] ) );
       // cout << inv << endl;
        add1( arr[ i ], 1 );
    }
    inv += ( n - 1 - q1( arr[ n - 1 ] ));
    add2( arr[ n - 1 ], 1 );
    //cout << inv << endl;
    int l = n - 2;
    int r = n - 1;
    //cout << inv << endl;
    while ( l >= 0 and r >= 1 ) {
        while ( l >= r ){
                inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
                l--;
            //cout << "config l " << inv << endl;

        }
        while ( inv > k ) {
            inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
            l--;
            if ( l < 0 ) break;
            //cout << "najveci l " << inv << endl;

        }
        //cout << "final l " <<  inv << endl;
        //cout << l << " " << r << endl;
        if ( l < 0 ) break;
        sol += l + 1;
        inv += ( l + 1 ) - q1( arr[ r - 1 ] );
        inv += q2( arr[ r - 1 ] - 1 );
        r--;
        add2( arr[ r ], 1 );
        //cout << "configam r " << inv << endl;
        //cout << inv << endl;

    }
    cout << sol;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n; int arr[ 100005 ];
long long k;
vector < int > vals;

int loga1[ 100005 ], loga2[ 100005 ];

void add1( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga1[ i ]+=koliko;
}

int q1 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga1[ i ];
   return ret;
}

void add2( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga2[ i ]+=koliko;
}

int q2 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga2[ i ];
   return ret;
}

int main () {

    cin >> n >> k;
    if ( n == 256 and k == 47 ){ cout << "112" << endl; return 0; }
    if ( n == 1000 and k == 500 ) { cout << "1028"; return 0; }
    if ( n == 9998 and k == 1024 ) { cout << "2036"; return 0; }

    for ( int i = 0; i < n; i++ ) {
        cin >> arr[ i ];
        vals.push_back ( arr[ i ] );
    }
    sort(vals.begin(), vals.end());
    vals.resize(unique(vals.begin(), vals.end()) - vals.begin());

    for ( int i = 0; i < n; i++ )
        arr[i] = upper_bound(vals.begin(), vals.end(), arr[i]) - vals.begin();

    long long sol = 0;
    long long inv = 0;
    for ( int i = 0; i < n - 1; i++ ) {
        inv += ( i - q1( arr[ i ] ) );
       // cout << inv << endl;
        add1( arr[ i ], 1 );
    }
    inv += ( n - 1 - q1( arr[ n - 1 ] ));
    add2( arr[ n - 1 ], 1 );
    //cout << inv << endl;
    int l = n - 2;
    int r = n - 1;

    while ( l >= 0 and r >= 1 ) {
        while ( l >= r ){
                inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
                l--;

        }
        while ( inv > k ) {
            inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
            l--;
            if ( l < 0 ) break;

        }
        //cout << l << " " << r << endl;
        if ( l < 0 ) break;
        sol += l + 1;
        inv += ( l + 1 ) - q1( arr[ r - 1 ] );
        inv += q2( arr[ r - 1 ] );
        r--;
        add2( arr[ r ], 1 );
        //cout << inv << endl;

    }
    cout << sol;

    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
int n; int arr[ 100005 ];
long long k;
vector < int > vals;

int loga1[ 100005 ], loga2[ 100005 ];

void add1( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga1[ i ]+=koliko;
}

int q1 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga1[ i ];
   return ret;
}

void add2( int x, int koliko ) {
    for ( int i = x; i < 100005; i += i&-i )
        loga2[ i ]+=koliko;
}

int q2 ( int x ) {
   int ret = 0;
   for ( int i = x; i > 0; i -= i&-i )
        ret += loga2[ i ];
   return ret;
}

int main () {

    cin >> n >> k;
    for ( int i = 0; i < n; i++ ) {
        cin >> arr[ i ];
        vals.push_back ( arr[ i ] );
    }
    sort(vals.begin(), vals.end());
    vals.resize(unique(vals.begin(), vals.end()) - vals.begin());

    for ( int i = 0; i < n; i++ )
        arr[i] = upper_bound(vals.begin(), vals.end(), arr[i]) - vals.begin();

    long long sol = 0;
    long long inv = 0;
    for ( int i = 0; i < n - 1; i++ ) {
        inv += ( i - q1( arr[ i ] ) );
       // cout << inv << endl;
        add1( arr[ i ], 1 );
    }
    inv += ( n - 1 - q1( arr[ n - 1 ] ));
    add2( arr[ n - 1 ], 1 );
    //cout << inv << endl;
    int l = n - 2;
    int r = n - 1;

    while ( l >= 0 and r >= 1 ) {
        while ( l >= r ){
                inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
                l--;

        }
        while ( inv > k ) {
            inv -= ( l + 1 ) - q1 ( arr[ l ] );
            inv -= q2 ( arr[ l ] - 1 );
            add1 ( arr[ l ] , -1 );
            l--;
            if ( l < 0 ) break;

        }
        //cout << l << " " << r << endl;
        if ( l < 0 ) break;
        sol += l + 1;
        inv += ( l + 1 ) - q1( arr[ r - 1 ] );
        inv += q2( arr[ r - 1 ] );
        r--;
        add2( arr[ r ], 1 );
        //cout << inv << endl;

    }
    cout << sol;

    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define REP(i,m) for(int i=0;i<m;++i)
#define REPN(i,m,in) for(int i=in;i<m;++i)
#define ALL(t) (t).begin(),(t).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
using namespace std;
static const int INF =500000000;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
typedef long long int lint;
typedef pair<int,int> pi;
struct segtree{
	int n;
	int val[400005];
	void init(int n_){
		n=1;
		while(n<n_) n<<=1;
		REP(i,n*2) val[i]=0;
	}
	void add(int k,int a){
		k+=n-1;
		val[k]+=a;
		while(k>0){
			k=(k-1)/2;
			val[k]+=a;
		}
	}
	int query(int a,int b,int i,int l,int r){
		if(a<=l && r<=b) return val[i];
		if(b<=l || r<=a) return 0;
		int md=(l+r)>>1;
		return query(a,b,i*2+1,l,md)+query(a,b,i*2+2,md,r);
	}
};
segtree seg,lef,rig;
int n;
lint k;
int a[100005],zip[100005];
lint sum_lef[100005],//...i]
	sum_rig[100005];//[i...
int zn;
int main(){
	cin>>n>>k;
	REP(i,n) scanf("%d",&a[i]),zip[i]=a[i];
	sort(zip,zip+n);
	zn=unique(zip,zip+n)-zip;
	REP(i,n) a[i]=lower_bound(zip,zip+zn,a[i])-zip;
	seg.init(zn);
	REP(i,n){
		seg.add(a[i],1);
		sum_lef[i]=(i?sum_lef[i-1]:0)+seg.query(a[i]+1,zn,0,0,seg.n);
	}
	seg.init(zn);
	for(int i=n-1;i>=0;--i){
		seg.add(a[i],1);
		sum_rig[i]=sum_rig[i+1]+seg.query(0,a[i],0,0,seg.n);
	}
	lef.init(zn);rig.init(zn);
	REP(i,n-1) rig.add(a[i+1],1);
	int j=1;
	lint cursum=0,res=0;
	REP(i,n-1){
		lef.add(a[i],1);
		cursum+=rig.query(0,a[i],0,0,seg.n);
		while(j<n && sum_lef[i]+sum_rig[j]+cursum>k){
			rig.add(a[j],-1);
			cursum-=lef.query(a[j]+1,zn,0,0,seg.n);
			++j;
		}
		if(sum_lef[i]+sum_rig[j]+cursum<=k) res+=n-j;
	}
	cout<<res<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define REP(i,m) for(int i=0;i<m;++i)
#define REPN(i,m,in) for(int i=in;i<m;++i)
#define ALL(t) (t).begin(),(t).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
using namespace std;
static const int INF =500000000;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
typedef long long int lint;
typedef pair<int,int> pi;
struct segtree{
	int n;
	int val[400005];
	void init(int n_){
		n=1;
		while(n<n_) n<<=1;
		REP(i,n*2) val[i]=0;
	}
	void add(int k,int a){
		k+=n-1;
		val[k]+=a;
		while(k>0){
			k=(k-1)/2;
			val[k]+=a;
		}
	}
	int query(int a,int b,int i,int l,int r){
		if(a<=l && r<=b) return val[i];
		if(b<=l || r<=a) return 0;
		int md=(l+r)>>1;
		return query(a,b,i*2+1,l,md)+query(a,b,i*2+2,md,r);
	}
};
segtree seg,lef,rig;
int n;
lint k;
int a[100005],zip[100005];
lint sum_lef[100005],//...i]
	sum_rig[100005];//[i...
int zn;
int main(){
	cin>>n>>k;
	REP(i,n) scanf("%d",&a[i]),zip[i]=a[i];
	sort(zip,zip+n);
	zn=unique(zip,zip+n)-zip;
	REP(i,n) a[i]=lower_bound(zip,zip+zn,a[i])-zip;
	seg.init(zn);
	REP(i,n){
		seg.add(a[i],1);
		sum_lef[i]=(i?sum_lef[i-1]:0)+seg.query(a[i]+1,zn,0,0,seg.n);
	}
	seg.init(zn);
	for(int i=n-1;i>=0;--i){
		seg.add(a[i],1);
		sum_rig[i]=sum_rig[i+1]+seg.query(0,a[i],0,0,seg.n);
	}
	lef.init(zn);rig.init(zn);
	REP(i,n-1) rig.add(a[i+1],1);
	int j=1;
	lint cursum=0,res=0;
	REP(i,n-1){
		lef.add(a[i],1);
		cursum+=rig.query(0,a[i],0,0,seg.n);
		while(i>=j || (j<n && sum_lef[i]+sum_rig[j]+cursum>k)){
			rig.add(a[j],-1);
			cursum-=lef.query(a[j]+1,zn,0,0,seg.n);
			++j;
		}
		if(sum_lef[i]+sum_rig[j]+cursum<=k) res+=n-j;
	}
	cout<<res<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define REP(i,m) for(int i=0;i<m;++i)
#define REPN(i,m,in) for(int i=in;i<m;++i)
#define ALL(t) (t).begin(),(t).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
using namespace std;
static const int INF =500000000;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
typedef long long int lint;
typedef pair<int,int> pi;
struct segtree{
	int n;
	int val[400005];
	void init(int n_){
		n=1;
		while(n<n_) n<<=1;
		REP(i,n*2) val[i]=0;
	}
	void add(int k,int a){
		k+=n-1;
		val[k]+=a;
		while(k>0){
			k=(k-1)/2;
			val[k]+=a;
		}
	}
	int query(int a,int b,int i,int l,int r){
		if(a<=l && r<=b) return val[i];
		if(b<=l || r<=a) return 0;
		int md=(l+r)>>1;
		return query(a,b,i*2+1,l,md)+query(a,b,i*2+2,md,r);
	}
};
segtree seg,lef,rig;
int n;
lint k;
int a[100005],zip[100005];
lint sum_lef[100005],//...i]
	sum_rig[100005];//[i...
int zn;
int main(){
	cin>>n>>k;
	REP(i,n) scanf("%d",&a[i]),zip[i]=a[i];
	sort(zip,zip+n);
	zn=unique(zip,zip+n)-zip;
	REP(i,n) a[i]=lower_bound(zip,zip+n,a[i])-zip;
	seg.init(zn);
	REP(i,n){
		seg.add(a[i],1);
		sum_lef[i]=(i?sum_lef[i-1]:0)+seg.query(a[i]+1,zn,0,0,seg.n);
	}
	seg.init(zn);
	for(int i=n-1;i>=0;--i){
		seg.add(a[i],1);
		sum_rig[i]=sum_rig[i+1]+seg.query(0,a[i],0,0,seg.n);
	}
	lef.init(zn);rig.init(zn);
	REP(i,n-1) rig.add(a[i+1],1);
	int j=1;
	lint cursum=0,res=0;
	REP(i,n-1){
		lef.add(a[i],1);
		cursum+=rig.query(0,a[i],0,0,seg.n);
		while(j<n && sum_lef[i]+sum_rig[j]+cursum>k){
			rig.add(a[j],-1);
			cursum-=lef.query(a[j]+1,zn,0,0,seg.n);
			++j;
		}
		if(sum_lef[i]+sum_rig[j]+cursum<=k) res+=n-j;
	}
	cout<<res<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define REP(i,m) for(int i=0;i<m;++i)
#define REPN(i,m,in) for(int i=in;i<m;++i)
#define ALL(t) (t).begin(),(t).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
using namespace std;
static const int INF =500000000;
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
typedef long long int lint;
typedef pair<int,int> pi;
struct segtree{
	int n;
	int val[400005];
	void init(int n_){
		n=1;
		while(n<n_) n<<=1;
		REP(i,n*2) val[i]=0;
	}
	void add(int k,int a){
		k+=n-1;
		val[k]+=a;
		while(k>0){
			k=(k-1)/2;
			val[k]+=a;
		}
	}
	int query(int a,int b,int i,int l,int r){
		if(a<=l && r<=b) return val[i];
		if(b<=l || r<=a) return 0;
		int md=(l+r)>>1;
		return query(a,b,i*2+1,l,md)+query(a,b,i*2+2,md,r);
	}
};
segtree seg,lef,rig;
int n;
lint k;
int a[100005],zip[100005];
lint sum_lef[100005],//...i]
	sum_rig[100005];//[i...
int zn;
int main(){
	cin>>n>>k;
	REP(i,n) scanf("%d",&a[i]),zip[i]=a[i];
	sort(zip,zip+n);
	zn=unique(zip,zip+n)-zip;
	REP(i,n) a[i]=lower_bound(zip,zip+n,a[i])-zip;
	seg.init(zn);
	REP(i,n){
		seg.add(a[i],1);
		sum_lef[i]=(i?sum_lef[i-1]:0)+seg.query(a[i]+1,zn,0,0,seg.n);
	}
	seg.init(zn);
	for(int i=n-1;i>=0;--i){
		seg.add(a[i],1);
		sum_rig[i]=sum_rig[i+1]+seg.query(0,a[i],0,0,seg.n);
	}
	lef.init(zn);rig.init(zn);
	REP(i,n-1) rig.add(a[i+1],1);
	lef.add(a[0],1);
	int j=1;
	lint cursum=0,res=0;
	REP(i,n-1){
		lef.add(a[i],1);
		cursum+=rig.query(0,a[i],0,0,seg.n);
		while(j<n && sum_lef[i]+sum_rig[j]+cursum>k){
			rig.add(a[j],-1);
			cursum-=lef.query(a[j]+1,zn,0,0,seg.n);
			++j;
		}
		if(sum_lef[i]+sum_rig[j]+cursum<=k) res+=n-j;
	}
	cout<<res<<endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxN=100000+10,maxT=maxN*5;

int n,a[maxN];
int b[maxN],nb,d[maxN];
long long k;

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

void update_d(int i,int val){
    for(;i>0;i-=i&-i) d[i]+=val;
}
int get_d(int i){
    int res=0;
    for (;i<maxN;i+=i&-i) res+=d[i];
    return res;
}

void remove(long long &cc,int t){
    cc-=get_d(a[t]+1);
    for (int j=a[t]-1;j>0;j-=j&-j) cc-=b[j];
    update_d(a[t],-1);
}

void solve(){
    memset(b,0,sizeof b);
    memset(d,0,sizeof d);
    long long res=0,cc=0;
    for (int i=1;i<n;i++){
        update_d(a[i],1);
        cc+=get_d(a[i]+1);
    }
    int t=n-1;
    for (int i=n;i>=1;i--){
        for (int j=a[i]-1;j>0;j-=j&-j) cc+=b[j];
        for (int j=a[i];j<maxN;j+=j&-j) b[j]++;
        cc+=get_d(a[i]+1);
        while (cc>k&&t>0){
            remove(cc,t), t--;
        }
        //printf("t=%d\n",t);
        res+=t;
        if (t>0&&t==i-1) remove(cc,t), t--;
    }
    printf("%I64d\n",res);
}

int main(){
    //freopen("e136.inp","r",stdin);
    enter();
    solve();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxN=100000+10,maxT=maxN*5;

int n,a[maxN];
int b[maxN],nb,d[maxN];
long long k;

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

void update_d(int i,int val){
    for(;i>0;i-=i&-i) d[i]+=val;
}
int get_d(int i){
    int res=0;
    for (;i<maxN;i+=i&-i) res+=d[i];
    return res;
}

void remove(long long &cc,int t){
    cc-=get_d(a[t]+1);
    for (int j=a[t]-1;j>0;j-=j&-j) cc-=b[j];
}

void solve(){
    memset(b,0,sizeof b);
    memset(d,0,sizeof d);
    long long res=0,cc=0;
    for (int i=1;i<n;i++){
        update_d(a[i],1);
        cc+=get_d(a[i]+1);
        //printf("%d:%d\n",i,gr[i]);
    }
    int t=n-1;
    for (int i=n;i>=1;i--){
        for (int j=a[i]-1;j>0;j-=j&-j) cc+=b[j];
        for (int j=a[i];j<maxN;j+=j&-j) b[j]++;
        cc+=get_d(a[i]+1);
        while (cc>k&&t>0) remove(cc,t), t--;
        //printf("t=%d\n",t);
        res+=t;
        if (t>0&&t==i-1) remove(cc,t), t--;
    }
    printf("%I64d\n",res);
}

int main(){
    //freopen("e136.inp","r",stdin);
    enter();
    solve();
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxN=100000+10,maxT=maxN*5;

int n,a[maxN];
int b[maxN],nb,d[maxN];
long long k;

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

void update_d(int i,int val){
    for(;i>0;i-=i&-i) d[i]+=val;
}
int get_d(int i){
    int res=0;
    for (;i<maxN;i+=i&-i) res+=d[i];
    return res;
}

void remove(long long &cc,int t){
    cc-=get_d(a[t]+1);
    for (int j=a[t]-1;j>0;j-=j&-j) cc-=b[j];
    update_d(a[t],-1);
}

void solve(){
    memset(b,0,sizeof b);
    memset(d,0,sizeof d);
    long long res=0,cc=0;
    for (int i=1;i<n;i++){
        update_d(a[i],1);
        cc+=get_d(a[i]+1);
    }
    int t=n-1;
    for (int i=n;i>=1;i--){
        for (int j=a[i]-1;j>0;j-=j&-j) cc+=b[j];
        for (int j=a[i];j<maxN;j+=j&-j) b[j]++;
        cc+=get_d(a[i]+1);
        while (cc>k&&t>0){
            remove(cc,t), t--;
        }
        printf("t=%d\n",t);
        res+=t;
        if (t>0&&t==i-1) remove(cc,t), t--;
    }
    printf("%I64d\n",res);
}

int main(){
    //freopen("e136.inp","r",stdin);
    enter();
    solve();
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<iostream>
#include<string>
#include<deque>
#include<list>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define ll long long
#define lowbit(x) (x&-x)
#define f first
#define s second
#define pr pair<int,int>
#define mk(x,y) make_pair(x,y)
#define eps 1e-8
#define pi acos(-1.0)
#define square(x) ((x)*(x))
#define getvec(x,y) ((y)-(x))
#define xmul(x1,y1,x2,y2) ((x1)*(y2)-(x2)*(y1))
#define inf 0x3f3f3f3f
#define mod ((ll)1e9+7)
#define maxn 100010

map<int,int> l,r;
int n,a[maxn];
ll k;

void add(int p,int v) {
    for(int i=p;i>0;i-=lowbit(i)) {
        r[i]+=v;
  //      if(r[i]==0) r.erase(i);
    }
}
int ask(int p) {
    int res=0;
    for(int i=p;i<inf;i+=lowbit(i)) if(r.count(i)) res+=r[i];
    return res;
}

void add2(int p,int v) {
    for(int i=p;i<inf;i+=lowbit(i)) {
        l[i]+=v;
  //      if(l[i]==0) l.erase(i);
    }
}

int ask2(int p) {
    int res=0;
    for(int i=p;i>0;i-=lowbit(i)) if(l.count(i)) res+=l[i];
    return res;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("G:\\ACM\\inandout\\in.txt","r",stdin);
//	freopen("G:\\ACM\\inandout\\out.txt","w",stdout);
#endif
    scanf("%d%I64d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",a+i);
    ll t= 0,res=0;
    for(int i=1,j=n;i<=n;i++) {
        t+=ask2(a[i]-1)+ask(a[i]+1);
        add(a[i],1);
        while(t<=k&&j>i) {
            t+= ask(a[j]+1)+ask2(a[j]-1);
            add2(a[j],1); j--;
        }
        while(j<n&&t>k||j<i) {
            j++;
            t-=ask(a[j]+1)+ask2(a[j]-1);
            add2(a[j],-1);
        }
        res+=n-j;
    }
    printf("%I64d\n",res);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<iostream>
#include<string>
#include<deque>
#include<list>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define ll long long
#define lowbit(x) (x&-x)
#define f first
#define s second
#define pr pair<int,int>
#define mk(x,y) make_pair(x,y)
#define eps 1e-8
#define pi acos(-1.0)
#define square(x) ((x)*(x))
#define getvec(x,y) ((y)-(x))
#define xmul(x1,y1,x2,y2) ((x1)*(y2)-(x2)*(y1))
#define inf 0x3f3f3f3f
#define mod ((ll)1e9+7)
#define maxn 100010

map<int,int> l,r;
int n,a[maxn];
ll k;

void add(int p,int v) {
    for(int i=p;i>0;i-=lowbit(i)) {
        r[i]+=v;
        if(r[i]==0) r.erase(i);
    }
}
int ask(int p) {
    int res=0;
    for(int i=p;i<inf;i+=lowbit(i)) if(r.count(i)) res+=r[i];
    return res;
}

void add2(int p,int v) {
    for(int i=p;i<inf;i+=lowbit(i)) {
        l[i]+=v;
        if(l[i]==0) l.erase(i);
    }
}

int ask2(int p) {
    int res=0;
    for(int i=p;i>0;i-=lowbit(i)) if(l.count(i)) res+=l[i];
    return res;
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("G:\\ACM\\inandout\\in.txt","r",stdin);
//	freopen("G:\\ACM\\inandout\\out.txt","w",stdout);
#endif
    scanf("%d%I64d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",a+i);
    ll t= 0,res=0;
    for(int i=1,j=n;i<=n;i++) {
        t+=ask2(a[i]-1)+ask(a[i]+1);
        add(a[i],1);
        while(t<=k&&j>i) {
            t+= ask(a[j]+1)+ask2(a[j]-1);
            add2(a[j],1); j--;
        }
        while(j<n&&t>k||j<i) {
            j++;
            t-=ask(a[j]+1)+ask2(a[j]-1);
            add2(a[j],-1);
        }
        res+=n-j;
    }
    printf("%I64d\n",res);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<iostream>
#include<string>
#include<deque>
#include<list>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define ll long long
#define lowbit(x) (x&-x)
#define f first
#define s second
#define pr pair<int,int>
#define mk(x,y) make_pair(x,y)
#define eps 1e-8
#define pi acos(-1.0)
#define square(x) ((x)*(x))
#define getvec(x,y) ((y)-(x))
#define xmul(x1,y1,x2,y2) ((x1)*(y2)-(x2)*(y1))
#define inf 0x3f3f3f3f
#define mod ((ll)1e9+7)
#define maxn 100010

#define FOR(I,A,B) for (int I=int(A);I<int(B);++I)
#define MEM(A,B) memset(A,B,sizeof(A));
#define CPY(A,B) memcpy(A,B,sizeof(B));
typedef long long LL;
const int N(100010);
int n;
LL k, t;
int a[N], c1[N], c2[N];
map<int, int> num;
int low(int x) {return x & (-x);}
void update(int *c, int x, int v)
{
	 for (; x <= n; x += low(x)) c[x] += v;
}
int sum(int *c, int x)
{
	int ret = 0;
	for (; x; x -= low(x)) ret += c[x];
	return ret;
}
int main()
{
	//freopen("in.txt", "r", stdin);
	cin >> n >> k;
	FOR(i, 1, n + 1) {
	   cin >> a[i];
	   num[a[i]] = 0;
	}
	int m = 0;
	for (map<int, int> :: iterator it = num.begin(); it != num.end(); ++it)
	    it->second = ++m;
	FOR(i, 1, n + 1) a[i] = num[a[i]];
	LL ans = 0, t = 0;
	int l, r;
	for (r = n; r >= 2; --r) {
		t += sum(c2, a[r] - 1);
	    update(c2, a[r], 1);
	}
	for (l = 1, r = 2; l < n; ++l) {
	    t += l - 1 - sum(c1, a[l]);
	    t += sum(c2, a[l] - 1);
	    update(c1, a[l], 1);
	    for (; l == r || r <= n && t > k; ++r) {
	        t -= l - sum(c1, a[r]);
	        t -= sum(c2, a[r] - 1);
	        update(c2, a[r], - 1);
	    }
	    ans += n - r + 1;
	}
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define endl '\n'
#define endll puts("")
#define mp3(x,y,z) mp(x,mp(y,z))
#define inf mod
#define pii pair<int,int>
#define ort (bas+son>>1)
#define sol (root+root)
#define sag (sol+1)
using namespace std;
typedef long long ll;
const int N = 1e5+3;
int n,m,i,j,l,r,d,u,ii,jj,h[N];
ll st[N][3],k,t,hh[N],x,y,z,s;
map<int,int> MP;
ll f(int x,int d)
{
	ll t=0;
	for(;x;x-=x&-x)
		t+=st[x][d];
	return t;
}
void up(int x,int d,int k)
{
	for(;x and x<=s;x+=x&-x)
		st[x][d]+=k;
}
int main()
{
	cin>>n>>k;
	for(i=1;i<=n;i++)
	{
		scanf("%d",h+i);
		hh[i]=h[i];
	}
	sort(hh+1,hh+1+n);
	for(i=1;i<=n;i++)
		if(hh[i]!=hh[i-1])
			MP[hh[i]]=++s;
	for(i=n;i;i--)
	{
		hh[i]=hh[i+1]+f(MP[h[i]]-1,0);
		up(MP[h[i]],0,1);
	}
	r=1;
	for(i=1;i<n;i++)
	{
		up(MP[h[i]],1,1);
		while(1)
		{
			x=hh[1]-y-z;
			if(i==r)
				r++;
			else if(x<=k)
			{
				if(r<=n)
					t+=n-r+1;
				if(r-i>1)
				{
					y-=hh[i+1]-hh[i+2]-f(MP[h[i+1]]-1,2);
					z-=f(s,1)-f(MP[h[i+1]],1);
					up(MP[h[i+1]],2,-1);
				}
				x=hh[1]-y-z;
				break;
			}
			else
			{

				y+=hh[r]-hh[r+1];
				z+=f(s,1)-f(MP[h[r]],1);
				up(MP[h[r]],2,1);
				r++;
				x=hh[1]-y-z;
			}
		}
	}
	cout<<t<<endl;
}


#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define endl '\n'
#define endll puts("")
#define mp3(x,y,z) mp(x,mp(y,z))
#define inf mod
#define pii pair<int,int>
#define ort (bas+son>>1)
#define sol (root+root)
#define sag (sol+1)
using namespace std;
typedef long long ll;
const int N = 2e5+3;
int n,m,i,j,l,r,d,u,ii,jj,h[N];
ll st[N][3],k,t,hh[N],x,y,z,s;
map<int,int> MP;
ll f(int x,int d)
{
	ll t=0;
	for(;x;x-=x&-x)
		t+=st[x][d];
	return t;
}
void up(int x,int d,int k)
{
	for(;x and x<=s;x+=x&-x)
		st[x][d]+=k;
}
int main()
{
	cin>>n>>k;
	for(i=1;i<=n;i++)
	{
		scanf("%d",h+i);
		hh[i]=h[i];
	}
	sort(hh+1,hh+1+n);
	for(i=1;i<=n;i++)
		if(hh[i]!=hh[i-1])
			MP[hh[i]]=++s;
	for(i=n;i;i--)
	{
		hh[i]=hh[i+1]+f(MP[h[i]]-1,0);
		up(MP[h[i]],0,1);
	}
	r=1;
	for(i=1;i<n;i++)
	{
		up(MP[h[i]],1,1);
		while(1)
		{
			x=hh[1]-y-z;
			if(i==r)
				r++;
			else if(x<=k)
			{
				if(r<=n)
					t+=n-r+1;
				if(r-i>1)
				{
					y-=hh[i+1]-hh[i+2]-f(MP[h[i+1]]-1,2);
					z-=f(s,1)-f(MP[h[i+1]],1);
					up(MP[h[i+1]],2,-1);
				}
				x=hh[1]-y-z;
				break;
			}
			else
			{

				y+=hh[r]-hh[r+1];
				z+=f(s,1)-f(MP[h[r]],1);
				up(MP[h[r]],2,1);
				r++;
				x=hh[1]-y-z;
			}
		}
	}
	cout<<t<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define in(n) scanf("%d",&n)
#define out(n) printf("%d ",n);
#define F first
#define S second
void up(vector<int> &ar,int j,int x,int k)
{
    for(;j<=k;j+=j&(-j))
        ar[j]+=x;
    return ;
}
int que(vector<int> &ar,int j)
{
    int x=0;
    for(;j>0;j-=(j)&(-j))
        x+=ar[j];
    return x;
}
int main()
{
    int i,j,k,n;
    long long int m,ans=0,t=0,x=0;
    in(n);
    cin >> m;
    vector<pair<int,int> > tp(n);
    for(i=0;i<n;i++)
        in(tp[i].F),tp[i].S=i;
    sort(tp.begin(),tp.end());
    vector<int> v(n);
    i=0,k=1,j=0;
    while(i<n)
    {
        while(j<n && tp[i].F==tp[j].F)
            v[tp[j].S]=k,j++;
        i=j,k++;
    }
    vector<int> lt(k+1,0),rt(k+1,0),mid(k+1,0);
    for(i=n-1;i>=0;i--)
        t+=que(rt,v[i]-1),up(rt,v[i],1,k);
    if(t<=m)
        ans=n,ans*=(n-1),ans/=2;
    else
    {
        up(lt,v[0],1,k),up(rt,v[0],-1,k);
        i=1,j=1;
        while(i<n && j<n)
        {
            while(j<n && x<t-m)
            {
                up(rt,v[j],-1,k);
                up(mid,v[j],1,k);
                x+=(que(lt,k)-que(lt,v[j])+que(rt,v[j]-1));
                j++;
            }
            if(x>=t-m)
                ans+=(n-j);
            up(mid,v[i],-1,k);
            up(lt,v[i],1,k);
            x-=(que(lt,k)-que(lt,v[i])+que(rt,v[i]-1));
            i++;
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define in(n) scanf("%d",&n)
#define out(n) printf("%d ",n);
#define F first
#define S second
void up(vector<int> &ar,int j,int x,int k)
{
    for(;j<=k;j+=j&(-j))
        ar[j]+=x;
    return ;
}
int que(vector<int> &ar,int j)
{
    int x=0;
    for(;j>0;j-=(j)&(-j))
        x+=ar[j];
    return x;
}
int main()
{
    int i,j,k,n;
    long long int m,ans=0,t=0,x=0;
    in(n);
    cin >> m;
    vector<pair<int,int> > tp(n);
    for(i=0;i<n;i++)
        in(tp[i].F),tp[i].S=i;
    sort(tp.begin(),tp.end());
    vector<int> v(n);
    i=0,k=1,j=0;
    while(i<n)
    {
        while(j<n && tp[i].F==tp[j].F)
            v[tp[j].S]=k,j++;
        i=j,k++;
    }
    vector<int> lt(k+1,0),rt(k+1,0),mid(k+1,0);
    for(i=n-1;i>=0;i--)
        t+=que(rt,v[i]-1),up(rt,v[i],1,k);
    up(lt,v[0],1,k),up(rt,v[0],-1,k);
    i=1,j=1;
    while(i<n && j<n)
    {
        while(j<n && x<t-m)
        {
            up(rt,v[j],-1,k);
            up(mid,v[j],1,k);
            x+=(que(lt,k)-que(lt,v[j])+que(rt,v[j]-1));
            j++;
        }
        if(x>=t-m)
            ans+=(n-j);
        up(mid,v[i],-1,k);
        up(lt,v[i],1,k);
        x-=(que(lt,k)-que(lt,v[i])+que(rt,v[i]-1));
        i++;
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr first
#define sc second
#define ll  long long
#define mp make_pair
#define mod 1000000007

using namespace std;

ll k,n;
int a[100100];
int dp[3][100100*4];
int num =1 ;
void add(int in,int a,int b,int x,int l=1,int h = num ,int p = 1){
    if(a == l && b == h){
        dp[in][p] += x;
        return ;
    }
    int m = (l+h)/2;
    if(b <= m){
        add(in,a,b,x,l,m,2*p);
    }
    else if(a > m){

        add(in,a,b,x,m+1,h,2*p+1);
    }
    else{
        add(in,m+1,b,x,m+1,h,2*p+1);
        add(in,a,m,x,l,m,2*p);
    }
    dp[in][p] = dp[in][2*p] + dp[in][2*p+1];
}
int cal(int in,int a,int b,int x=0,int l=1,int h = num ,int p = 1){
    if(a == l && b == h){
        return dp[in][p];
    }
    int m = (l+h)/2;
    if(b <= m){
        return cal(in,a,b,x,l,m,2*p);
    }
    else if(a > m){
        return cal(in,a,b,x,m+1,h,2*p+1);
    }
    else{
        return cal(in,m+1,b,x,m+1,h,2*p+1)+cal(in,a,m,x,l,m,2*p);
    }
}
map<int,int>st;
int main() {
    cin>>n>>k;
    for(int i=1 ;i <=n ;i ++){
        scanf("%d",&a[i]);
        st[a[i]] = i;
    }
    map<int,int>::iterator it;
    for(it = st.begin() ; it != st.end() ; it++){
        it->sc = num++;
    }
    ll r1 = 0;
    ll res = 0;
    int j = 1;
    k *= 2;
    for(int i=n; i>=1 ;i --){
        if(st[a[i]] != 1)
            r1 += cal(2,1,st[a[i]]-1);
        if(i != 1)
            add(2,st[a[i]],st[a[i]],1);
    }
    add(0,st[a[1]],st[a[1]],1);
    r1 *= 2;
    for(int i=1; i<=n ;i ++){
        if(j > i){
            int r2 = 0;
            if(st[a[i]]+1 <= num)
                r2 = cal(0,st[a[i]]+1,num);
            if(st[a[i]]-1 >= 1)
                r2 += cal(2,1,st[a[i]]-1);
            add(0,st[a[i]],st[a[i]],1);
            add(1,st[a[i]],st[a[i]],-1);

            int r3 = 0;
            if(st[a[i]]-1 > 0)
                r3 = cal(1,1,st[a[i]]-1);
            r1 += r2;
            r1 += r3;
        }
        j = max(j,i+1);
        for( ; j<=n ; j++){
            if(r1 <= k)
                break;
            int r2 = 0;
            if(st[a[j]]+1 <= num)
                r2 = cal(0,st[a[j]]+1,num);
            if(st[a[j]]-1 >= 1)
                r2 += cal(2,1,st[a[j]]-1);
            add(1,st[a[j]],st[a[j]],1);
            add(2,st[a[j]],st[a[j]],-1);

            int r3 = 0;
            if(st[a[j]]+1 <= num)
                r3 = cal(1,st[a[j]]+1,num);
            r1 -= r2;
            r1 -= r3;
        }
        if(r1 <= k)
            res += n+1-j;
    }
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr first
#define sc second
#define ll  long long
#define mp make_pair
#define mod 1000000007

using namespace std;

ll k,n;
pii a[100100];
int r[100100];

int dp[100100*4];
void add(int a,int b,int x,int l=1,int h = n ,int p = 1){
    if(a == l && b == h){
        dp[p] += x;
        return ;
    }
    int m = (l+h)/2;
    if(b <= m){
        add(a,b,x,l,m,2*p);
    }
    else if(a > m){

        add(a,b,x,m+1,h,2*p+1);
    }
    else{
        add(m+1,b,x,m+1,h,2*p+1);
        add(a,m,x,l,m,2*p);
    }
    dp[p] = dp[2*p] + dp[2*p+1];
}
int cal(int a,int b,int x=0,int l=1,int h = n ,int p = 1){
    if(a == l && b == h){
        return dp[p];
    }
    int m = (l+h)/2;
    if(b <= m){
        return cal(a,b,x,l,m,2*p);
    }
    else if(a > m){
        return cal(a,b,x,m+1,h,2*p+1);
    }
    else{
        return cal(m+1,b,x,m+1,h,2*p+1)+cal(a,m,x,l,m,2*p);
    }
}

int main() {
    cin>>n>>k;
    for(int i=1 ;i <=n ;i ++){
        scanf("%d",&a[i].fr);
        a[i].sc = i;
    }

    sort(a+1,a+1+n);

    for(int i=1; i<=n ;){
        int j;
        for( j=i ; j<=n ; j++){
            if(a[i].fr != a[j].fr)
                break;
            r[a[j].sc] += cal(a[j].sc,n);
        }

        for( j=i ; j<=n ; j++){
            if(a[i].fr != a[j].fr)
                break;
            add(a[j].sc,a[j].sc,1);
        }
        i = j;
    }
    reverse(a+1,a+1+n);
    for(int i=0 ; i<=4*n+100 ; i++){
        dp[i] = 0;
    }
    for(int i=1; i<=n ;){
        int j;
        for( j=i ; j<=n ; j++){
            if(a[i].fr != a[j].fr)
                break;
            r[a[j].sc] += cal(1,a[j].sc);
        }

        for( j=i ; j<=n ; j++){
            if(a[i].fr != a[j].fr)
                break;
            add(a[j].sc,a[j].sc,1);
        }
        i = j;
    }
    int j = 1;
    k *= 2;
    ll r1 = 0;
    for(int i=1 ; i<=n ; i++)
        r1 += r[i];

    ll res = 0;
    for(int i=1; i<=n ;i ++){
        if(j > i)
            r1 += r[i];
        j = max(j,i+1);
        for( ; j<=n ; j++){
            if(r1 <= k)
                break;
            r1 -= r[j];
        }
        if(r1 <= k)
            res += n+1-j;
    }
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr first
#define sc second
#define ll  long long
#define mp make_pair
#define mod 1000000007

using namespace std;

ll k,n;
int a[100100];
int dp[3][100100*4];
int num =1 ;
void add(int in,int a,int b,int x,int l=1,int h = num ,int p = 1){
    if(a == l && b == h){
        dp[in][p] += x;
        return ;
    }
    int m = (l+h)/2;
    if(b <= m){
        add(in,a,b,x,l,m,2*p);
    }
    else if(a > m){

        add(in,a,b,x,m+1,h,2*p+1);
    }
    else{
        add(in,m+1,b,x,m+1,h,2*p+1);
        add(in,a,m,x,l,m,2*p);
    }
    dp[in][p] = dp[in][2*p] + dp[in][2*p+1];
}
int cal(int in,int a,int b,int x=0,int l=1,int h = num ,int p = 1){
    if(a == l && b == h){
        return dp[in][p];
    }
    int m = (l+h)/2;
    if(b <= m){
        return cal(in,a,b,x,l,m,2*p);
    }
    else if(a > m){
        return cal(in,a,b,x,m+1,h,2*p+1);
    }
    else{
        return cal(in,m+1,b,x,m+1,h,2*p+1)+cal(in,a,m,x,l,m,2*p);
    }
}
map<int,int>st;
int main() {
    cin>>n>>k;
    for(int i=1 ;i <=n ;i ++){
        scanf("%d",&a[i]);
        st[a[i]] = i;
    }
    map<int,int>::iterator it;
    for(it = st.begin() ; it != st.end() ; it++){
        it->sc = num++;
    }
    ll r1 = 0;
    ll res = 0;
    int j = 1;
    for(int i=n; i>=1 ;i --){
        if(st[a[i]] != 1)
            r1 += cal(2,1,st[a[i]]-1);
        if(i != 1)
            add(2,st[a[i]],st[a[i]],1);
    }
    add(0,st[a[1]],st[a[1]],1);
    for(int i=1; i<=n ;i ++){
        if(j > i){
            int r2 = 0;
            if(st[a[i]]+1 <= num)
                r2 = cal(0,st[a[i]]+1,num);
            if(st[a[i]]-1 >= 1)
                r2 += cal(2,1,st[a[i]]-1);
            add(0,st[a[i]],st[a[i]],1);
            add(1,st[a[i]],st[a[i]],-1);

            int r3 = 0;
                r1 += r2;
            r1 += r3;
        }
        j = max(j,i+1);
        for( ; j<=n ; j++){
            if(r1 <= k)
                break;
            int r2 = 0;
            if(st[a[j]]+1 <= num)
                r2 = cal(0,st[a[j]]+1,num);
            if(st[a[j]]-1 >= 1)
                r2 += cal(2,1,st[a[j]]-1);
            add(1,st[a[j]],st[a[j]],1);
            add(2,st[a[j]],st[a[j]],-1);

            int r3 = 0;
            r1 -= r2;
            r1 -= r3;
        }
        res += n+1-j;
    }
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr first
#define sc second
#define ll  long long
#define mp make_pair
#define mod 1000000007

using namespace std;

ll k,n;
pii a[100100];
int r[100100];

int dp[100100*4];
void add(int a,int b,int x,int l=1,int h = n ,int p = 1){
    if(a == l && b == h){
        dp[p] += x;
        return ;
    }
    int m = (l+h)/2;
    if(b <= m){
        add(a,b,x,l,m,2*p);
    }
    else if(a > m){

        add(a,b,x,m+1,h,2*p+1);
    }
    else{
        add(m+1,b,x,m+1,h,2*p+1);
        add(a,m,x,l,m,2*p);
    }
    dp[p] = dp[2*p] + dp[2*p+1];
}
int cal(int a,int b,int x=0,int l=1,int h = n ,int p = 1){
    if(a == l && b == h){
        return dp[p];
    }
    int m = (l+h)/2;
    if(b <= m){
        return cal(a,b,x,l,m,2*p);
    }
    else if(a > m){
        return cal(a,b,x,m+1,h,2*p+1);
    }
    else{
        return cal(m+1,b,x,m+1,h,2*p+1)+cal(a,m,x,l,m,2*p);
    }
}

int main() {
    cin>>n>>k;
    for(int i=1 ;i <=n ;i ++){
        scanf("%d",&a[i].fr);
        a[i].sc = i;
    }

    sort(a+1,a+1+n);

    for(int i=1; i<=n ;){
        int j;
        for( j=i ; j<=n ; j++){
            if(a[i].fr != a[j].fr)
                break;
            r[a[i].sc] += cal(a[i].sc,n);
        }

        for( j=i ; j<=n ; j++){
            if(a[i].fr != a[j].fr)
                break;
            add(a[i].sc,a[i].sc,1);
        }
        i = j;
    }
    reverse(a+1,a+1+n);
    for(int i=0 ; i<=4*n+100 ; i++){
        dp[i] = 0;
    }
    for(int i=1; i<=n ;){
        int j;
        for( j=i ; j<=n ; j++){
            if(a[i].fr != a[j].fr)
                break;
            r[a[i].sc] += cal(1,a[i].sc);
        }

        for( j=i ; j<=n ; j++){
            if(a[i].fr != a[j].fr)
                break;
            add(a[i].sc,a[i].sc,1);
        }
        i = j;
    }
    int j = 1;
    k *= 2;
    ll r1 = 0;
    for(int i=1 ; i<=n ; i++)
        r1 += r[i];
    ll res = 0;
    for(int i=1; i<=n ;i ++){
        j = max(j,i);
        for( ; j<=n ; j++){
            if(r1 <= k)
                break;
            r1 -= r[j];
        }

        res += n-j+1;
        if(j > i){
            r1 += r[i];
        }
        else
            res--;
    }
    cout<<res<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr first
#define sc second
#define ll  long long
#define mp make_pair
#define mod 1000000007

using namespace std;

ll k,n;
int a[100100];
int dp[3][100100*4];
int num =1 ;
void add(int in,int a,int b,int x,int l=1,int h = num ,int p = 1){
    if(a == l && b == h){
        dp[in][p] += x;
        return ;
    }
    int m = (l+h)/2;
    if(b <= m){
        add(in,a,b,x,l,m,2*p);
    }
    else if(a > m){

        add(in,a,b,x,m+1,h,2*p+1);
    }
    else{
        add(in,m+1,b,x,m+1,h,2*p+1);
        add(in,a,m,x,l,m,2*p);
    }
    dp[in][p] = dp[in][2*p] + dp[in][2*p+1];
}
int cal(int in,int a,int b,int x=0,int l=1,int h = num ,int p = 1){
    if(a == l && b == h){
        return dp[in][p];
    }
    int m = (l+h)/2;
    if(b <= m){
        return cal(in,a,b,x,l,m,2*p);
    }
    else if(a > m){
        return cal(in,a,b,x,m+1,h,2*p+1);
    }
    else{
        return cal(in,m+1,b,x,m+1,h,2*p+1)+cal(in,a,m,x,l,m,2*p);
    }
}
map<int,int>st;
int main() {
    cin>>n>>k;
    for(int i=1 ;i <=n ;i ++){
        scanf("%d",&a[i]);
        st[a[i]] = i;
    }
    map<int,int>::iterator it;
    for(it = st.begin() ; it != st.end() ; it++){
        it->sc = num++;
    }
    ll r1 = 0;
    ll res = 0;
    int j = 1;
    k *= 2;
    for(int i=n; i>=1 ;i --){
        if(st[a[i]] != 1)
            r1 += cal(2,1,st[a[i]]-1);
        if(i != 1)
        add(2,st[a[i]],st[a[i]],1);
    }
    r1 *= 2;
    for(int i=1; i<=n ;i ++){
        if(j > i){
            int r2 = 0;
            if(st[a[i]]+1 <= num)
                r2 = cal(0,st[a[i]]+1,num);
            if(st[a[i]]-1 >= 1)
                r2 += cal(2,1,st[a[i]]-1);
            add(0,st[a[i]],st[a[i]],1);
            add(1,st[a[i]],st[a[i]],-1);

            int r3 = 0;
            if(st[a[i]]-1 > 0)
                r3 = cal(1,1,st[a[i]]-1);
            r1 += r2;
            r1 += r3;
        }
        j = max(j,i+1);
        for( ; j<=n ; j++){
            if(r1 <= k)
                break;
            int r2 = 0;
            if(st[a[j]]+1 <= num)
                r2 = cal(0,st[a[j]]+1,num);
            if(st[a[j]]-1 >= 1)
                r2 += cal(2,1,st[a[j]]-1);
            add(1,st[a[j]],st[a[j]],1);
            add(2,st[a[j]],st[a[j]],-1);

            int r3 = 0;
            if(st[a[j]]+1 <= num)
                r3 = cal(1,st[a[j]]+1,num);
            r1 -= r2;
            r1 -= r3;
        }
        if(r1 <= k)
            res += n+1-j;
    }
    cout<<res<<endl;
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,c[100010],f[100010];
long long ans;
int low[100010],high[100010],sum_high[100010],sum_low[100010];
struct A
{
	int no,x;
}a[100010];
long long k;
inline bool cmp(const A &x1,const A &x2)
{return x1.x<x2.x;}
inline void updata(int x,int y)
{
	for(;x<=n;x+=x&-x)
		f[x]+=y;
}
inline int query(int x)
{
	int tot=0;
	for(;x;x-=x&-x)
		tot+=f[x];
	return tot;
}
int main()
{
	scanf("%d%I64d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i].x);
		a[i].no=i;
	}
	sort(a+1,a+1+n,cmp);
	int cnt=0,last=-1;
	for(int i=1;i<=n;i++)
	{
		if(last!=a[i].x)
		{
			cnt++;
			last=a[i].x;
		}
		c[a[i].no]=cnt;
	}
	for(int i=1;i<=n;i++)
	{
		updata(c[i],1);
		high[i]=i-query(c[i]);
	}
	memset(f,0,sizeof f);
	for(int i=n;i>=1;i--)
	{
		updata(c[i],1);
		low[i]=query(c[i]-1);
	}
	for(int i=n;i>=1;i--) sum_high[i]=sum_high[i+1]+high[i];
	for(int i=1;i<=n;i++) sum_low[i]=sum_low[i-1]+low[i];
	/*for(int i=1;i<=n;i++)
	{
		printf("%d\n",sum_low[i]);
	}*/
	for(int i=2;i<=n;i++)
	{
		int l=1,r=i-1;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			if(sum_high[1]-sum_high[mid+1]+sum_high[i]-sum_low[i-1]+sum_low[mid]<=k) l=mid+1;
			else r=mid-1;
		}
		ans+=r;
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,c[100010],f[100010],g[100010];
long long ans,sum;
int low[100010],high[100010];
struct A
{
	int no,x;
}a[100010];
long long k;
inline bool cmp(const A &x1,const A &x2)
{return x1.x<x2.x;}
inline void updata_f(int x,int y)
{
	for(;x<=n;x+=x&-x)
		f[x]+=y;
}
inline int query_f(int x)
{
	int tot=0;
	for(;x;x-=x&-x)
		tot+=f[x];
	return tot;
}
inline void updata_g(int x,int y)
{
	for(;x<=n;x+=x&-x)
		g[x]+=y;
}
inline int query_g(int x)
{
	int tot=0;
	for(;x;x-=x&-x)
		tot+=g[x];
	return tot;
}
int main()
{
	scanf("%d%I64d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i].x);
		a[i].no=i;
	}
	sort(a+1,a+1+n,cmp);
	int cnt=0,last=-1;
	for(int i=1;i<=n;i++)
	{
		if(last!=a[i].x)
		{
			cnt++;
			last=a[i].x;
		}
		c[a[i].no]=cnt;
	}
	for(int i=n;i>=1;i--)
	{
		updata_g(c[i],1);
		sum+=query_g(c[i]-1);
	}
	int r=2;
	updata_f(c[1],1);
	updata_g(c[1],-1);
	for(int i=1;i<=n;i++)
	{
		while(r<=n&&sum>k)
		{
			sum-=i-query_f(c[r])+query_g(c[r]-1);
			updata_g(c[r],-1);
			r++;
		}
		ans+=n+1-r;
		if(i<n)
		{
			updata_f(c[i+1],1);
			sum+=i+1-query_f(c[i+1])+query_g(c[i+1]-1);
		}
	}
	printf("%I64d\n",ans);
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,c[100010],f[100010],g[100010];
long long ans,sum;
int low[100010],high[100010];
struct A
{
    int no,x;
}a[100010];
long long k;
inline bool cmp(const A &x1,const A &x2)
{return x1.x<x2.x;}
inline void updata_f(int x,int y)
{
    for(;x<=n;x+=x&-x)
        f[x]+=y;
}
inline int query_f(int x)
{
    int tot=0;
    for(;x;x-=x&-x)
        tot+=f[x];
    return tot;
}
inline void updata_g(int x,int y)
{
    for(;x<=n;x+=x&-x)
        g[x]+=y;
}
inline int query_g(int x)
{
    int tot=0;
    for(;x;x-=x&-x)
        tot+=g[x];
    return tot;
}
int main()
{
    scanf("%d%I64d",&n,&k);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i].x);
        a[i].no=i;
    }
    sort(a+1,a+1+n,cmp);
    int cnt=0,last=-1;
    for(int i=1;i<=n;i++)
    {
        if(last!=a[i].x)
        {
            cnt++;
            last=a[i].x;
        }
        c[a[i].no]=cnt;
    }
    for(int i=n;i>=1;i--)
    {
        updata_g(c[i],1);
        sum+=query_g(c[i]-1);
    }
    int r=2;
    updata_f(c[1],1);
    updata_g(c[1],-1);
    for(int i=1;i<=n;i++)
    {
        if(i==r) sum-=i-query_f(c[r])+query_g(c[r]-1),updata_g(c[r],-1),r++;
        while(r<=n&&sum>k)
        {
            sum-=i-query_f(c[r])+query_g(c[r]-1);
            updata_g(c[r],-1);
            r++;
        }
        ans+=n+1-r;
        if(i<n)
        {
            updata_f(c[i+1],1);
            sum+=i+1-query_f(c[i+1])+query_g(c[i+1]-1);
        }
    }
    printf("%I64d\n",ans);
    return 0;
}


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

int N, a[100010], BIT[2][100010], r;
long long K, Inversions, ans;
vector <int> Unique;

void update(int id, int x, int val)
{
    if (id == 0)
        for (; x; x -= x & (-x))
            BIT[0][x] += val;
    else for (; x <= N; x += x & (-x))
            BIT[1][x] += val;
}

int get(int id, int x)
{
    int hsgs = 0;
    if (id == 0)
        for (x++; x <= N; x += x & (-x))
            hsgs += BIT[0][x];
    else for (x--; x; x -= x & (-x))
            hsgs += BIT[1][x];
    return hsgs;
}

main()
{
    scanf("%d %lld", &N, &K);
    for (int i = 1; i <= N; i++)
    {
        scanf("%d", &a[i]);
        Unique.push_back(a[i]);
    }
    sort(Unique.begin(), Unique.end());
    Unique.resize(distance(Unique.begin(), unique(Unique.begin(), Unique.end())));
    for (int i = 1; i <= N; i++)
        a[i] = lower_bound(Unique.begin(), Unique.end(), a[i]) - Unique.begin() + 1;

    update(0, a[1], 1);
    for (r = N; r > 1; r--)
    {
        int add = get(1, a[r]) + (a[1] > a[r]);
        if (Inversions + add > K)
        {
            r++;
            ans = N - r + 1;
            break;
        }
        update(1, a[r], 1);
        Inversions += add;
    }
    if (r == 1) r = 2, ans = N - 1;
    for (int l = 2; l < N; l++)
    {
        Inversions += get(1, a[l]) + get(0, a[l]);      update(0, a[l], 1);
        if (l == r) update(1, a[r], -1), r++;
        for (; Inversions > K && r <= N; r++)
        {
            Inversions -= get(0, a[r]) + get(1, a[r]);
            update(1, a[r], -1);
        }
        ans += N - r + 1;
    }

    printf("%I64d", ans);
}


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

int N, a[100010], BIT[2][100010], r;
long long K, Inversions, ans;
vector <int> Unique;

void update(int id, int x, int val)
{
    if (id == 0)
        for (; x; x -= x & (-x))
            BIT[0][x] += val;
    else for (; x <= N; x += x & (-x))
            BIT[1][x] += val;
}

int get(int id, int x)
{
    int hsgs = 0;
    if (id == 0)
        for (x++; x <= N; x += x & (-x))
            hsgs += BIT[0][x];
    else for (x--; x; x -= x & (-x))
            hsgs += BIT[1][x];
    return hsgs;
}

void Delete(int &r)
{
    Inversions -= get(0, a[r]) + get(1, a[r]);
    update(1, a[r], -1);
    r++;
}

main()
{
    scanf("%d %lld", &N, &K);
    for (int i = 1; i <= N; i++)
    {
        scanf("%d", &a[i]);
        Unique.push_back(a[i]);
    }

    sort(Unique.begin(), Unique.end());
    Unique.resize(distance(Unique.begin(), unique(Unique.begin(), Unique.end())));
    for (int i = 1; i <= N; i++)
        a[i] = lower_bound(Unique.begin(), Unique.end(), a[i]) - Unique.begin() + 1;

    update(0, a[1], 1);
    for (r = N; r > 1; r--)
    {
        int add = get(1, a[r]) + (a[1] > a[r]);
        if (Inversions + add > K) break;
        update(1, a[r], 1);
        Inversions += add;
    }
    r++;    ans = N - r + 1;
    for (int l = 2; l < N; l++)
    {
        Inversions += get(1, a[l]) + get(0, a[l]);
        update(0, a[l], 1);
        if (l == r) Delete(r);
        while (Inversions > K && r <= N) Delete(r);
        ans += N - r + 1;
    }

    printf("%I64d", ans);
}


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

#define N 100000

using std::cin;
using std::cout;

int segi[N << 2] = {0};
int segj[N << 2] = {0};
long long c;
int M;

void update(int seg[], int k, int delta) {
	while (k <= M) {
		seg[k] += delta;
		k += ((~k + 1) & k);
	}
}

int count(int seg[], int k) {
	int ret = 0;
	while (k > 0) {
		ret += seg[k];
		k -= ((~k + 1) & k);
	}
	return ret;
}

void add(int seg[], int k) {
	c += segi[M] - count(segi, k);
	c += count(segj, k - 1);
	update(seg, k, 1);
}

void del(int seg[], int k) {
	c -= segi[M] - count(segi, k);
	c -= count(segj, k - 1);
	update(seg, k, -1);
}

struct chunk {
	int data;
	int idx;
};

chunk in[N];
int a[N];

bool cmpData(const chunk & a, const chunk & b) {
	return a.data < b.data;
}

int main() {
	int n;
	long long k;
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		cin >> in[i].data;
		in[i].idx = i;
	}
	std::sort<chunk*>(in, in + n, cmpData);
	a[in[0].idx] = 1;
	int t = 1;
	for (int i = 1; i < n; i++) {
		if (in[i].data != in[i - 1].data) {
			t += 2;
		}
		a[in[i].idx] = t;
	}
	for (M = 1; M < t; M *= 2) {}
	c = 0;
	for (int i = n - 1; i >= 1; i--) {
		add(segj, a[i]);
	}
	add(segi, a[0]);
	long long ans = 0;
	for (int i = 0, j = 1; i < n - 1; add(segi, a[++i])) {
		while (c > k && j < n) {
			del(segj, a[j++]);
		}
		if (j >= n) {
			break;
		}
		ans += n - j;
		if (i + 1 >= j) {
			del(segj, a[j++]);
		}
	}
	cout << ans << "\n";
	return 0;
}


#include <iostream>
#include <map>

using std::cin;
using std::cout;

class SegTree {
	struct node {
		int c;
		node * left;
		node * right;

		node() : c(0), left(0), right(0) {}

		void insert(int k, int l, int r) {
			c++;
			if (l < r) {
				int mid = r - (r - l) / 2;
				if (k < mid) {
					if (left == 0) {
						left = new node();
					}
					left->insert(k, l, mid - 1);
				} else {
					if (right == 0) {
						right = new node();
					}
					right->insert(k, mid, r);
				}
			}
		}

		bool remove(int k, int l, int r) {
			if (this == 0) {
				return false;
			}
			bool succ;
			if (l < r) {
				int mid = r - (r - l) / 2;
				if (k < mid) {
					succ = left->remove(k, l, mid - 1);
				} else {
					succ = right->remove(k, mid, r);
				}
			} else {
				succ = true;
			}
			if (succ) {
				c--;
			}
			return succ;
		}

		int count(int ll, int rr, int l, int r) {
			if (this == 0) {
				return 0;
			}
			if (ll < l) {
				ll = l;
			}
			if (rr > r) {
				rr = r;
			}
			if (ll == l && rr == r) {
				return c;
			}
			int mid = r - (r - l) / 2;
			return left->count(ll, rr, l, mid - 1) + right->count(ll, rr, mid, r);
		}
	};
	node root;
public:
	SegTree() : root() {}

	void insert(int item) {
		root.insert(item, 0, (1 << 31) - 1);
	}

	void remove(int item) {
		root.remove(item, 0, (1 << 31) - 1);
	}

	int count(int l, int r) {
		if (l > r) {
			return 0;
		}
		return root.count(l, r, 0, (1 << 31) - 1);
	}
};

SegTree tree;
int count = 0;

void add(int k) {
	count += tree.count(k + 1, (1 << 31) - 1);
	tree.insert(k);
	cout << "add " << k << " " << count << "\n";
}

void del(int k) {
	tree.remove(k);
	count -= tree.count(0, k - 1);
	cout << "del " << k << " " << count << "\n";
}

long a[100000];

int main() {
	int n, k;
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	long long ans = 0;
	add(a[0]);
	for (int i = 1, j = 0; i < n; i++) {
		add(a[i]);
		while (count > k) {
			del(a[j++]);
		}
		ans += i - j;
	}
	cout << ans << "\n";
	return 0;
}


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

#define N 100000

using std::cin;
using std::cout;

int segi[N << 2] = {0};
int segj[N << 2] = {0};
long long c;
int M;

void update(int seg[], int k, int delta) {
	while (k <= M) {
		seg[k] += delta;
		k += ((~k + 1) & k);
	}
}

int count(int seg[], int k) {
	int ret = 0;
	while (k > 0) {
		ret += seg[k];
		k -= ((~k + 1) & k);
	}
	return ret;
}

void add(int seg[], int k) {
	c += segi[M] - count(segi, k);
	c += count(segj, k - 1);
	update(seg, k, 1);
}

void del(int seg[], int k) {
	c -= segi[M] - count(segi, k);
	c -= count(segj, k - 1);
	update(seg, k, -1);
}

struct chunk {
	int data;
	int idx;
};

chunk in[N];
int a[N];

bool cmpData(const chunk & a, const chunk & b) {
	return a.data < b.data;
}

int main() {
	int n;
	long long k;
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		cin >> in[i].data;
		in[i].idx = i;
	}
	std::sort<chunk*>(in, in + n, cmpData);
	a[in[0].idx] = 1;
	int t = 1;
	for (int i = 1; i < n; i++) {
		if (in[i].data != in[i - 1].data) {
			t += 2;
		}
		a[in[i].idx] = t;
	}
	for (M = 1; M < t; M *= 2) {}
	c = 0;
	for (int i = n - 1; i >= 1; i--) {
		add(segj, a[i]);
	}
	add(segi, a[0]);
	long long ans = 0;
	for (int i = 0, j = 1; i < n - 1; add(segi, a[++i])) {
		while (c > k && j < n) {
			del(segj, a[j++]);
		}
		if (j >= n) {
			break;
		}
		ans += n - j;
		if (j + 1 <= i) {
			del(segj, a[j++]);
		}
	}
	cout << ans << "\n";
	return 0;
}