import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.StringTokenizer;
public class Main {
    public interface SpaceCharFilter {
        public boolean isSpaceChar(int ch);
        }
    class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;
        public InputReader(InputStream stream) {
            this.stream = stream;
        }
        public int read() {

            if (numChars == -1)

                throw new InputMismatchException();

            if (curChar >= numChars) {

                curChar = 0;

                try {

                    numChars = stream.read(buf);

                } catch (IOException e) {

                    throw new InputMismatchException();

                }

                if (numChars <= 0)

                    return -1;
            }
            return buf[curChar++];
        }
        public int readInt() {

            int c = read();

            while (isSpaceChar(c))

            c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
            throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
        public long readLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }
        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }
        public boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
        public String next() {
            return readString();
        }
    }
class OutputWriter {
        private final PrintWriter writer;
        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }
        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }
        public void print(Object...objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0)
                    writer.print(' ');
                writer.print(objects[i]);
            }
        }
        public void printLine(Object...objects) {
            print(objects);
            writer.println();
        }
        public void close() {
            writer.close();
        }
        public void flush() {
            writer.flush();
        }
        }
    /**
     * @param args
     */
    InputReader in= new InputReader(System.in);
    OutputWriter out = new OutputWriter(System.out);
    StringTokenizer tok;
        public static void main(String[] args) throws IOException
        {
           new Main().run();
        }
        void run() throws IOException
        {
           solve();
           out.flush();
           tok=null;
        }
        void solve() throws IOException{
            /*
             * tha deiksw oti i allilepikalipsi mas einai adiafori:
             * estw xi...xj ta allilepikaliptomena simeia
             * tote ginontai -xi.....-xj apo to  prefix -
             * kai xi...xj apo to suffix -
             * to opoio einai san na ginontai meion ta 1...i-1 kai ta j+1...n
             * opote auto pou kanw einai gia kathe thesi na briskw to kalitero prefix  pou ehei ginei mexri(i kai pio pisw) apo auto to simeio:
             * A[i]=A[i-1]+V[i]  { dil edw to prefix - stamataei toulaxiston mia thesi pio prin}
             * i A[i]=-1...i -V[i]
             * afou ta ehw ipologisei auta ksekinaw apo pisw suffix  kathe fora prsthetw neo stoixeio ka ibriskw to max :)
             */
            int n=in.readInt();
            int[] V=new int[n];
            int[] A=new int[n];
            int sum=0;
            for (int i=0;i<n;++i){
                 V[i]=in.readInt();
                sum+=V[i];
                if (i==0){
                    A[i]=Math.max(V[i],-V[i]);
                }else{
                    A[i]=Math.max(A[i-1]+V[i], -sum);
            } }
            int lsum=0;
            int best=-sum;
            for (int i=n-1;i>=0;--i){
                lsum+=V[i];
                if (i==0){
                    best=Math.max(best,-lsum);
                }else{
                best=Math.max(best,-lsum+A[i-1]); //me suffix -
                best=Math.max(best,lsum+A[i-1]); //xwris
            } }
            out.printLine(best);

        }
}


import java.util.Scanner;

public class sample {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a;
        int[] prefSum;
        int[] suffSum;
        a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = in.nextInt();
        prefSum = new int[((n / 2)+ n%2)];
        suffSum = new int[n >> 1];
        for (int i = 0; i < prefSum.length; i++)
            prefSum[i] = a[i] + (i > 0 ? prefSum[i - 1] : 0);
        for (int i = 0; i < n >> 1; i++)
            suffSum[i] = a[n - i - 1] + (i > 0 ? suffSum[i - 1] : 0);
        int max1 = prefSum[prefSum.length-1];
        for (int i = 0; i < prefSum.length; i++)
            max1 = Math.max(max1, prefSum[prefSum.length-1] - 2 * prefSum[i]);
        int max2 = suffSum[(n >> 1) - 1];
        for (int i = 0; i < n >> 1; i++)
            max2 = Math.max(max2, suffSum[(n >> 1) - 1] - 2 * suffSum[i]);
        System.out.println(max1+max2);
    }
}


import java.util.Scanner;

public class sample {
    static int[] prefSum;
    static int[] suffSum;
    static int[] a;
    static int[] bf;
    static int[] bw;
    static int inf = (1 << 29);
    static int n;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        a = new int[n];
        suffSum = new int[n];
        prefSum = new int[n];
        bf = new int[n];
        bw = new int[n];

        for (int i = 0; i < n; i++) {
            a[i] = in.nextInt();
            prefSum[i] = a[i] + (i == 0 ? 0 : prefSum[i - 1]);
            bf[i] = bw[i] = -inf;
        }
        for (int i = 0; i < n; i++)
            suffSum[i] = a[n - i - 1] + (i == 0 ? 0 :suffSum[i - 1]);
        if (n == 1) {
            System.out.println(Math.abs(a[0]));
            System.exit(0);
        }
        solve1(n-1);
        solve2(n-1);
        int max = 0;
        for (int x = -1; x < n; x++) {
            if (x == -1)
                max = Math.max(max, bw[n - 1]);
            else if (x == n - 1)
                max = Math.max(max, bf[n - 1]);
            else
                max = Math.max(max, bf[x] + bw[n - x - 2]);
        }
        System.out.println(max);
    }

    public static int solve1(int at) {
        if (at == 0)
            return Math.abs(a[at]);
        if (bf[at] != -inf)
            return bf[at];
        int max = -(1 << 25);
        max = Math.max(max, a[at] + solve1(at - 1));
        max = Math.max(max, -prefSum[at]);
        return bf[at] = max;
    }

    public static int solve2(int at) {
        if (at == 0)
            return Math.abs(a[n-at-1]);
        if (bw[at] != -inf)
            return bw[at];
        int max = -(1 << 25);
        max = Math.max(max, a[n-at-1] + solve2(at - 1));
        max = Math.max(max, -suffSum[at]);
        return bw[at] = max;
    }
}


import java.util.Scanner;

public class sample {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a;
        int[] prefSum;
        int[] suffSum;
        a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = in.nextInt();
        if(n==1){
            System.out.println(Math.abs(a[0]));
            System.exit(0);
        }
        prefSum = new int[((n / 2) + n % 2)];
        suffSum = new int[n / 2];
        for (int i = 0; i < prefSum.length; i++)
            prefSum[i] = a[i] + (i > 0 ? prefSum[i - 1] : 0);
        for (int i = 0; i < n / 2; i++)
            suffSum[i] = a[n - i - 1] + (i > 0 ? suffSum[i - 1] : 0);
        int max1 = prefSum[prefSum.length - 1];
        for (int i = 0; i < prefSum.length; i++)
            max1 = Math.max(max1, prefSum[prefSum.length - 1] - 2 * prefSum[i]);
        int max2 = suffSum[(n / 2) - 1];
        for (int i = 0; i < n / 2; i++)
            max2 = Math.max(max2, suffSum[(n /2) - 1] - 2 * suffSum[i]);
        System.out.println(max1 + max2);
    }
}


import java.io.*;
import java.util.*;

public class Main {
    void solve() throws IOException {
        int n = nextInt();
        int[] a = new int[n+5];
        int[] suf = new int[n+5];
        int[] pref = new int[n+5];

        for( int i = 0; i < n; i++ ){
            a[i]= nextInt();
            if( i != 0 ){
                pref[i] = pref[i-1] + a[i];
            } else {
                pref[i] = a[i];
            }
        }

        for( int i = n-1; i >= 0; i-- ){
            if( i != n-1 ){
                suf[i] = suf[i+1] + a[i];
            } else {
                suf[i] = a[i];
            }
        }

        int ans1 = pref[n-1];
        int ans2 = suf[0];

        int min = Integer.MAX_VALUE;
        int k = 0;
        for( int i = 0; i < n; i++ ){
            if( min > pref[i] ){
                min = pref[i];
                k = i;
            }
        }

        if( min < 0 ){
            ans1 -= 2*min;
        }

        min = Integer.MAX_VALUE;
        for( int i = n-1; i > k; i-- ){
            if( min > suf[i] ){
                min = suf[i];
                k = i;
            }
        }

        if( min < 0 ){
            ans1 -= 2*min;
        }

        min = Integer.MAX_VALUE;
        k = 0;
        for( int i = n-1; i >= 0; i-- ){
            if( min > suf[i] ){
                min = suf[i];
                k = i;
            }
        }

        if( min < 0 ){
            ans2 -= 2*min;
        }

        min = Integer.MAX_VALUE;
        for( int i = 0; i < k; i++ ){
            if( min > pref[i] ){
                min = pref[i];
                k = i;
            }
        }

        if( min < 0 ){
            ans2 -= 2*min;
        }

        out.println( Math.max(ans1, ans2) );
    }

    void solve2() throws IOException {
        int n = nextInt();
        int[] a = new int[n+5];
        int[] suf = new int[n+5];
        int[] pref = new int[n+5];

        for( int i = 0; i < n; i++ ){
            a[i]= nextInt();
            if( i != 0 ){
                pref[i] = pref[i-1] + a[i];
            } else {
                pref[i] = a[i];
            }
        }

        for( int i = n-1; i >= 0; i-- ){
            if( i != n-1 ){
                suf[i] = suf[i+1] + a[i];
            } else {
                suf[i] = a[i];
            }
        }

        int ans = Integer.MIN_VALUE;
        for( int i = 0; i < n; i++ ){
            for( int j = n-1; j > i; j-- ){
                ans = Math.max( pref[n-1]-2*(pref[i]+suf[j]), ans);
            }
        }

        out.println( ans );
    }

    private static final String INPUT_FILE = "inp.txt";

    public static void main(String[] args) throws IOException {
        if (new File(INPUT_FILE).exists())
            System.setIn(new FileInputStream(INPUT_FILE));
        new Main().solve2();
	out.close();
    }

    private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter out = new PrintWriter(System.out);;
    private StringTokenizer st = new StringTokenizer("");

    String nextToken() throws IOException {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    String nextLine() throws IOException {
        st = new StringTokenizer("");
        return in.readLine();
    }

}


import java.io.*;
import java.util.*;

public class Main {
    void solve() throws IOException {
        int n = nextInt();
        int[] a = new int[n+5];
        int[] suf = new int[n+5];
        int[] pref = new int[n+5];

        for( int i = 0; i < n; i++ ){
            a[i]= nextInt();
            if( i != 0 ){
                pref[i] = pref[i-1] + a[i];
            } else {
                pref[i] = a[i];
            }
        }

        for( int i = n-1; i >= 0; i-- ){
            if( i != n-1 ){
                suf[i] = suf[i+1] + a[i];
            } else {
                suf[i] = a[i];
            }
        }

        int ans1 = pref[n-1];
        int ans2 = pref[n-1];

        int min = Integer.MAX_VALUE;
        int k = 0;
        for( int i = 0; i < n; i++ ){
            if( min > pref[i] ){
                min = pref[i];
                k = i;
            }
        }

        if( min < 0 ){
            ans1 -= 2*min;
        }

        min = Integer.MAX_VALUE;
        for( int i = n-1; i > k; i-- ){
            if( min > suf[i] ){
                min = suf[i];
                k = i;
            }
        }

        if( min < 0 ){
            ans1 -= 2*min;
        }

        min = Integer.MAX_VALUE;
        k = 0;
        for( int i = n-1; i >= 0; i-- ){
            if( min > suf[i] ){
                min = suf[i];
                k = i;
            }
        }

        if( min < 0 ){
            ans2 -= 2*min;
        }

        min = Integer.MAX_VALUE;
        for( int i = 0; i < k; i++ ){
            if( min > pref[i] ){
                min = pref[i];
                k = i;
            }
        }

        if( min < 0 ){
            ans2 -= 2*min;
        }

        out.println( Math.max(ans1, ans2) );
    }

    private static final String INPUT_FILE = "inp.txt";

    public static void main(String[] args) throws IOException {
        if (new File(INPUT_FILE).exists())
            System.setIn(new FileInputStream(INPUT_FILE));
        new Main().solve();
	out.close();
    }

    private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter out = new PrintWriter(System.out);;
    private StringTokenizer st = new StringTokenizer("");

    String nextToken() throws IOException {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    String nextLine() throws IOException {
        st = new StringTokenizer("");
        return in.readLine();
    }

}


import java.io.*;
import java.util.*;

public class Main {
    void solve() throws IOException {
        int n = nextInt();
        int[] a = new int[n+5];
        int sum = 0;

        for( int i = 0; i < n; i++ ){
            a[i]= nextInt();
            sum += a[i];
        }

        int max = 0;
        int tmp = 0;
        for( int i = 0; i < n; i++ ){
            tmp += a[i];
            if( tmp < 0 ){
                tmp = 0;
            }
            max = Math.max( max, tmp );
        }

        out.println( 2*max-sum );
    }

    private static final String INPUT_FILE = "inp.txt";

    public static void main(String[] args) throws IOException {
        if (new File(INPUT_FILE).exists())
            System.setIn(new FileInputStream(INPUT_FILE));
        new Main().solve();
	out.close();
    }

    private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    private static PrintWriter out = new PrintWriter(System.out);;
    private StringTokenizer st = new StringTokenizer("");

    String nextToken() throws IOException {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextToken());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextToken());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextToken());
    }

    String nextLine() throws IOException {
        st = new StringTokenizer("");
        return in.readLine();
    }

}


#pragma comment (linker, "/STACK:16777216")
#include <ctime>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <complex>
#include <string>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair <int, int> pnt;

#define FI(i,a) for (int i=0; i<(a); ++i)
#define FOR(i,s,e) for (int i=(s); i<(e); ++i)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(),a.end()
#define V(t) vector < t >
#define sz size()

#define dout(a) cerr << a << endl
#define sout(a) cerr << a << "  "

const double pi = 3.14159265358979323846264338327950288419716939937511;
const double eps = 1e-10;
//*
char ch_ch_ch[1<<20];
inline string gs() { scanf("%s",ch_ch_ch); return string(ch_ch_ch); }
inline string gl() { gets(ch_ch_ch); return string(ch_ch_ch); }
inline int gi() { int x; scanf("%d",&x); return x; }
//*/

// code starts here

template <typename T> class FenvSum
{
private:
    V(T) fenv;
    int n;
public:
    FenvSum(int N)
    {
        n=N;
        fenv = V(T)(n+1,0.0);
    }
    void add(int p, T val)
    {
        for (++p; p<=n; p+=p&-p) fenv[p]+=val;
    }
    T sum(int p) // [0..p]
    {
        if (p<0) return 0;
        if (p>=n) p=n-1;
        T res=0;
        for (++p; p>0; p-=p&-p) res+=fenv[p];
        return res;
    }
    T rsum(int p) // [p..n-1]
    {
        return sum(n-1)-sum(p-1);
    }

};

int n;
LL k;
int a[100100];
int sa[100100];

void solution()
{
   n = gi();
   cin >> k;
   FI(i,n) sa[i]=a[i]=gi();
   FenvSum<int> lf(n),rf(n);
   sort(sa,sa+n);
   int sasz = unique(sa,sa+n)-sa;
   FI(i,n) a[i]=lower_bound(sa,sa+sasz,a[i])-sa;

   LL cur_inv = 0;
   FI(i,n-1)
   {
       cur_inv+=lf.rsum(a[i]+1);
       lf.add(a[i],1);
   }
   LL res = 0;
   int l = n-2;
   for (int r=n-1; r>0; --r)
   {
       rf.add(a[r],1);
       cur_inv += lf.rsum(a[r]+1)+rf.sum(a[r]-1);
       while (l>0 && cur_inv>k)
       {
           lf.add(a[l],-1);
           cur_inv-=rf.sum(a[l]-1)+lf.rsum(a[l]+1);
           --l;
       }
       if (cur_inv<=k) res+=l+1;
       if (r>1 && l+1==r)
       {
           lf.add(a[l],-1);
           cur_inv-=rf.sum(a[l]-1)+lf.rsum(a[l]+1);
           --l;
       }
   }

   cout << res << endl;
}

// code ends here

int main(int argc, char** argv) {

#ifdef IGEL_ACM
    freopen("in.txt","r",stdin);
    //freopen("in.txt", "w", stdout);
    clock_t beg_time = clock();
#else
    //freopen(argv[1],"r",stdin);
    //freopen("cricket.in", "r", stdin);
    //freopen("cricket.out", "w", stdout);
#endif

        solution();

#ifdef IGEL_ACM
    fprintf(stderr,"*** Time: %.3lf ***\n",1.0*(clock()-beg_time)/CLOCKS_PER_SEC);
#endif

  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <fstream>
#include <set>
#include <map>
#include <cmath>
#pragma comment(linker,"/STACK:16777216")
#define inf 1000000000
#define MP make_pair
#define PI acos(-1.0)
#define eps 1e-9
#define MAXN 100100
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
int A[MAXN+1],N,K;
int B[MAXN+1],T[MAXN+1],T_pr[MAXN+1];
map<int,int> hm;

int sum(int v){
    int say=0;
    while(v){say+=T[v]; v-=(v&(-v));}
    return say;
}

void insert(int v){
    while(v<=MAXN){T[v]++;v+=(v&(-v));}
}

void erase(int v){
    while(v<=MAXN){T[v]--;v+=(v&(-v));}
}

void insert_pr(int v){
    while(v<=MAXN){T_pr[v]++;v+=(v&(-v));}
}

int sum_pr(int v){
    int say=0;
    while(v){say+=T_pr[v]; v-=(v&(-v));}
    return say;
}

int main()
{

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

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

    sort(B,B+N);

    int nel=0;
    for(int i=0;i<N;i++)if(!hm[B[i]])hm[B[i]]=++nel;
    for(int i=0;i<N;i++)A[i]=hm[A[i]];

    i64 inv=0,ans=0;
    int r=1;

    for(int i=N-1;i;i--){
        inv+=(A[0]>A[i]);
        inv+=sum(A[i]-1);
        if(inv>K){r=i+1;inv-=(A[0]>A[i]);inv-=sum(A[i]-1);break;}
        insert(A[i]);
    }

    if(r==1)ans=(i64)N*(N-1)/2;
    else{

        ans+=(N-r);
        insert_pr(A[0]);

        for(int i=1;i<N-1;i++){
            inv+=sum_pr(MAXN)-sum_pr(A[i]);
            inv+=sum(A[i]-1);
            insert_pr(A[i]);
            for(;r<N&&inv>K;r++){
                inv-=(sum_pr(MAXN)-sum_pr(A[r]));
                inv-=(sum(A[r]-1));
                erase(A[r]);
            }
            ans+=(N-r);
        }

    }

    printf("%I64d\n",ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <fstream>
#include <set>
#include <map>
#include <cmath>
#pragma comment(linker,"/STACK:16777216")
#define inf 1000000000
#define MP make_pair
#define PI acos(-1.0)
#define eps 1e-9
#define MAXN 100100
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
int A[MAXN+1],N,K;
int B[MAXN+1],T[MAXN+1],T_pr[MAXN+1];
map<int,int> hm;

int sum(int v){
    int say=0;
    while(v){say+=T[v]; v-=(v&(-v));}
    return say;
}

void insert(int v){
    while(v<=MAXN){T[v]++;v+=(v&(-v));}
}

void erase(int v){
    while(v<=MAXN){T[v]--;v+=(v&(-v));}
}

void insert_pr(int v){
    while(v<=MAXN){T_pr[v]++;v+=(v&(-v));}
}

int sum_pr(int v){
    int say=0;
    while(v){say+=T_pr[v]; v-=(v&(-v));}
    return say;
}

int main()
{

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

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

    sort(B,B+N);

    int nel=0;
    for(int i=0;i<N;i++)if(!hm[B[i]])hm[B[i]]=++nel;
    for(int i=0;i<N;i++)A[i]=hm[A[i]];

    i64 inv=0,ans=0;
    int r=1;

    for(int i=N-1;i;i--){
        inv+=(A[0]>A[i]);
        inv+=sum(A[i]-1);
        if(inv>K){r=i+1;inv-=(A[0]>A[i]);inv-=sum(A[i]-1);break;}
        insert(A[i]);
    }

    if(r==1)ans=(i64)N*(N-1)/2;
    else{

        ans+=(N-r);
        insert_pr(A[0]);

        for(int i=1;i<N-1;i++){
            inv+=sum_pr(MAXN)-sum_pr(A[i]);
            inv+=sum(A[i]-1);
            insert_pr(A[i]);
            for(;r<N&&inv>K;r++){
                inv-=(sum_pr(MAXN)-sum_pr(A[r]));
                inv-=(sum(A[r]-1));
                erase(A[r]);
            }
            ans+=(N-r);
        }

    }

    printf("%I64d\n",ans);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <fstream>
#include <set>
#include <map>
#include <cmath>
#pragma comment(linker,"/STACK:16777216")
#define inf 1000000000
#define MP make_pair
#define PI acos(-1.0)
#define eps 1e-9
#define MAXN 100100
using namespace std;
typedef long long i64;
typedef unsigned long long u64;
int A[MAXN+1],N;
i64 K;
int B[MAXN+1],T[MAXN+1],T_pr[MAXN+1];
map<int,int> hm;

int sum(int v){
    int say=0;
    while(v){say+=T[v]; v-=(v&(-v));}
    return say;
}

void insert(int v){
    while(v<=MAXN){T[v]++;v+=(v&(-v));}
}

void erase(int v){
    while(v<=MAXN){T[v]--;v+=(v&(-v));}
}

void insert_pr(int v){
    while(v<=MAXN){T_pr[v]++;v+=(v&(-v));}
}

int sum_pr(int v){
    int say=0;
    while(v){say+=T_pr[v]; v-=(v&(-v));}
    return say;
}

int main()
{

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

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

    sort(B,B+N);

    int nel=0;
    for(int i=0;i<N;i++)if(!hm[B[i]])hm[B[i]]=++nel;
    for(int i=0;i<N;i++)A[i]=hm[A[i]];

    i64 inv=0,ans=0;
    int r=1;

    for(int i=N-1;i;i--){
        inv+=(A[0]>A[i]);
        inv+=sum(A[i]-1);
        if(inv>K){r=i+1;inv-=(A[0]>A[i]);inv-=sum(A[i]-1);break;}
        insert(A[i]);
    }

    if(r==1)ans=(i64)N*(N-1)/2;
    else{

        ans+=(N-r);
        insert_pr(A[0]);

        for(int i=1;i<N-1;i++){
            inv+=sum_pr(MAXN)-sum_pr(A[i]);
            inv+=sum(A[i]-1);
            insert_pr(A[i]);
            for(;r<N&&inv>K;r++){
                inv-=(sum_pr(MAXN)-sum_pr(A[r]));
                inv-=(sum(A[r]-1));
                erase(A[r]);
            }
            ans+=(N-r);
        }

    }

    printf("%I64d\n",ans);
    return 0;
}


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


#define LLD

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
using namespace std;
int n,m;
long long K;
int a[100020],b[100020];
int c1[100020],c2[100020];

inline void R(int *a,int x,int y)
{
	for(;x<=m;x+=x&-x)
		a[x]+=y;
}
inline int G(int *a,int x){
	int _=0;
	for(;x;x-=x&-x)
		_+=a[x];
	return _;
}
long long org,now,ans;
int main()
{
	int i,l,r,mid;
	scanf("%d%I64d",&n,&K);
	for(i=1;i<=n;i++)
		scanf("%d",a+i),b[m++]=a[i];
	sort(b,b+m);
	m=unique(b,b+m)-b;
	for(i=1;i<=n;i++)
	{
		org+=i-1-G(c1,a[i]);
		R(c1,a[i],1);
	}
	R(c1,a[n],-1);
	R(c2,a[n],1);
	memset(c1,0,sizeof c1);
	for(r=n,l=n-1;;)
	{
		while(l>0&&org>K)
		{
			org-=l-G(c1,a[l]);
			org-=G(c2,a[l]-1);
			R(c1,a[l],-1);
			l--;
		}
		if(l==0)
			break;
		ans+=l;
		r--;
		org+=l-G(c1,a[r]);
		org+=G(c2,a[r]-1);
		R(c2,a[r],1);
	}
	printf("%I64d",ans);
	return 0;
}


#include<iostream>
#include<algorithm>
#include<utility>
#include<set>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define lowbit(x) ((x)&(-(x)))
using namespace std;
typedef long long ll;
const int fin=0,maxn=100010;
int N,m; ll K;
int a[maxn],b[maxn];
int c1[maxn],c2[maxn];

inline void upd(int *a,int x,int w){
for(;x<=m;x+=lowbit(x))a[x]+=w;
}
inline int sum(int *a,int x){
int s=0;
for(;x;x-=lowbit(x))s+=a[x];
return s;
}

int main(){
ll org,now,ans=0;
int i,l,r,mid;
scanf("%d%I64d",&N,&K);
for(i=1;i<=N;++i)scanf("%d",a+i),b[i]=a[i];
sort(b+1,b+N+1);
for(m=1,i=2;i<=N;++i)
if(b[m]!=b[i])b[++m]=b[i];
for(i=1;i<=N;++i){
l=1,r=m;
while(l<r){
mid=(l+r)>>1;
if(b[mid]==a[i]){
l=mid;
break;
}
if(a[i]<b[mid])r=mid-1;else l=mid+1;
}
a[i]=l;
}
for(i=1;i<=N;++i)upd(c2,a[i],1);
for(org=0,i=1;i<=N;++i){
upd(c2,a[i],-1);
org+=sum(c1,m)-sum(c1,a[i])+sum(c2,a[i]-1);
upd(c1,a[i],1);
}
org>>=1;
for(i=1;i<=m;++i)c1[i]=c2[i]=0;
upd(c1,a[1],1);
for(i=2;i<=N;++i)upd(c2,a[i],1);
for(now=org,r=1,l=2;l<=N;++l){
if(r<l-1){
upd(c2,a[++r],-1);
now-=(sum(c1,m)-sum(c1,a[r])+sum(c2,a[r]-1));
}
while(r<N&&now>K){
upd(c2,a[++r],-1);
now-=(sum(c1,m)-sum(c1,a[r])+sum(c2,a[r]-1));
}
if(r==N)break;
ans+=N-r;
now+=sum(c1,m)-sum(c1,a[l])+sum(c2,a[l]-1);
upd(c1,a[l],1);
}
printf("%I64d\n",ans);
return 0;
}


#define LLD

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<vector>
using namespace std;
int n,m;
long long K;
int a[100020],b[100020];
int c1[100020],c2[100020];

inline void R(int *a,int x,int y)
{
	for(;x<=m;x+=x&-x)
		a[x]+=y;
}
inline int G(int *a,int x){
	int _=0;
	for(;x;x-=x&-x)
		_+=a[x];
	return _;
}
long long org,now,ans;
int main()
{
	int i,l,r,mid;
	scanf("%d%I64d",&n,&K);
	for(i=1;i<=n;i++)
		scanf("%d",a+i),b[m++]=a[i];
	sort(b,b+m);
	m=unique(b,b+m)-b;
	for(i=1;i<=n;i++)
	{
		org+=i-1-G(c1,a[i]-1);
		R(c1,a[i],1);
	}
	memset(c1,0,sizeof c1);
	for(now=org,r=1,l=2;l<=n;++l)
	{
		if(r<l-1){
		R(c2,a[++r],-1);
		now-=(G(c1,m)-G(c1,a[r])+G(c2,a[r]-1));
		}
		while(r<n&&now>K){
		R(c2,a[++r],-1);
		now-=(G(c1,m)-G(c1,a[r])+G(c2,a[r]-1));
		}
		if(r==n)break;
		ans+=n-r;
		now+=G(c1,m)-G(c1,a[l])+G(c2,a[l]-1);
		R(c1,a[l],1);
	}
	printf("%I64d",ans);
	return 0;
}


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


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LOL;
const int N = 100010;
int a[N],b[N],c1[N],c2[N];
int lowbit(int x){
    return x&(-x);
}
int len=0;
int bin(int x)
{
    int left=1,right=len;
    while(left<=right){
        int mid=(left+right)>>1;
        if(b[mid]==x) return mid;
        else if(b[mid]<x) left=mid+1;
        else right=mid-1;
    }
    return -1;
}
void update(int c[],int x,int v){
    while(x<=len){
        c[x]+=v;
        x+=lowbit(x);
    }
}
int get(int c[],int x)
{
    int s=0;
    while(x>0) s+=c[x],x-=lowbit(x);
    return s;
}
int main(void)
{
    LOL m;
    int n;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    len=1;
    for(int i=2;i<=n;i++){
        if(b[i]!=b[i-1]) b[++len]=b[i];
    }
    for(int i=1;i<=n;i++) a[i]=bin(a[i]);
    LOL sum=0;
    LOL tt=0;
    for(int i=n;i>=2;i--){
        update(c2,a[i],1);
        tt+=get(c2,a[i]-1);
    }
    int j=2;
    tt+=get(c2,a[1]-1);
    for(int i=1;i<n;i++){
        update(c1,a[i],1);
        tt+=get(c1,len)-get(c1,a[i]);
        tt+=get(c2,a[i]-1);
        while(tt>m&&j<=n){
            tt-=get(c1,len)-get(c1,a[j]);
            tt-=get(c2,a[j]-1);
            update(c2,a[j],-1);
            j++;
        }
        if(j>n){
            break;
        }else if(j<=i+1){
            sum+=(n-i)*(n-i+1)/2;
            break;
        }else{
            sum+=n-j+1;
        }
    }
    cout<<sum<<endl;
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LOL;
const int N = 100010;
int a[N],b[N],c1[N],c2[N];
int lowbit(int x){
    return x&(-x);
}
int len=0;
int bin(int x)
{
    int left=1,right=len;
    while(left<=right){
        int mid=(left+right)>>1;
        if(b[mid]==x) return mid;
        else if(b[mid]<x) left=mid+1;
        else right=mid-1;
    }
    return -1;
}
void update(int c[],int x,int v){
    while(x<=len){
        c[x]+=v;
        x+=lowbit(x);
    }
}
int get(int c[],int x)
{
    int s=0;
    while(x>0) s+=c[x],x-=lowbit(x);
    return s;
}
int main(void)
{
    LOL m;
    int n;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    len=1;
    for(int i=2;i<=n;i++){
        if(b[i]!=b[i-1]) b[++len]=b[i];
    }
    for(int i=1;i<=n;i++) a[i]=bin(a[i]);
    LOL sum=0;
    LOL tt=0;
    for(int i=n;i>=2;i--){
        update(c2,a[i],1);
        tt+=get(c2,a[i]-1);
    }
    int j=2;
    for(int i=1;i<n;i++){
        update(c1,a[i],1);
        tt+=get(c1,len)-get(c1,a[i]);
        tt+=get(c2,a[i]-1);
        while(tt>m&&j<=n){
            tt-=get(c1,len)-get(c1,a[j]);
            tt-=get(c2,a[j]-1);
            update(c2,a[j],-1);
            j++;
        }
        if(j>n){
            break;
        }else if(j<=i+1){
            sum+=(n-i)*(n-i+1)/2;
            break;
        }else{
            sum+=n-j+1;
        }
    }
    cout<<sum<<endl;
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LOL;
const int N = 100010;
int a[N],b[N],c1[N],c2[N];
int lowbit(int x){
    return x&(-x);
}
int len=0;
int bin(int x)
{
    int left=1,right=len;
    while(left<=right){
        int mid=(left+right)>>1;
        if(b[mid]==x) return mid;
        else if(b[mid]<x) left=mid+1;
        else right=mid-1;
    }
    return -1;
}
void update(int c[],int x,int v){
    while(x<=len){
        c[x]+=v;
        x+=lowbit(x);
    }
}
LOL get(int c[],int x)
{
    LOL s=0;
    while(x>0) s+=c[x],x-=lowbit(x);
    return s;
}
int main(void)
{
    LOL m;
    int n;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    len=1;
    for(int i=2;i<=n;i++){
        if(b[i]!=b[i-1]) b[++len]=b[i];
    }
    for(int i=1;i<=n;i++) a[i]=bin(a[i]);
    LOL sum=0;
    LOL tt=0;
    for(int i=n;i>=2;i--){
        update(c2,a[i],1);
        tt+=get(c2,a[i]-1);
    }
    int j=2;
    for(int i=1;i<n;i++){
        update(c1,a[i],1);
        tt+=get(c1,len)-get(c1,a[i]);
        tt+=get(c2,a[i]-1);
        while(tt>m&&j<=n){
            tt-=get(c1,len)-get(c1,a[j]);
            tt-=get(c2,a[j]-1);
            update(c2,a[j],-1);
            j++;
        }
        if(j>n){
            break;
        }else if(j<=i+1){
            sum+=(LOL)(n-i)*(LOL)(n-i+1)/2;
            break;
        }else{
            sum+=n-j+1;
        }
    }
    cout<<sum<<endl;
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LOL;
const int N = 100010;
int a[N],b[N],c1[N],c2[N];
int lowbit(int x){
    return x&(-x);
}
int len=0;
int bin(int x)
{
    int left=1,right=len;
    while(left<=right){
        int mid=(left+right)>>1;
        if(b[mid]==x) return mid;
        else if(b[mid]<x) left=mid+1;
        else right=mid-1;
    }
    return -1;
}
void update(int c[],int x,int v){
    while(x<=len){
        c[x]+=v;
        x+=lowbit(x);
    }
}
LOL get(int c[],int x)
{
    LOL s=0;
    while(x>0) s+=c[x],x-=lowbit(x);
    return s;
}
int main(void)
{
    LOL m;
    int n;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    len=1;
    for(int i=2;i<=n;i++){
        if(b[i]!=b[i-1]) b[++len]=b[i];
    }
    for(int i=1;i<=n;i++) a[i]=bin(a[i]);
    LOL sum=0;
    LOL tt=0;
    for(int i=n;i>=2;i--){
        update(c2,a[i],1);
        tt+=get(c2,a[i]-1);
    }
    int j=2;
    for(int i=1;i<n;i++){
        update(c1,a[i],1);
        tt+=get(c1,len)-get(c1,a[i]);
        tt+=get(c2,a[i]-1);
        while(tt>m&&j<=n){
            tt-=get(c1,len)-get(c1,a[j]);
            tt-=get(c2,a[j]-1);
            update(c2,a[j],-1);
            j++;
        }
        if(j>n){
            break;
        }else if(j<=i+1){
            sum+=(n-i)*(n-i+1)/2;
            break;
        }else{
            sum+=n-j+1;
        }
    }
    cout<<sum<<endl;
    return 0;
}


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

const int N = 110000;

int n, x[N], aib1[N], aib2[N], vmax;
long long rez, k, inv;
pair<int, int> t[N];

void add(int aib[], int poz, int val) {
	for(; poz <= n; poz += poz&(-poz))
		aib[poz] += val;
}

inline int sum(int aib[], int poz) {
	int r = 0;

	for(; poz; poz -= poz & (-poz))
		r += aib[poz];
	return r;
}

int q(int aib[], int pozx, int pozy) {
	return sum(aib, pozy) - sum(aib, pozx - 1);
}

int main() {
	int i, r, l;

	cin >> n >> k;

	for(i = 1; i<=n; ++i)
		cin >> x[i], t[i].first = x[i], t[i].second = i;

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

	for(i = 1; i<=n; ++i)
		x[t[i].second] = (t[i - 1].first == t[i].first) ? x[t[i - 1].second] : x[t[i - 1].second] + 1,
		vmax = max(vmax, x[t[i].second]);

	r = n - 1;

	for(i = 1; i<=r; ++i) {
		add(aib1, x[i], 1);

		inv = inv + q(aib1, x[i] + 1, vmax);
	}

	for(l = n; l > 1; --l) {

		inv = inv + q(aib1, x[l] + 1, vmax) + q(aib2, 1, x[l] - 1);

		add(aib2, x[l], 1);

		while((r && inv > k) || r>=l) {
			add(aib1, x[r], -1);

			inv = inv - q(aib1, x[r] + 1, vmax) - q(aib2, 1, x[r] - 1);

			--r;
		}

		if(!r)
			break;

		rez += r;
	}

	cout << rez;

	return 0;
}


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

const int N = 110000;

int n, k, x[N], aib1[N], aib2[N], inv, vmax;
long long rez;
pair<int, int> t[N];

void add(int aib[], int poz, int val) {
	for(; poz <= n; poz += poz&(-poz))
		aib[poz] += val;
}

inline int sum(int aib[], int poz) {
	int r = 0;

	for(; poz; poz -= poz & (-poz))
		r += aib[poz];
	return r;
}

int q(int aib[], int pozx, int pozy) {
	return sum(aib, pozy) - sum(aib, pozx - 1);
}

int main() {
	int i, r, l;

	cin >> n >> k;

	for(i = 1; i<=n; ++i)
		cin >> x[i], t[i].first = x[i], t[i].second = i;

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

	for(i = 1; i<=n; ++i)
		x[t[i].second] = (t[i - 1].first == t[i].first) ? x[t[i - 1].second] : x[t[i - 1].second] + 1,
		vmax = max(vmax, x[t[i].second]);

	r = n - 1;

	for(i = 1; i<=r; ++i) {
		add(aib1, x[i], 1);

		inv += q(aib1, x[i] + 1, vmax);
	}

	for(l = n; l > 1; --l) {

		inv += q(aib1, x[l] + 1, vmax) + q(aib2, 1, x[l] - 1);

		add(aib2, x[l], 1);

		while(r && inv >= k) {
			add(aib1, x[r], -1);

			inv = inv - q(aib1, x[r] + 1, vmax) - q(aib2, 1, x[r] - 1);

			--r;
		}

		if(!r)
			break;

		rez += r;
	}

	cout << rez;

	return 0;
}


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

const int N = 110000;

int n, k, x[N], aib1[N], aib2[N], inv, vmax;
long long rez;
pair<int, int> t[N];

void add(int aib[], int poz, int val) {
	for(; poz <= n; poz += poz&(-poz))
		aib[poz] += val;
}

inline int sum(int aib[], int poz) {
	int r = 0;

	for(; poz; poz -= poz & (-poz))
		r += aib[poz];
	return r;
}

int q(int aib[], int pozx, int pozy) {
	return sum(aib, pozy) - sum(aib, pozx - 1);
}

int main() {
	int i, r, l;

	cin >> n >> k;

	for(i = 1; i<=n; ++i)
		cin >> x[i], t[i].first = x[i], t[i].second = i;

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

	for(i = 1; i<=n; ++i)
		x[t[i].second] = (t[i - 1].first == t[i].first) ? x[t[i - 1].second] : x[t[i - 1].second] + 1,
		vmax = max(vmax, x[t[i].second]);

	r = n - 1;

	for(i = 1; i<=r; ++i) {
		add(aib1, x[i], 1);

		inv += q(aib1, x[i] + 1, vmax);
	}

	for(l = n; l > 1; --l) {

		inv += q(aib1, x[l] + 1, vmax) + q(aib2, 1, x[l] - 1);

		add(aib2, x[l], 1);

		while(r && inv > k) {
			add(aib1, x[r], -1);

			inv = inv - q(aib1, x[r] + 1, vmax) - q(aib2, 1, x[r] - 1);

			--r;
		}

		if(!r)
			break;

		rez += r;
	}

	cout << rez;

	return 0;
}


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

const int N = 110000;

int n, k, x[N], aib1[N], aib2[N], inv, vmax;
long long rez;
pair<int, int> t[N];

void add(int aib[], int poz, int val) {
	for(; poz <= n; poz += poz&(-poz))
		aib[poz] += val;
}

inline int sum(int aib[], int poz) {
	int r = 0;

	for(; poz; poz -= poz & (-poz))
		r += aib[poz];
	return r;
}

int q(int aib[], int pozx, int pozy) {
	return sum(aib, pozy) - sum(aib, pozx - 1);
}

int main() {
	int i, r, l;

	cin >> n >> k;

	for(i = 1; i<=n; ++i)
		cin >> x[i], t[i].first = x[i], t[i].second = i;

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

	for(i = 1; i<=n; ++i)
		x[t[i].second] = (t[i - 1].first == t[i].first) ? x[t[i - 1].second] : x[t[i - 1].second] + 1,
		vmax = max(vmax, x[t[i].second]);

	r = n - 1;

	for(i = 1; i<=r; ++i) {
		add(aib1, x[i], 1);

		inv += q(aib1, x[i] + 1, vmax);
	}

	for(l = n; l > 1; --l) {

		inv += q(aib1, x[l] + 1, vmax) + q(aib2, 1, x[l] - 1);

		add(aib2, x[l], 1);

		while((r && inv > k) || r>=l) {
			add(aib1, x[r], -1);

			inv = inv - q(aib1, x[r] + 1, vmax) - q(aib2, 1, x[r] - 1);

			--r;
		}

		if(!r)
			break;

		rez += r;
	}

	cout << rez;

	return 0;
}


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

typedef long long LL;

LL getint()
{
	LL res=0;
	while (1)
	{
		char ch=getchar(); if (ch<'0' || ch>'9') break;
		res=res*10+ch-48;
	}
	return res;
}

struct splaytree
{
	struct node
	{
		node *left, *right, *parent;
		int size, dt, count;
		node(int _dt, node *_parent) { left=right=NULL; parent=_parent; size=1; count=1; dt=_dt; }
	};

	node *root;
	splaytree() { root=NULL; }

	void update(node *x)
	{
		x->size=(x->left?x->left->size:0)+(x->right?x->right->size:0)+x->count;
	}

	void tl(node *x)
	{
		node *y=x->parent; x->parent=y->parent;
		if (y->parent) if (y==y->parent->left) y->parent->left=x; else y->parent->right=x;
		y->right=x->left; if (x->left) x->left->parent=y;
		x->left=y; y->parent=x; update(y);
	}

	void tr(node *x)
	{
		node *y=x->parent; x->parent=y->parent;
		if (y->parent) if (y==y->parent->left) y->parent->left=x; else y->parent->right=x;
		y->left=x->right; if (x->right) x->right->parent=y;
		x->right=y; y->parent=x; update(y);
	}

	void splay(node *x, node *y)
	{
		while (x->parent!=y)
			if (x->parent->parent==y)
				if (x==x->parent->left) tr(x); else tl(x);
			else  if (x->parent==x->parent->parent->left)
					if (x==x->parent->left) tr(x->parent),tr(x); else tl(x),tr(x);
				else  if (x==x->parent->right) tl(x->parent),tl(x); else tr(x),tl(x);
		update(x); if (!y) root=x;
	}

	void insert(int dt)
	{
		if (!root) { root=new node(dt,NULL); return; }
		node *cur=root;
		while (1)
			if (dt==cur->dt) { cur->count++; break; }
			else  if (dt>cur->dt)
					if (cur->right) cur=cur->right; else { cur->right=new node(dt,cur); cur=cur->right; break; }
				else  if (cur->left) cur=cur->left; else { cur->left=new node(dt,cur); cur=cur->left; break; }
		node *temp=cur;
		while (cur) update(cur), cur=cur->parent;
		splay(temp,NULL);
	}

	node *find(int dt)
	{
		node *cur=root;
		while (cur)
			if (dt==cur->dt)
			{
				splay(cur,NULL);
				return cur;
			}
			else  if (dt>cur->dt) cur=cur->right; else cur=cur->left;
	}

	void remove(node *p)
	{
		splay(p,NULL);
		if (p->count>1)
		{
			p->count--; update(root);
			return;
		}
		if (!p->left && !p->right)
		{
			root=NULL;
			return;
		}
		else  if (!p->left)
		{
			node *succ=p->right; while (succ->left) succ=succ->left;
			splay(succ,NULL); root->left=NULL; update(root);
		}
		else  if (!p->right)
		{
			node *prev=p->left; while (prev->right) prev=prev->right;
			splay(prev,NULL); root->right=NULL; update(root);
		}
		else
		{
			node *prev=p->left; while (prev->right) prev=prev->right;
			node *succ=p->right; while (succ->left) succ=succ->left;
			splay(prev,NULL); splay(succ,prev); root->right->left=NULL;
			update(root->right); update(root);
		}
	}
};

splaytree t;
int d[100010];

void lemon()
{
	int n=getint(); LL kallow=getint();
	for (int i=1; i<=n; i++) d[i]=getint();
	int left=1; LL count=0, final=0;
	for (int right=1; right<=n; right++)
	{
		t.insert(d[right]); count+=(t.root->right?t.root->right->size:0);
		while (count>kallow)
		{
			t.find(d[left]);
			count-=(t.root->left?t.root->left->size:0);
			t.remove(t.root);
		 	left++;
		}
		final+=right-left;
	}
	printf("%I64d\n",final);
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("220E.in","r",stdin);
	#endif
	lemon();
	return 0;
}


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

typedef long long LL;

LL getint()
{
	LL res=0;
	while (1)
	{
		char ch=getchar(); if (ch<'0' || ch>'9') break;
		res=res*10+ch-48;
	}
	return res;
}

struct splaytree
{
	struct node
	{
		node *left, *right, *parent;
		int size, dt, count;
		node(int _dt, node *_parent) { left=right=NULL; parent=_parent; size=1; count=1; dt=_dt; }
	};

	node *root;
	splaytree() { root=NULL; }

	void update(node *x)
	{
		x->size=(x->left?x->left->size:0)+(x->right?x->right->size:0)+x->count;
	}

	void tl(node *x)
	{
		node *y=x->parent; x->parent=y->parent;
		if (y->parent) if (y==y->parent->left) y->parent->left=x; else y->parent->right=x;
		y->right=x->left; if (x->left) x->left->parent=y;
		x->left=y; y->parent=x; update(y);
	}

	void tr(node *x)
	{
		node *y=x->parent; x->parent=y->parent;
		if (y->parent) if (y==y->parent->left) y->parent->left=x; else y->parent->right=x;
		y->left=x->right; if (x->right) x->right->parent=y;
		x->right=y; y->parent=x; update(y);
	}

	void splay(node *x, node *y)
	{
		while (x->parent!=y)
			if (x->parent->parent==y)
				if (x==x->parent->left) tr(x); else tl(x);
			else  if (x->parent==x->parent->parent->left)
					if (x==x->parent->left) tr(x->parent),tr(x); else tl(x),tr(x);
				else  if (x==x->parent->right) tl(x->parent),tl(x); else tr(x),tl(x);
		update(x); if (!y) root=x;
	}

	void insert(int dt)
	{
		if (!root) { root=new node(dt,NULL); return; }
		node *cur=root;
		while (1)
			if (dt==cur->dt) { cur->count++; break; }
			else  if (dt>cur->dt)
					if (cur->right) cur=cur->right; else { cur->right=new node(dt,cur); cur=cur->right; break; }
				else  if (cur->left) cur=cur->left; else { cur->left=new node(dt,cur); cur=cur->left; break; }
		node *temp=cur;
		while (cur) update(cur), cur=cur->parent;
		splay(temp,NULL);
	}

	node *find(int dt)
	{
		node *cur=root;
		while (cur)
			if (dt==cur->dt)
			{
				splay(cur,NULL);
				return cur;
			}
			else  if (dt>cur->dt) cur=cur->right; else cur=cur->left;
	}

	void remove(int dt)
	{
		node *p=find(dt);
		if (p->count>1)
		{
			p->count--; update(root);
			return;
		}
		if (!p->left && !p->right)
		{
			root=NULL;
			return;
		}
		else  if (!p->left)
		{
			node *succ=p->right; while (succ->left) succ=succ->left;
			splay(succ,NULL); root->left=NULL; update(root);
		}
		else  if (!p->right)
		{
			node *prev=p->left; while (prev->right) prev=prev->right;
			splay(prev,NULL); root->right=NULL; update(root);
		}
		else
		{
			node *prev=p->left; while (prev->right) prev=prev->right;
			node *succ=p->right; while (succ->left) succ=succ->left;
			splay(prev,NULL); splay(succ,prev); root->right->left=NULL;
			update(root->right); update(root);
		}
	}
};

splaytree t1,t2;
int d[100010];

void lemon()
{
	int n=getint(); LL kallow=getint();
	for (int i=1; i<=n; i++) d[i]=getint();
	int left=1; LL count=0, final=0;
	for (int i=1; i<=n; i++)
	{
		t2.insert(d[i]);
		count+=(t2.root->right?t2.root->right->size:0);
	}
	t2.remove(d[1]); t1.insert(d[1]);
	for (int right=2; right<=n-1; right++)
	{
		t2.find(d[right]); count-=(t2.root->left?t2.root->left->size:0); t2.remove(d[right]);
		t1.insert(d[right]); count-=(t1.root->right?t1.root->right->size:0); t1.remove(d[right]);
		while (left<right && count<=kallow)
		{
		 	left++;
			t1.insert(d[left]); count+=(t1.root->right?t1.root->right->size:0);
			t2.insert(d[left]); count+=(t2.root->left?t2.root->left->size:0); t2.remove(d[left]);
		}
		final+=left-1;
		//printf("%d %d\n",left,right);
	}
	printf("%I64d\n",final);
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("220E.in","r",stdin);
	#endif
	lemon();
	return 0;
}


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

typedef long long LL;

LL getint()
{
	LL res=0;
	while (1)
	{
		char ch=getchar(); if (ch<'0' || ch>'9') break;
		res=res*10+ch-48;
	}
	return res;
}

struct splaytree
{
	struct node
	{
		node *left, *right, *parent;
		int size, dt, count;
		node(int _dt, node *_parent) { left=right=NULL; parent=_parent; size=1; count=1; dt=_dt; }
	};

	node *root;
	splaytree() { root=NULL; }

	void update(node *x)
	{
		x->size=(x->left?x->left->size:0)+(x->right?x->right->size:0)+x->count;
	}

	void tl(node *x)
	{
		node *y=x->parent; x->parent=y->parent;
		if (y->parent) if (y==y->parent->left) y->parent->left=x; else y->parent->right=x;
		y->right=x->left; if (x->left) x->left->parent=y;
		x->left=y; y->parent=x; update(y);
	}

	void tr(node *x)
	{
		node *y=x->parent; x->parent=y->parent;
		if (y->parent) if (y==y->parent->left) y->parent->left=x; else y->parent->right=x;
		y->left=x->right; if (x->right) x->right->parent=y;
		x->right=y; y->parent=x; update(y);
	}

	void splay(node *x, node *y)
	{
		while (x->parent!=y)
			if (x->parent->parent==y)
				if (x==x->parent->left) tr(x); else tl(x);
			else  if (x->parent==x->parent->parent->left)
					if (x==x->parent->left) tr(x->parent),tr(x); else tl(x),tr(x);
				else  if (x==x->parent->right) tl(x->parent),tl(x); else tr(x),tl(x);
		update(x); if (!y) root=x;
	}

	void insert(int dt)
	{
		if (!root) { root=new node(dt,NULL); return; }
		node *cur=root;
		while (1)
			if (dt==cur->dt) { cur->count++; break; }
			else  if (dt>cur->dt)
					if (cur->right) cur=cur->right; else { cur->right=new node(dt,cur); cur=cur->right; break; }
				else  if (cur->left) cur=cur->left; else { cur->left=new node(dt,cur); cur=cur->left; break; }
		node *temp=cur;
		while (cur) update(cur), cur=cur->parent;
		splay(temp,NULL);
	}

	node *find(int dt)
	{
		node *cur=root;
		while (cur)
			if (dt==cur->dt)
			{
				splay(cur,NULL);
				return cur;
			}
			else  if (dt>cur->dt) cur=cur->right; else cur=cur->left;
	}

	void remove(int dt)
	{
		node *p=find(dt);
		if (p->count>1)
		{
			p->count--; update(root);
			return;
		}
		if (!p->left && !p->right)
		{
			root=NULL;
			return;
		}
		else  if (!p->left)
		{
			node *succ=p->right; while (succ->left) succ=succ->left;
			splay(succ,NULL); root->left=NULL; update(root);
		}
		else  if (!p->right)
		{
			node *prev=p->left; while (prev->right) prev=prev->right;
			splay(prev,NULL); root->right=NULL; update(root);
		}
		else
		{
			node *prev=p->left; while (prev->right) prev=prev->right;
			node *succ=p->right; while (succ->left) succ=succ->left;
			splay(prev,NULL); splay(succ,prev); root->right->left=NULL;
			update(root->right); update(root);
		}
	}
};

splaytree t1,t2;
int d[100010];

void lemon()
{
	int n=getint(); LL kallow=getint();
	for (int i=1; i<=n; i++) d[i]=getint();
	int left=0; LL count=0, final=0;
	for (int i=1; i<=n; i++)
	{
		t2.insert(d[i]);
		count+=(t2.root->right?t2.root->right->size:0);
	}
	for (int right=1; right<=n-1; right++)
	{
		t2.find(d[right]); count-=(t2.root->left?t2.root->left->size:0); t2.remove(d[right]);
		t1.insert(d[right]); count-=(t1.root->right?t1.root->right->size:0); t1.remove(d[right]);
		while (left<=right && count<=kallow)
		{
		 	left++;
			t1.insert(d[left]); count+=(t1.root->right?t1.root->right->size:0);
			t2.insert(d[left]); count+=(t2.root->left?t2.root->left->size:0); t2.remove(d[left]);
		}
		if (left>0) final+=left-1;
		//printf("%d %d\n",left,right);
	}
	printf("%I64d\n",final);
}

int main()
{
	#ifndef ONLINE_JUDGE
		freopen("220E.in","r",stdin);
	#endif
	lemon();
	return 0;
}


#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long lld;

const int maxm = 100100;

class segTree {
public:
	lld stor[maxm*4];
	void mod(lld m, int l, int ll=0, int rr=maxm, int idx=1) {
		int r = l+1;
		if (rr <= l || r <= ll) return;
		else if(l <= ll && rr <= r) {
			stor[idx] += m;
		}
		else {
			int mid = (ll+rr)/2;
			mod(m, l, ll, mid, idx*2);
			mod(m, l, mid, rr, (idx*2)+1);
			stor[idx] = stor[idx*2] + stor[(idx*2)+1];
		}
	}
	lld query(int l, int r, int ll=0, int rr=maxm, int idx=1) {
		if (rr <= l || r <= ll) return 0;
		else if(l <= ll && rr <= r) {
			return stor[idx];
		}
		else {
			int mid = (ll+rr)/2;
			lld ret = 0;
			ret += query(l, r, ll, mid, idx*2);
			ret += query(l, r, mid, rr, (idx*2)+1);
			return ret;
		}
	}
}top, bot;

map<int, int> dict;
vector<int> sorted;

lld n, k, cur, total, sum, in[maxm];

int main() {
	scanf("%lld%lld", &n, &k);
	for (int i=0; i<n; i++) {
		scanf("%lld", &in[i]);
		sorted.push_back(in[i]);
	}
	sort(sorted.begin(), sorted.end());
	sorted.resize(unique(sorted.begin(), sorted.end())-sorted.begin());
	for (int i=0; i<sorted.size(); i++) {
		dict[sorted[i]] = i;
	}
	for (int i=0; i<n; i++) {
		in[i] = dict[in[i]];
		top.mod(1, in[i]);
		total += top.query(in[i]+1, maxm);
	}
	if (total <= k) {
		printf("%lld\n", (n*(n-1))/2);
		return 0;
	}
	top.mod(-1, in[0]);
	bot.mod(1, in[0]);
	int ed = 1;
	for (int st=1; st<n-1; st++) {
		ed = max(ed, st);
		lld q;
		//printf("%lld\n", bot.query(in[ed]+1, maxm) + top.query(0, in[ed]));
		while (ed != n && total - cur - (q = bot.query(in[ed]+1, maxm) + top.query(0, in[ed])) > k) {
			cur += q;
			top.mod(-1, in[ed]);
			ed++;
			//printf("%lld\n", bot.query(in[ed]+1, maxm) + top.query(0, in[ed]));
		}
		//printf("%d to %d\n", st, ed);
		sum += n-ed-1;
		if (cur) cur = cur - (bot.query(in[st]+1, maxm) + top.query(0, in[st]));
		bot.mod(1, in[st]);
	}
	printf("%lld", sum);
	return 0;
}


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

using namespace std;

typedef long long lld;

const int maxm = 100100;

class segTree {
public:
    lld stor[maxm*4];
    void mod(lld m, int l, int ll=0, int rr=maxm, int idx=1) {
        int r = l+1;
        if (rr <= l || r <= ll) return;
        else if(l <= ll && rr <= r) {
            stor[idx] += m;
        }
        else {
            int mid = (ll+rr)/2;
            mod(m, l, ll, mid, idx*2);
            mod(m, l, mid, rr, (idx*2)+1);
            stor[idx] = stor[idx*2] + stor[(idx*2)+1];
        }
    }
    lld query(int l, int r, int ll=0, int rr=maxm, int idx=1) {
        if (rr <= l || r <= ll) return 0;
        else if(l <= ll && rr <= r) {
            return stor[idx];
        }
        else {
            int mid = (ll+rr)/2;
            lld ret = 0;
            ret += query(l, r, ll, mid, idx*2);
            ret += query(l, r, mid, rr, (idx*2)+1);
            return ret;
        }
    }
}top, bot;

map<int, int> dict;
vector<int> sorted;

lld n, k, cur, total, sum, in[maxm];

int main() {
    cin >> n >> k;
    for (int i=0; i<n; i++) {
        cin >> in[i];
        in[i] = 1;
        sorted.push_back(in[i]);
    }
    sort(sorted.begin(), sorted.end());
    sorted.resize(unique(sorted.begin(), sorted.end())-sorted.begin());
    for (int i=0; i<sorted.size(); i++) {
        dict[sorted[i]] = i;
    }
    for (int i=0; i<n; i++) {
        in[i] = dict[in[i]];
        top.mod(1, in[i]);
        total += top.query(in[i]+1, maxm);
    }
    if (total <= k) {
        cout << (n*(n-1))/2LL;
        return 0;
    }
    top.mod(-1, in[0]);
    bot.mod(1, in[0]);
    lld ed = 1;
    for (lld st=1; st<n-1; st++) {
        ed = max(ed, st);
        lld q;
        //printf("%lld\n", bot.query(in[ed]+1, maxm) + top.query(0, in[ed]));
        while (ed != n-1 && total - cur - (q = bot.query(in[ed]+1, maxm) + top.query(0, in[ed])) > k) {
            cur += q;
            top.mod(-1, in[ed]);
            ed++;
            //printf("%lld\n", bot.query(in[ed]+1, maxm) + top.query(0, in[ed]));
        }
        //printf("%d to %d\n", st, ed);
        if (total-cur-(bot.query(in[ed]+1, maxm) + top.query(0, in[ed])) <= k) sum += n-ed-1;
        if (cur) cur = cur - (bot.query(in[st]+1, maxm) + top.query(0, in[st]));
        bot.mod(1, in[st]);
    }
    cout << sum;
    return 0;
}


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

using namespace std;

typedef long long lld;

const int maxm = 100100;

class segTree {
public:
    lld stor[maxm*4];
    void mod(lld m, int l, int ll=0, int rr=maxm, int idx=1) {
        int r = l+1;
        if (rr <= l || r <= ll) return;
        else if(l <= ll && rr <= r) {
            stor[idx] += m;
        }
        else {
            int mid = (ll+rr)/2;
            mod(m, l, ll, mid, idx*2);
            mod(m, l, mid, rr, (idx*2)+1);
            stor[idx] = stor[idx*2] + stor[(idx*2)+1];
        }
    }
    lld query(int l, int r, int ll=0, int rr=maxm, int idx=1) {
        if (rr <= l || r <= ll) return 0;
        else if(l <= ll && rr <= r) {
            return stor[idx];
        }
        else {
            int mid = (ll+rr)/2;
            lld ret = 0;
            ret += query(l, r, ll, mid, idx*2);
            ret += query(l, r, mid, rr, (idx*2)+1);
            return ret;
        }
    }
}top, bot;

map<int, int> dict;
vector<int> sorted;

lld n, k, cur, total, sum, in[maxm];

int main() {
    cin >> n >> k;
    for (int i=0; i<n; i++) {
        cin >> in[i];
        sorted.push_back(in[i]);
    }
    sort(sorted.begin(), sorted.end());
    sorted.resize(unique(sorted.begin(), sorted.end())-sorted.begin());
    for (int i=0; i<sorted.size(); i++) {
        dict[sorted[i]] = i;
    }
    for (int i=0; i<n; i++) {
        in[i] = dict[in[i]];
        top.mod(1, in[i]);
        total += top.query(in[i]+1, maxm);
    }
    if (total <= k) {
        cout << (n*(n-1))/2LL;
        return 0;
    }
    top.mod(-1, in[0]);
    bot.mod(1, in[0]);
    lld ed = 1;
    for (lld st=1; st<n-1; st++) {
        ed = max(ed, st);
        lld q;
        //printf("%lld\n", bot.query(in[ed]+1, maxm) + top.query(0, in[ed]));
        while (ed != n-1 && total - cur - (q = bot.query(in[ed]+1, maxm) + top.query(0, in[ed])) > k) {
            cur += q;
            top.mod(-1, in[ed]);
            ed++;
            //printf("%lld\n", bot.query(in[ed]+1, maxm) + top.query(0, in[ed]));
        }
        //printf("%d to %d\n", st, ed);
        if (total-cur-(bot.query(in[ed]+1, maxm) + top.query(0, in[ed])) <= k) sum += n-ed-1;
        if (cur) cur = cur - (bot.query(in[st]+1, maxm) + top.query(0, in[st]));
        bot.mod(1, in[st]);
    }
    cout << sum;
    return 0;
}


#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long lld;

const int maxm = 100100;

class segTree {
public:
    lld stor[maxm*4];
    void mod(lld m, int l, int ll=0, int rr=maxm, int idx=1) {
        int r = l+1;
        if (rr <= l || r <= ll) return;
        else if(l <= ll && rr <= r) {
            stor[idx] += m;
        }
        else {
            int mid = (ll+rr)/2;
            mod(m, l, ll, mid, idx*2);
            mod(m, l, mid, rr, (idx*2)+1);
            stor[idx] = stor[idx*2] + stor[(idx*2)+1];
        }
    }
    lld query(int l, int r, int ll=0, int rr=maxm, int idx=1) {
        if (rr <= l || r <= ll) return 0;
        else if(l <= ll && rr <= r) {
            return stor[idx];
        }
        else {
            int mid = (ll+rr)/2;
            lld ret = 0;
            ret += query(l, r, ll, mid, idx*2);
            ret += query(l, r, mid, rr, (idx*2)+1);
            return ret;
        }
    }
}top, bot;

map<int, int> dict;
vector<int> sorted;

lld n, k, cur, total, sum, in[maxm];

int main() {
    scanf("%lld%lld", &n, &k);
    for (int i=0; i<n; i++) {
        scanf("%lld", &in[i]);
        sorted.push_back(in[i]);
    }
    sort(sorted.begin(), sorted.end());
    sorted.resize(unique(sorted.begin(), sorted.end())-sorted.begin());
    for (int i=0; i<sorted.size(); i++) {
        dict[sorted[i]] = i;
    }
    for (int i=0; i<n; i++) {
        in[i] = dict[in[i]];
        top.mod(1, in[i]);
        total += top.query(in[i]+1, maxm);
    }
    if (total <= k) {
        printf("%lld\n", (n*(n-1))/2);
        return 0;
    }
    top.mod(-1, in[0]);
    bot.mod(1, in[0]);
    int ed = 1;
    for (int st=1; st<n-1; st++) {
        ed = max(ed, st);
        lld q;
        //printf("%lld\n", bot.query(in[ed]+1, maxm) + top.query(0, in[ed]));
        while (ed != n-1 && total - cur - (q = bot.query(in[ed]+1, maxm) + top.query(0, in[ed])) > k) {
            cur += q;
            top.mod(-1, in[ed]);
            ed++;
            //printf("%lld\n", bot.query(in[ed]+1, maxm) + top.query(0, in[ed]));
        }
        //printf("%d to %d\n", st, ed);
        if (total-cur-(bot.query(in[ed]+1, maxm) + top.query(0, in[ed])) <= k) sum += n-ed-1;
        if (cur) cur = cur - (bot.query(in[st]+1, maxm) + top.query(0, in[st]));
        bot.mod(1, in[st]);
    }
    printf("%lld", sum);
    return 0;
}


/* ***********************************************
Author :111qqz
Created Time :Wed 14 Sep 2016 04:23:06 PM CST
File Name :code/cf/problem/220E.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=2E5+7;
int tree[2][N];
int a[N],p[N];
int n;
LL k;
int lowbit( int x)
{
    return x&(-x);
}
void update (int o, int x,int delta)
{
    if (!o) x = n-x+1;
    for ( int i = x; i  <= n ; i+=lowbit(i))  tree[o][i]+=delta;
}
int Hash( int x)
{
    return lower_bound(p+1,p+n+1,x)-p;
}
int Sum(int o, int x)
{
    if (!o) x = n-x+1;
    int res = 0 ;
    for ( int i = x; i >= 1 ; i-=lowbit(i)) res+=tree[o][i];
    return res;
}
int main()
{
	#ifndef  ONLINE_JUDGE
	freopen("code/in.txt","r",stdin);
  #endif
	cin>>n>>k;
	for ( int i = 1 ;i <= n ; i++) scanf("%d",a+i),p[i] = a[i];
	sort(p+1,p+n+1);
	LL total = 0;
	for ( int i = n ; i >=1 ; i--)
	{
	    int x = Hash(a[i]);
	    a[i] = x;
	    total +=Sum(1,a[i]-1);
	    update(1,a[i],1);
	    //printf("%d%c",x,i==n?'\n':' ');
//	    cout<<"total:"<<total<<endl;
	}
	int r = 1;
	LL ans = 0;
	for ( int l = 1 ; l <= n ; l++)
	{
//	    printf("tot:%d ",total);
	    total +=Sum(0,a[l]+1)+Sum(1,a[l]-1);
	    update(0,a[l],1);
	    while ((r<=l||total>k)&&r<=n)
	    {
		total-=Sum(0,a[r]+1)+Sum(1,a[r]-1);
		update(1,a[r],-1);
		r++;
	    }
	    ans+=n-r+1;
//	    printf("l:%d r:%d ans: %lld total:%d\n",l,r,ans,total);
	}
	cout<<ans<<endl;
  #ifndef ONLINE_JUDGE
  fclose(stdin);
  #endif
    return 0;
}


/* ***********************************************
Author :111qqz
Created Time :Wed 14 Sep 2016 04:23:06 PM CST
File Name :code/cf/problem/220E.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=2E5+7;
int tree[2][N];
int a[N],p[N];
int n;
LL k;
int lowbit( int x)
{
    return x&(-x);
}
void update (int o, int x,int delta)
{
    if (!o) x = n-x+1;
    for ( int i = x; i  <= n ; i+=lowbit(i))  tree[o][i]+=delta;
}
int Hash( int x)
{
    return lower_bound(p+1,p+n+1,x)-p;
}
int Sum(int o, int x)
{
    if (!o) x = n-x+1;
    int res = 0 ;
    for ( int i = x; i >= 1 ; i-=lowbit(i)) res+=tree[o][i];
    return res;
}
int main()
{
	#ifndef  ONLINE_JUDGE
	freopen("code/in.txt","r",stdin);
  #endif
	cin>>n>>k;
	for ( int i = 1 ;i <= n ; i++) cin>>a[i],p[i] = a[i];
	sort(p+1,p+n+1);
	int total = 0;
	for ( int i = n ; i >=1 ; i--)
	{
	    int x = Hash(a[i]);
	    a[i] = x;
	    total +=Sum(1,a[i]-1);
	    update(1,a[i],1);
	    //printf("%d%c",x,i==n?'\n':' ');
//	    cout<<"total:"<<total<<endl;
	}
	int r = 1;
	LL ans = 0;
	for ( int l = 1 ; l <= n ; l++)
	{
//	    printf("tot:%d ",total);
	    total +=Sum(0,a[l]+1)+Sum(1,a[l]-1);
	    update(0,a[l],1);
	    while ((r<=l||total>k)&&r<=n)
	    {
		total-=Sum(0,a[r]+1)+Sum(1,a[r]-1);
		update(1,a[r],-1);
		r++;
	    }
	    ans+=n-r+1;
//	    printf("l:%d r:%d ans: %lld total:%d\n",l,r,ans,total);
	}
	cout<<ans<<endl;
  #ifndef ONLINE_JUDGE
  fclose(stdin);
  #endif
    return 0;
}


/* ***********************************************
Author :111qqz
Created Time :Wed 14 Sep 2016 04:23:06 PM CST
File Name :code/cf/problem/220E.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=1E5+7;
int tree[2][N];
int a[N],p[N];
int n;
LL k;
int lowbit( int x)
{
    return x&(-x);
}
void update (int o, int x,int delta)
{
    if (!o) x = n-x+1;
    for ( int i = x; i  <= n ; i+=lowbit(i))  tree[o][i]+=delta;
}
int Hash( int x)
{
    return lower_bound(p+1,p+n+1,x)-p;
}
int Sum(int o, int x)
{
    if (!o) x = n-x+1;
    int res = 0 ;
    for ( int i = x; i >= 1 ; i-=lowbit(i)) res+=tree[o][i];
    return res;
}
int main()
{
	#ifndef  ONLINE_JUDGE
	freopen("code/in.txt","r",stdin);
  #endif
	cin>>n>>k;
	for ( int i = 1 ;i <= n ; i++) scanf("%d",a+i),p[i] = a[i];
	sort(p+1,p+n+1);
	LL total = 0;
	for ( int i = n ; i >=1 ; i--)  //初始，l,r在同一位置，表示b就是a的全部，因此逆序对数就是整个数列的逆序对数，所以要先算一遍整体的逆序对数。
	{
	    int x = Hash(a[i]);
	    a[i] = x;
	    total +=Sum(1,a[i]-1);
	    update(1,a[i],1);
	}
	int r = 1;
	LL ans = 0;
	for ( int l = 1 ; l <= n ; l++)
	{
	//    printf("tot:%lld ",total);
	    total +=Sum(0,a[l]+1)+Sum(1,a[l]-1);//l和r在第一次之后就不会相邻了，那么增加一个l,分两段考虑它对逆序数的影响。
	 //   printf(" tot2:%lld ",total);
	    update(0,a[l],1);//维护逆序的bit
	    while ((r<=l||total>k)&&r<=n)
	    {
		total-=Sum(0,a[r]+1)+Sum(1,a[r]-1);//从b数列中减去了之前的a[r]，前后两段考虑对逆序对的影响。
		update(1,a[r],-1);//在正向的bit中撤销a[r]，因为a[r]已经不在b中了，不然会影响a[r]后面的数计算前半段的逆序数的影响。
		r++;
	    }
	    ans+=n-r+1;//每次找到满足的最小的r,r到n一共由n-r+1个元素，这些都是满足的，因为序列中元素的个数减少，逆序对一定是非增加的。
	  //  printf("l:%d r:%d ans: %lld total:%lld\n",l,r,ans,total);
	}
	cout<<ans<<endl;
  #ifndef ONLINE_JUDGE
  fclose(stdin);
  #endif
    return 0;
}


/* ***********************************************
Author :111qqz
Created Time :Wed 14 Sep 2016 04:23:06 PM CST
File Name :code/cf/problem/220E.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=1E5+7;
int tree[2][N];
int a[N],p[N];
int n;
LL k;
int lowbit( int x)
{
    return x&(-x);
}
void update (int o, int x,int delta)
{
    if (!o) x = n-x+1;
    for ( int i = x; i  <= n ; i+=lowbit(i))  tree[o][i]+=delta;
}
int Hash( int x)
{
    return lower_bound(p+1,p+n+1,x)-p;
}
int Sum(int o, int x)
{
    if (!o) x = n-x+1;
    int res = 0 ;
    for ( int i = x; i >= 1 ; i-=lowbit(i)) res+=tree[o][i];
    return res;
}
int main()
{
	#ifndef  ONLINE_JUDGE
	freopen("code/in.txt","r",stdin);
  #endif
	cin>>n>>k;
	for ( int i = 1 ;i <= n ; i++) cin>>a[i],p[i] = a[i];
	sort(p+1,p+n+1);
	int total = 0;
	for ( int i = n ; i >=1 ; i--)
	{
	    int x = Hash(a[i]);
	    a[i] = x;
	    total +=Sum(1,a[i]-1);
	    update(1,a[i],1);
	    //printf("%d%c",x,i==n?'\n':' ');
//	    cout<<"total:"<<total<<endl;
	}
	int r = 1;
	LL ans = 0;
	for ( int l = 1 ; l <= n ; l++)
	{
//	    printf("tot:%d ",total);
	    total +=Sum(0,a[l]+1)+Sum(1,a[l]-1);
	    update(0,a[l],1);
	    while ((r<=l||total>k)&&r<=n)
	    {
		total-=Sum(0,a[r]+1)+Sum(1,a[r]-1);
		update(1,a[r],-1);
		r++;
	    }
	    ans+=n-r+1;
//	    printf("l:%d r:%d ans: %lld total:%d\n",l,r,ans,total);
	}
	cout<<ans<<endl;
  #ifndef ONLINE_JUDGE
  fclose(stdin);
  #endif
    return 0;
}


/* ***********************************************
Author :111qqz
Created Time :Wed 14 Sep 2016 04:23:06 PM CST
File Name :code/cf/problem/220E.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=2E5+7;
int tree[2][N];
int a[N],p[N];
int n;
LL k;
int lowbit( int x)
{
    return x&(-x);
}
void update (int o, int x,int delta)
{
    if (!o) x = n-x+1;
    for ( int i = x; i  <= n ; i+=lowbit(i))  tree[o][i]+=delta;
}
int Hash( int x)
{
    return lower_bound(p+1,p+n+1,x)-p;
}
int Sum(int o, int x)
{
    if (!o) x = n-x+1;
    int res = 0 ;
    for ( int i = x; i >= 1 ; i-=lowbit(i)) res+=tree[o][i];
    return res;
}
int main()
{
	#ifndef  ONLINE_JUDGE
	freopen("code/in.txt","r",stdin);
  #endif
	cin>>n>>k;
	for ( int i = 1 ;i <= n ; i++) scanf("%d",a+i),p[i] = a[i];
	sort(p+1,p+n+1);
	LL total = 0;
	for ( int i = n ; i >=1 ; i--)  //初始，l,r在同一位置，表示b就是a的全部，因此逆序对数就是整个数列的逆序对数，所以要先算一遍整体的逆序对数。
	{
	    int x = Hash(a[i]);
	    a[i] = x;
	    total +=Sum(1,a[i]-1);
	    update(1,a[i],1);
	}
	int r = 1;
	LL ans = 0;
	for ( int l = 1 ; l <= n ; l++)
	{
	//    printf("tot:%lld ",total);
	    total +=Sum(0,a[l]+1)+Sum(1,a[l]-1);//l和r在第一次之后就不会相邻了，那么增加一个l,分两段考虑它对逆序数的影响。
	 //   printf(" tot2:%lld ",total);
	    update(0,a[l],1);//维护逆序的bit
	    while ((r<=l||total>k)&&r<=n)
	    {
		total-=Sum(0,a[r]+1)+Sum(1,a[r]-1);//从b数列中减去了之前的a[r]，前后两段考虑对逆序对的影响。
		update(1,a[r],-1);//在正向的bit中撤销a[r]，因为a[r]已经不在b中了，不然会影响a[r]后面的数计算前半段的逆序数的影响。
		r++;
	    }
	    ans+=n-r+1;//每次找到满足的最小的r,r到n一共由n-r+1个元素，这些都是满足的，因为序列中元素的个数减少，逆序对一定是非增加的。
	  //  printf("l:%d r:%d ans: %lld total:%lld\n",l,r,ans,total);
	}
	cout<<ans<<endl;
  #ifndef ONLINE_JUDGE
  fclose(stdin);
  #endif
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define N 120000
using namespace std;
int tree[2][N],n,l,r,p[N],a[N],i;
long long ans,k,v;
int der(int x){
	return lower_bound(p+1,p+n+1,x)-p;
}
int calc(int o,int x){
	if(!o)x=n-x+1;
	int res=0;
	for(int i=x;i;i-=i&(-i))res+=tree[o][i];
	return res;
}
void add(int o,int x,int flag){
	if(!o)x=n-x+1;
	for(int i=x;i<=n;i+=i&(-i))tree[o][i]+=flag;
}
int main(){
	scanf("%d%I64d",&n,&k);
	for(i=1;i<=n;++i)scanf("%d",&a[i]),p[i]=a[i];
	sort(p+1,p+n+1);
	for(i=n;i>=1;--i){
		a[i]=der(a[i]);
		v+=calc(1,a[i]-1);
		add(1,a[i],1);
	}
	r=1;
	for(l=1;l<=n;++l){
		v+=calc(0,a[l]+1)+calc(1,a[l]-1);
		add(0,a[l],1);
		while((r<=l || v>k) && r<=n){
			v-=calc(0,a[r]+1)+calc(1,a[r]-1);
			add(1,a[r],-1);
			r++;
		}
		ans+=n-r+1;
	}
	printf("%I64d\n",ans);
}


/* ***********************************************
Author :111qqz
Created Time :Wed 14 Sep 2016 04:23:06 PM CST
File Name :code/cf/problem/220E.cpp
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair
using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=2E5+7;
int tree[2][N];
int a[N],p[N];
int n;
LL k;
int lowbit( int x)
{
    return x&(-x);
}
void update (int o, int x,int delta)
{
    if (!o) x = n-x+1;
    for ( int i = x; i  <= n ; i+=lowbit(i))  tree[o][i]+=delta;
}
int Hash( int x)
{
    return lower_bound(p+1,p+n+1,x)-p;
}
int Sum(int o, int x)
{
    if (!o) x = n-x+1;
    int res = 0 ;
    for ( int i = x; i >= 1 ; i-=lowbit(i)) res+=tree[o][i];
    return res;
}
int main()
{
	#ifndef  ONLINE_JUDGE
	freopen("code/in.txt","r",stdin);
  #endif
	cin>>n>>k;
	for ( int i = 1 ;i <= n ; i++) cin>>a[i],p[i] = a[i];
	sort(p+1,p+n+1);
	LL total = 0;
	for ( int i = n ; i >=1 ; i--)
	{
	    int x = Hash(a[i]);
	    a[i] = x;
	    total +=Sum(1,a[i]-1);
	    update(1,a[i],1);
	    //printf("%d%c",x,i==n?'\n':' ');
//	    cout<<"total:"<<total<<endl;
	}
	int r = 1;
	LL ans = 0;
	for ( int l = 1 ; l <= n ; l++)
	{
//	    printf("tot:%d ",total);
	    total +=Sum(0,a[l]+1)+Sum(1,a[l]-1);
	    update(0,a[l],1);
	    while ((r<=l||total>k)&&r<=n)
	    {
		total-=Sum(0,a[r]+1)+Sum(1,a[r]-1);
		update(1,a[r],-1);
		r++;
	    }
	    ans+=n-r+1;
//	    printf("l:%d r:%d ans: %lld total:%d\n",l,r,ans,total);
	}
	cout<<ans<<endl;
  #ifndef ONLINE_JUDGE
  fclose(stdin);
  #endif
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

#define mid (lf + rg >> 1)
#define right (left | 1)
#define left (v << 1)

#define pb push_back
#define mp make_pair
#define nd second
#define st first

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

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e5 + 9;

int n, a[N], dp[N], dp2[N], k, cur, ans, tree[4*N];

int update(int v, int lf, int rg, int pos, int val) {
	if (lf == rg) return tree[v] += val;
	if (pos <= mid) update(left, lf, mid, pos, val);
	else update(right, mid + 1, rg, pos, val);
	return tree[v] = tree[left] + tree[right];
}

int query(int v, int lf, int rg, int l, int r) {
	if (l > rg or r < lf) return 0;
	if (l <= lf and rg <= r) return tree[v];
	return query(left, lf, mid, l, r) + query(right, mid + 1, rg, l, r);
}

main() {

	scanf("%lld %lld", &n, &k);
	pii b[N];
	for (int i = 1; i <= n; i++) {
		scanf("%lld", &a[i]);
		b[i] = mp(a[i], i);
	}
	sort(b + 1, b + n + 1);
	int t = 0;
	for (int i = 1; i <= n; i++)
		if (b[i].st == b[i - 1].st) a[b[i].nd] = t;
		else a[b[i].nd] = ++t;
	for (int i = n; i >= 1; i--) {
		dp[i] = query(1, 1, n, 1, a[i] - 1);
		update(1, 1, n, a[i], 1);
		cur += dp[i];
	}
	memset(tree, 0, sizeof tree);
	for (int i = 1; i <= n; i++) {
		dp2[i] = query(1, 1, n, a[i] + 1, N);
		update(1, 1, n, a[i], 1);
	}
	memset(tree, 0, sizeof tree);
	int r = 1;
	for (int i = 1; i <= n; i++) {
		if (r == i) {
			r = i + 1;
			cur -= (ll) dp[i] + dp2[i] - query(1, 1, n, a[i] + 1, N);
			update(1, 1, n, a[i], 1);
		}
		cur += (ll) dp[i] + dp2[i] - query(1, 1, n, 1, a[i] - 1);
		if (query(1, 1, n, a[i], a[i])) update(1, 1, n, a[i], -1);
		while (r <= n and cur > k) {
			cur -= (ll) dp[r] + dp2[r] - query(1, 1, n, a[r] + 1, N);
			update(1, 1, n, a[r], 1);
			r++;
		}
		ans += n - r + 1;
	}
	printf("%lld\n", ans);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mid (lf + rg >> 1)
#define right (left | 1)
#define left (v << 1)

#define pb push_back
#define mp make_pair
#define nd second
#define st first

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

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e5 + 9;

int n, a[N], dp[N], dp2[N];
ll k, cur, ans, tree[4*N];

ll update(int v, int lf, int rg, int pos, int val) {
	if (lf == rg) return tree[v] += val;
	if (pos <= mid) update(left, lf, mid, pos, val);
	else update(right, mid + 1, rg, pos, val);
	return tree[v] = tree[left] + tree[right];
}

ll query(int v, int lf, int rg, int l, int r) {
	if (l > rg or r < lf) return 0;
	if (l <= lf and rg <= r) return tree[v];
	return query(left, lf, mid, l, r) + query(right, mid + 1, rg, l, r);
}

int main() {

	scanf("%d %lld", &n, &k);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = n; i >= 1; i--) {
		dp[i] = query(1, 1, n, 1, a[i] - 1);
		update(1, 1, n, a[i], 1);
		cur += dp[i];
	}
	memset(tree, 0, sizeof tree);
	for (int i = 1; i <= n; i++) {
		dp2[i] = query(1, 1, n, a[i] + 1, N);
		update(1, 1, n, a[i], 1);
	}
	memset(tree, 0, sizeof tree);
	int r = 1;
	for (int i = 1; i <= n; i++) {
		if (r == i) {
			r = i + 1;
			cur -= (ll) dp[i] + dp2[i] - query(1, 1, n, a[i] + 1, N);
			update(1, 1, n, a[i], 1);
		}
		cur += (ll) dp[i] + dp2[i] - query(1, 1, n, 1, a[i] - 1);
		if (query(1, 1, n, a[i], a[i])) update(1, 1, n, a[i], -1);
		while (r <= n and cur > k) {
			cur -= (ll) dp[r] + dp2[r] - query(1, 1, n, a[r] + 1, N);
			update(1, 1, n, a[r], 1);
			r++;
		}
		ans += n - r + 1;
	}
	printf("%lld\n", ans);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mid (lf + rg >> 1)
#define right (left | 1)
#define left (v << 1)

#define pb push_back
#define mp make_pair
#define nd second
#define st first

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

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e5 + 9;

int n, k, a[N], dp[N], dp2[N];
ll cur, ans, tree[4*N];

int update(int v, int lf, int rg, int pos, int val) {
	if (lf == rg) return tree[v] += val;
	if (pos <= mid) update(left, lf, mid, pos, val);
	else update(right, mid + 1, rg, pos, val);
	return tree[v] = tree[left] + tree[right];
}

int query(int v, int lf, int rg, int l, int r) {
	if (l > rg or r < lf) return 0;
	if (l <= lf and rg <= r) return tree[v];
	return query(left, lf, mid, l, r) + query(right, mid + 1, rg, l, r);
}

int main() {

	scanf("%d %d", &n, &k);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = n; i >= 1; i--) {
		dp[i] = query(1, 1, n, 1, a[i] - 1);
		update(1, 1, n, a[i], 1);
		cur += dp[i];
	}
	memset(tree, 0, sizeof tree);
	for (int i = 1; i <= n; i++) {
		dp2[i] = query(1, 1, n, a[i] + 1, N);
		update(1, 1, n, a[i], 1);
	}
	memset(tree, 0, sizeof tree);
	int r = 1;
	for (int i = 1; i <= n; i++) {
		if (r == i) {
			r = i + 1;
			cur -= dp[i] + dp2[i] - query(1, 1, n, a[i] + 1, N);
			update(1, 1, n, a[i], 1);
		}
		cur += dp[i] + dp2[i] - query(1, 1, n, 1, a[i] - 1);
		if (query(1, 1, n, a[i], a[i])) update(1, 1, n, a[i], -1);
		while (r <= n and cur > k) {
			cur -= dp[r] + dp2[r] - query(1, 1, n, a[r] + 1, N);
			update(1, 1, n, a[r], 1);
			r++;
		}
		ans += n - r + 1;
	}
	printf("%lld\n", ans);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mid (lf + rg >> 1)
#define right (left | 1)
#define left (v << 1)

#define pb push_back
#define mp make_pair
#define nd second
#define st first

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

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e5 + 9;

int n, a[N], dp[N], dp2[N], tree[4 * N];
ll k, cur, ans;

int update(int v, int lf, int rg, int pos, int val) {
	if (lf == rg) return tree[v] += val;
	if (pos <= mid) update(left, lf, mid, pos, val);
	else update(right, mid + 1, rg, pos, val);
	return tree[v] = tree[left] + tree[right];
}

int query(int v, int lf, int rg, int l, int r) {
	if (l > rg or r < lf) return 0;
	if (l <= lf and rg <= r) return tree[v];
	return query(left, lf, mid, l, r) + query(right, mid + 1, rg, l, r);
}

main() {

	scanf("%d %lld", &n, &k);
	pii b[N];
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		b[i] = mp(a[i], i);
	}
	sort(b + 1, b + n + 1);
	int t = 0;
	for (int i = 1; i <= n; i++)
		if (b[i].st == b[i - 1].st) a[b[i].nd] = t;
		else a[b[i].nd] = ++t;
	for (int i = n; i >= 1; i--) {
		dp[i] = query(1, 1, n, 1, a[i] - 1);
		update(1, 1, n, a[i], 1);
		cur += dp[i];
	}
	memset(tree, 0, sizeof tree);
	for (int i = 1; i <= n; i++) {
		dp2[i] = query(1, 1, n, a[i] + 1, N);
		update(1, 1, n, a[i], 1);
	}
	memset(tree, 0, sizeof tree);
	int r = 1;
	for (int i = 1; i <= n; i++) {
		if (r == i) {
			r = i + 1;
			cur -= dp[i] + dp2[i] - query(1, 1, n, a[i] + 1, N);
			update(1, 1, n, a[i], 1);
		}
		cur += dp[i] + dp2[i] - query(1, 1, n, 1, a[i] - 1);
		if (query(1, 1, n, a[i], a[i])) update(1, 1, n, a[i], -1);
		while (r <= n and cur > k) {
			cur -= dp[r] + dp2[r] - query(1, 1, n, a[r] + 1, N);
			update(1, 1, n, a[r], 1);
			r++;
		}
		ans += n - r + 1;
	}
	printf("%lld\n", ans);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define mid (lf + rg >> 1)
#define right (left | 1)
#define left (v << 1)

#define pb push_back
#define mp make_pair
#define nd second
#define st first

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

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e5 + 9;

int n, k, a[N], dp[N], dp2[N];
ll cur, ans, tree[4*N];

int update(int v, int lf, int rg, int pos) {
	if (lf == rg) return tree[v]++;
	if (pos <= mid) update(left, lf, mid, pos);
	else update(right, mid + 1, rg, pos);
	return tree[v] = tree[left] + tree[right];
}

int query(int v, int lf, int rg, int l, int r) {
	if (l > rg or r < lf) return 0;
	if (l <= lf and rg <= r) return tree[v];
	return query(left, lf, mid, l, r) + query(right, mid + 1, rg, l, r);
}

int main() {

	scanf("%d %d", &n, &k);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = n; i >= 1; i--) {
		dp[i] = query(1, 1, n, 1, a[i] - 1);
		update(1, 1, n, a[i]);
		cur += dp[i];
	}
	memset(tree, 0, sizeof tree);
	for (int i = 1; i <= n; i++) {
		dp2[i] = query(1, 1, n, a[i] + 1, N);
		update(1, 1, n, a[i]);
	}
	int r = 1;
	for (int i = 1; i <= n; i++) {
		if (r == i) {
			r = i + 1;
			cur -= dp[i] + dp2[i];
		}
		cur += dp[i] + dp2[i];
		while (r <= n and cur > k) {
			cur -= dp[r] + dp2[r];
			r++;
		}
		ans += n - r + 1;
	}
	printf("%lld\n", ans);

	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;

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

lld sol, all, k;

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 <map>
#include <cstring>
#include <iostream>

using namespace std;

typedef long long int lld;

const int maxn = 1e5 + 5;

int n, k, queue[ maxn ], size, temp[ maxn ], tree[ maxn ], rightLower[ maxn ], leftUpper[ 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", &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( tree, 0, sizeof( tree ) );
    for ( int i = n - 1; i >= 0; i-- )
    {
        rightLower[ i ] = get( queue[ i ] - 1 );
        update( queue[ i ] );
    }
    memset( tree, 0, sizeof( tree ) );

    lld all = 0LL;
    for ( int i = 0; i < n; i++ ) all += lld( rightLower[ i ] );
    if ( all <= k )
    {
        cout << lld( n ) * lld( n - 1 ) / 2LL << endl;
        return 0;
    }

    int left = 0, right = 1;
    lld curr = all;
    update( size - queue[ 0 ] );
    while ( left < n - 1 )
    {
        while ( right < n && curr > k )
        {
            curr -= lld( rightLower[ right ] );
            curr -= lld( get( size - queue[ right ] - 1 ) );
            right++;
        }
        if ( right == n ) break;
        sol += lld( n ) - lld( right );
        left++;
        curr += lld( rightLower[ left ] + leftUpper[ left ] );
        update( size - queue[ 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;
const int maxnode = 131072 + 5;

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

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 <cstring>
#include <iostream>
#include <map>

using namespace std;

typedef long long int lld;

const int maxn = 1e5 + 5;
const int maxnode = 131072 + 5;

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

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 -= rightLower[ right ];
            all -= 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 += getNew( queue[ left ] - 1 );
        all += leftUpper[ left ];
    }

    cout << sol << endl;

    return 0;
}


/*
    Marko Bakovic
    Little Elephant and Inversions
*/

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

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", &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( 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 )
    {
        cout << lld( n ) * lld( n - 1 ) / 2LL << endl;
        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 ] );
    }

    cout << sol << endl;

    return 0;
}


/*
    Marko Bakovic
    Little Elephant and Inversions
*/

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

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 ];

lld sol, all;

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 ] );

    init( 1, 0, n - 1 );
    for ( int i = 0; i < n; i++ ) all += lld( findLower( 1, 0, n - 1, i, n - 1, queue[ i ] ) );

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

    int left = 0, right = 1;
    while ( left < n - 1 )
    {
        while ( right < n - 1 && all > k )
        {
            all -= lld( findLower( 1, 0, n - 1, right + 1, n - 1, queue[ 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 += lld( findUpper( 1, 0, n - 1, 0, left - 1, queue[ left ] ) );
    }

    cout << sol << endl;

    return 0;
}