jiahuimbzuai/CoQuIR huggingface dataset:
//  [java]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public final class Main {

    public static void main(String[] args) {
        final FastScanner fs = new FastScanner();
        final int n = fs.nextInt();
        final int k = fs.nextInt();
        final int[] arr = fs.nextIntArray(n);
        long res = (long) 1e18;
        for (int i = 0; i < n - k; i++) {
            final long L = arr[i];
            final long R = arr[k + i - 1];
            final long curr = R - L + Math.min(Math.abs(L), Math.abs(R));
            res = Math.min(res, curr);
        }
        System.out.println(res);
    }

    static final class Util {
        public static void shuffleSort(int[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffleSort(long[] x) {
            shuffle(x);
            Arrays.sort(x);
        }

        public static void shuffle(int[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void shuffle(long[] x) {
            final Random r = new Random();

            for (int i = 0; i <= x.length - 2; i++) {
                final int j = i + r.nextInt(x.length - i);
                swap(x, i, j);
            }
        }

        public static void swap(int[] x, int i, int j) {
            final int t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        public static void swap(long[] x, int i, int j) {
            final long t = x[i];
            x[i] = x[j];
            x[j] = t;
        }

        private Util() {}
    }

    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        private String next() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    //noinspection CallToPrintStackTrace
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int[] nextIntArray(int n) {
            final int[] a = new int[n];
            for (int i = 0; i < n; i++) { a[i] = nextInt(); }
            return a;
        }

        long[] nextLongArray(int n) {
            final long[] a = new long[n];
            for (int i = 0; i < n; i++) { a[i] = nextLong(); }
            return a;
        }
    }
}
//  [java]
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner in = new Scanner(System.in)) {
            int N = in.nextInt();
            int K = in.nextInt();
            int[] x = new int[N + 1];
            for (int i = 0; i < N; i++) {
                x[i] = in.nextInt();
            }
            x[N] = 0;
            Arrays.sort(x);

            int index0 = 0;
            for (int i = 0; i < N + 1; i++) {
                if (x[i] == 0) {
                    index0 = i;
                    break;
                }
            }

            int start = index0 - K;
            int end = index0 + K;
            int Start = Math.max(0, start);
            int End = Math.min(N, end);

            int min = (int) 1e9;
            if (Start == start) {
                min = Math.min(min, -1 * x[Start]);
            }
            for (int i = Start + 1; i <= index0; i++) {
                if (i + K < x.length) {
                    min = Math.min(min, -2 * x[i] + x[i + K]);
                }
            }
            for (int i = index0; i < End; i++) {
                if (i - K >= 0) {
                    min = Math.min(min, -x[i - K] + 2 * x[i]);
                }
            }
            if (End == end) {
                min = Math.min(min, x[End]);
            }

            System.out.println(min);
        }
    }
}
//  [cpp]
//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

const int cm = 1 << 17;
char cn[cm], * ci = cn + cm, ct;
inline char getcha() {
	if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }
	return *ci++;
}
inline int getint() {
	int A = 0;
	int pn = 1;
	if (ci - cn + 16 > cm) {
		if ((ct = getcha()) == '-') { pn = -1; ct = getcha(); }
		A = ct - '0';
		while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';;
		return pn * A;
	}
	else {
		if ((ct = *ci++) == '-') { pn = -1; ct = *ci++; }
		A = ct - '0';
		while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';;
		return pn * A;
	}
}

int main() {
	//cin.tie(0);
	//ios::sync_with_stdio(false);


	int N = getint(), K = getint();
	int x[100000];
	rep(i, N) x[i] = getint();

	int saishou = 300000001;
	rep(i, N) {
		if (i + K <= N) {
			int kari = abs(x[i]) + abs(x[i + K - 1] - x[i]);
			saishou = min(kari, saishou);
		}
		if (i - K + 1 >= 0) {
			int kari = abs(x[i]) + abs(x[i - K + 1] - x[i]);
			saishou = min(kari, saishou);
		}
	}
	printf("%d", saishou);

	Would you please return 0;
}
//  [cpp]
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main() {
  ll a,b;
  cin>>a>>b;
  vector<ll>vec(a+1);
  ll c=0;
  vec.at(0)=0;
  for(ll i=0;i<a;i++){
    cin>>vec.at(i+1);
    if(vec.at(i+1)<0){
      c+=1;
    }
  }
  sort(vec.begin(),vec.end());
  ll d=1000000000000000;
  for(ll i=0;i<=c;i++){
    if(a-c<b-i){
      continue;
    }
    else{
      d=min(d,min(-vec.at(c-i),vec.at(c+b-i))-vec.at(c-i)+vec.at(c+b-i));
    }
  }
  cout<<d<<endl;
            
  return 0;
}
//  [cpp]
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const long long INF = 1LL << 60;
const ll C = 1000000000+7;

int main() {
    int N, K;
    cin >> N >> K;
    vector<int> x(N);
    for(int i=0; i<N; i++) cin >> x[i];

    ll min_time1 = abs(x[0]) + abs(x[K-1] - x[0]);
    ll min_time2 = abs(x[K-1]) +  abs(x[K-1] - x[0]);
    ll min_time = min(min_time1, min_time2);
    for(int i=0; i<N-K; i++) {
        ll t1 = abs(x[i]) + abs(x[i+K-1] - x[i]);
        ll t2 = abs(x[i + K - 1]) + abs(x[i+K-1] - x[i]);
        min_time = min(min_time, min(t1, t2));
    }
    cout << min_time << endl;
}
//  [cpp]
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const long long INF = 1LL << 60;
const ll C = 1000000000+7;

int main() {
    int N, K;
    cin >> N >> K;
    vector<int> x(N);
    for(int i=0; i<N; i++) cin >> x[i];

    ll min_time = abs(x[0]) + abs(x[K-1] - x[0]);
    for(int i=0; i<N-K; i++) {
        ll t1 = abs(x[i]) + abs(x[i+K-1] - x[i]);
        ll t2 = abs(x[i + K - 1]) + abs(x[i+K-1] - x[i]);
        min_time = min(min_time, min(t1, t2));
    }
    cout << min_time << endl;
}
//  [c]
// AtCoder ABC107: C - Candles
// 2019.9.11 bal4u

#include <stdio.h>

#if 1
int getchar_unlocked(void);
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() {  // 整数の入力
	int n = 0, c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

#define MIN(a,b)  ((a)<=(b)?(a):(b))
#define MAX(a,b)  ((a)>=(b)?(a):(b))
#define ABS(a)    ((a)>=0?(a):-(a))
int N, K;
int x[100005];

// 見つからなければ、一つ小さい要素を返す
int bs(int t)
{
	int m, l = 0, r = N;

    while (l < r) {
        m = (l+r) >> 1;
        if (x[m] <= t) l = m + 1; else r = m;
    }
	return l-1;
}

int main()
{
	int i, ans, t;
	
	N = in(), K = in();
	for (i = 0; i < N; i++) x[i] = in();
	if (x[0] >= 0) { printf("%d\n", x[K-1]); return 0; }
	
	ans = 0x7fffffff;
	i = bs(0); while (N-i < K) i--;
	for ( ; i >= 0; i--) {
		t = x[K+i-1]-x[i] + MIN(ABS(x[K+i-1]), ABS(x[i]));
		if (t < ans) ans = t;
	}
	printf("%d\n", ans);
	return 0;
}
//  [c]
//q1
// pactice
 
#pragma warning(disable:4996)
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wwrite-strings"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define MAX_NUMBER 100000
#define MAX_STRING 1000000

	int anNumber[MAX_NUMBER];
	int anMinusNumber[MAX_NUMBER];
	char sInput[MAX_STRING+1]="";

int main() {
 
	char* psInput=NULL;
	int nNumberOfNumber=0;
	int nQuotaNumber=0;

	int nMinChamp=1100000000;
	int nMinTemp=1100000000;
	int nMPChangedPos=-1;
	int nMinusCount=0;
	int nPlusCount=0;

	int i=0;
	int nTemp=0;
	int nPatternNo=0;

	for(i=0;i<MAX_NUMBER;i++){
		anNumber[i]=0;
	}
	// read line 1
	gets(sInput);
	psInput = strtok(sInput," ");
	nNumberOfNumber=atoi(psInput);
	psInput = strtok(NULL," ");
	nQuotaNumber=atoi(psInput);

	// read line 2
	gets(sInput);
	psInput = strtok(sInput," ");
	for (i=0;i<nNumberOfNumber;i++){
		nTemp=atoi(psInput);
		anNumber[i]=nTemp;
		if(nTemp<0){
			nMinusCount++;
		} else if (nTemp>0){
			nPlusCount++;
			if(nMPChangedPos==-1){
				if(nMinusCount>0){
					nMPChangedPos=i;
				}
			}
		} else {
			nQuotaNumber--;
		}
		psInput = strtok(NULL," ");
	}
	if (nQuotaNumber==0){
		printf("0\n");
	} else {
		if (nMinusCount==0&&nPlusCount>0){
			printf("%d\n",abs(anNumber[nQuotaNumber-1]));
		} else if (nMinusCount>0&&nPlusCount==0){
			printf("%d\n",abs(anNumber[nNumberOfNumber-nQuotaNumber]));
		} else {
			for(nPatternNo=0;nPatternNo<nQuotaNumber;nPatternNo++){
				if(nPatternNo==0){
					//左にだけ走る
					if(nMinusCount>=nQuotaNumber){
						nMinTemp=abs(anNumber[nMPChangedPos-nQuotaNumber]);
						if(nMinTemp<nMinChamp){
							nMinChamp=nMinTemp;
						}
					}
					//右にだけ走る
					if(nPlusCount>=nQuotaNumber){
						nMinTemp=abs(anNumber[nQuotaNumber-1+nMPChangedPos]);
						if(nMinTemp<nMinChamp){
							nMinChamp=nMinTemp;
						}
					}
				} else {
					//左に走ってから右に折り返す
					if(nMinusCount>=nPatternNo&&nPlusCount>=(nQuotaNumber-nPatternNo)){
						//左に走った分
						nMinTemp=abs(anNumber[nMPChangedPos-nPatternNo]*2);
						//右に走った分
						nMinTemp=nMinTemp+abs(anNumber[(nQuotaNumber-nPatternNo)-1+nMPChangedPos]);
						if(nMinTemp<nMinChamp){
							nMinChamp=nMinTemp;
						}
					}
					//右に走ってから左に折り返す
					if(nPlusCount>=nPatternNo&&nMinusCount>=(nQuotaNumber-nPatternNo)){
						//右に走った分
						nMinTemp=abs(anNumber[nPatternNo-1+nMPChangedPos]*2);
						//左に走った分
						nMinTemp=nMinTemp+abs(anNumber[nMPChangedPos-(nQuotaNumber-nPatternNo)]);
						if(nMinTemp<nMinChamp){
							nMinChamp=nMinTemp;
						}
					}
				}
			}
			printf("%d",nMinChamp);
		}
	}

	return 0;
}
//  [c]
#include <stdio.h>
#include <stdlib.h>

int N, K;
int x[114514];

int main(void) {
	int i;
	int mizuhasu = 1111111111;
	if (scanf("%d%d", &N, &K) != 2) return 1;
	for (i = 0; i < N; i++) {
		if (scanf("%d", &x[i]) != 1) return 1;
	}
	for (i = K - 1; i < N; i++) {
		int l = i - (K - 1), r = i;
		int satorina;
		satorina = abs(x[l]);
		if (abs(x[r]) < satorina) satorina = abs(x[r]);
		satorina += x[r] - x[l];
		if (satorina < mizuhasu) mizuhasu = satorina;
	}
	printf("%d\n", mizuhasu);
	return 0;
}
//  [c]
i;j;x[9<<14];m=9<<25;main(){
for(;~scanf("%d",x+i++););
for(;j<x[0]-x[1]+1;j++)m=fmin(x[j+x[1]+1]-x[j+2]+fmin(abs(x[j+2]),abs(x[j+x[1]+1])),m);
printf("%d",m);
//  [c]
#include <stdio.h>
int main(void){
    int x[9<<14];
    int i=0;
    int j=0;
    int min=9<<25;
    
    for(;~scanf("%d",x+i++););
    for(;j<x[0]-x[1]+1;)min=fmin(x[j+x[1]+1]-x[j+2]+fmin(abs(x[j+2]),abs(x[j+++x[1]+1])),min);
    printf("%d",min);
}
//  [c]
#include <stdio.h>
int main(void){
    int x[9<<14];
    int i=0;
    int j=0;
    int min=9<<25;
    
    for(;~scanf("%d",x+i);i++);
    for(j=2;j<x[0]-x[1]+3;j++)min=fmin(x[j+x[1]-1]-x[j]+fmin(abs(x[j]),abs(x[j+x[1]-1])),min);
    
    printf("%d",min);
//  [javascript]
function Main(input) {
  input = input.split("\n");
  var n = parseInt(input[0].split(" ")[0]);
  var k = parseInt(input[0].split(" ")[1]);
  x = new Array(n).fill(0);
  tmp = input[1].split(" ");
  var plus=0;
  var minus=0;
  for(i=0;i<n;i++){
    x[i]=parseInt(tmp[i]);
    if(x[i]>0){
      plus++;
    }else if(x[i]<0){
      minus++;
    }
  }
  var ans=Math.pow(10,9)+7;
  if(x.indexOf(0)!=-1){
    n-=1;
    k-=1;
    x.splice(x.indexOf(0),1);
  }
  if(x.length==0){
    ans=0;
  }else if(minus==0){
    ans=x[k-1];
  }else if(plus==0){
    ans=Math.abs(x[n-k]);
  }else if(n!=2){
    for(i=0;i<n-k+1;i++){
      if(x[i]<0&&x[i+k-1]>0){
        ans=Math.min(ans,Math.abs(x[i])*2+x[i+k-1],Math.abs(x[i])+x[i+k-1]*2);
      }else if(i==minus-k){
        ans=Math.min(ans,Math.abs(x[i]));
      }else if(i==minus){
        ans=Math.min(ans,x[i+k-1]);
      }
    }
  }else{
    ans=Math.min(Math.abs(x[0]),x[1]);
  }
  console.log('%s', ans);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
//  [javascript]
function main(input) {
  var ary = input.split("\n");
  var nums = ary[0].split(" ");
  var n = parseInt(nums[0], 10);
  var k = parseInt(nums[1], 10);
  var x = ary[1].split(" ").map(i => parseInt(i, 10));
  var minTime = null;

  for (var i = 0; i < n - k + 1; i++) {
    var time = calcMinTime(i, i + k - 1, x);
    if (minTime === null || time < minTime) {
      minTime = time;
    }
  }
  console.log(minTime);
}

function calcMinTime(i, j, x) {
  var a = Math.abs(x[i]) + Math.abs(x[j] - x[i]);
  var b = Math.abs(x[j]) + Math.abs(x[j] - x[i]);
  return Math.min(a, b);
}

main(require("fs").readFileSync("/dev/stdin", "utf8"));
//  [javascript]
function Main(input) {
    input = input.split("\n");
    var nk = input[0].split(" ");
    var n = parseInt(nk[0], 10);
    var k = parseInt(nk[1], 10);
    var X = input[1].split(" ").map(x => parseInt(x, 10));
    var zeroInd = -1;
    for (var i = 0; i < n; i++) {
        if (X[i] >= 0) {
            zeroInd = i;
            break;
        }
    }
    if (X[n-1] < 0) {
        zeroInd = n;
    }
    var left = Math.max(zeroInd - k, 0);
    var right = Math.min(zeroInd + k, n);
    var sofar = 1000000001;
    for (var i = left; i < right-k+1; i++) {
        var leftHalf = 0;
        var rightHalf = 0;
        if (i < zeroInd) {
            leftHalf = Math.abs(X[i]);
            rightHalf = Math.max(X[i+k-1], 0);
        } else if (i >= zeroInd) {
            leftHalf = 0;
            rightHalf = X[i+k-1];
        }
        var candid = Math.min(leftHalf*2+rightHalf, leftHalf+rightHalf*2)
        sofar = Math.min(sofar, candid);
    }
    console.log(sofar);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
//  [javascript]
"use strict";
    
const absOfArray = array => {
    const N = array.length;
    const temp = [];
    
    if(array.length === 1) {
        return array[0];
    }
    
    for(let i=1; i<N; i++) {
        temp.push(Math.abs(array[i-1] - array[i]));
    }
    
    return temp.reduce((m,n)=>m+n);
}

const main = arg => {
    arg = arg.trim().split("\n");
    const N = parseInt(arg[0].split(" ")[0]);
    let K = parseInt(arg[0].split(" ")[1]);
    const X = arg[1].split(" ").map(n=>parseInt(n));
    
    const minus = X.filter(n=>n<0).slice(-K);
    const plus  = X.filter(n=>n>0).slice(0, K);
    
    const newX = minus.concat([0]).concat(plus);
    
    let memo = absOfArray(newX.slice(0, K + 1));
    let answer = memo;
    
    // console.log(minus);
    // console.log(plus);
    // console.log(newX);
    
    if(K > newX.length) {
        console.log(memo);
        return;
    }
    
    for(let i=1; i<=newX.length-K-1; i++) {
        memo -= Math.abs(newX[i-1] - newX[i]);
        memo += Math.abs(newX[i + K] - newX[i + K - 1]);
        
        // console.log("newX[i + K]:" + newX[i + K])
        // console.log("新しい値:" + memo)
        
        answer = Math.min(memo, answer);
    }
    
    console.log(answer);
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
//  [go]
// by syu
package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var in = NewScanner(os.Stdin, 0)

func main() {
	N, K := in.Int(), in.Int()
	x := in.ArrayInt32(N)
	var min int32 = math.MaxInt32
	for i := 0; i < N-K+1; i++ {
		a, b := x[i], x[i+K-1]
		sum := Min232(Abs32(a), Abs32(b)) + b - a
		if sum < min {
			min = sum
		}
	}
	Pln(min)
}

type Scanner struct {
	*bufio.Scanner
}

func NewScanner(r io.Reader, max int) *Scanner {
	s := bufio.NewScanner(r)
	s.Split(scanWords)
	if max <= 0 {
		max = 1048576
	}
	s.Buffer([]byte{}, max)
	return &Scanner{s}
}
func (s *Scanner) Int() int {
	s.Scan()
	i, e := strconv.ParseInt(s.Text(), 10, 64)
	if e != nil {
		panic(e)
	}
	return int(i)
}
func (s *Scanner) Int32() int32 {
	s.Scan()
	i, e := strconv.ParseInt(s.Text(), 10, 32)
	if e != nil {
		panic(e)
	}
	return int32(i)
}
func (s *Scanner) ArrayInt32(n int) []int32 {
	r := make([]int32, n)
	for i := 0; i < n; i++ {
		r[i] = s.Int32()
	}
	return r
}
func isSpace(b byte) bool {
	return b == ' ' || b == '\n' || b == '\r' || b == '\t'
}
func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	start := 0
	for start < len(data) && isSpace(data[start]) {
		start++
	}
	for i := start; i < len(data); i++ {
		if isSpace(data[i]) {
			return i + 1, data[start:i], nil
		}
	}
	if atEOF && len(data) > start {
		return len(data), data[start:], nil
	}
	return start, nil, nil
}
func Min232(a, b int32) int32 {
	if a < b {
		return a
	}
	return b
}
func Abs32(n int32) int32 {
	y := n >> 31
	return (n ^ y) - y
}
func Pln(s ...interface{}) {
	fmt.Println(s...)
}
//  [go]
// by syu
package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
)

var in = NewScanner(os.Stdin, 0)

func main() {
	N, K := in.Int(), in.Int()
	x := in.ArrayInt(N)
	min := math.MaxInt64
	for i := 0; i < N-K+1; i++ {
		a, b := x[i], x[i+K-1]
		sum := Min2(Abs(a), Abs(b)) + b - a
		if sum < min {
			min = sum
		}
	}
	Pln(min)
}

type Scanner struct {
	*bufio.Scanner
}

func NewScanner(r io.Reader, max int) *Scanner {
	s := bufio.NewScanner(r)
	s.Split(scanWords)
	if max <= 0 {
		max = 1048576
	}
	s.Buffer([]byte{}, max)
	return &Scanner{s}
}
func (s *Scanner) Int() int {
	s.Scan()
	i, e := strconv.ParseInt(s.Text(), 10, 64)
	if e != nil {
		panic(e)
	}
	return int(i)
}
func (s *Scanner) ArrayInt(n int) []int {
	r := make([]int, n)
	for i := 0; i < n; i++ {
		r[i] = s.Int()
	}
	return r
}
func isSpace(b byte) bool {
	return b == ' ' || b == '\n' || b == '\r' || b == '\t'
}
func scanWords(data []byte, atEOF bool) (advance int, token []byte, err error) {
	start := 0
	for start < len(data) && isSpace(data[start]) {
		start++
	}
	for i := start; i < len(data); i++ {
		if isSpace(data[i]) {
			return i + 1, data[start:i], nil
		}
	}
	if atEOF && len(data) > start {
		return len(data), data[start:], nil
	}
	return start, nil, nil
}
func Min2(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func Abs(n int) int {
	y := n >> 63
	return (n ^ y) - y
}
func Pln(s ...interface{}) {
	fmt.Println(s...)
}
//  [go]
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

func intMin(x, y int) int {
	return int(math.Min(float64(x), float64(y)))
}

func main() {
	sc.Split(bufio.ScanWords)

	N, K := scanInt(), scanInt()
	X := make([]int, N)
	for i := 0; i < N; i++ {
		X[i] = scanInt()
	}

	ans := X[K-1] - X[0]
	for i := K; i < N; i++ {
		ans = intMin(ans, X[i]-X[i+1-K])
	}
	fmt.Println(ans)
}
//  [go]
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func main() {
	var scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	var n, k int
	if scanner.Scan() {
		tmp := scanner.Text()
		n, _ = strconv.Atoi(tmp)
	}
	if scanner.Scan() {
		tmp := scanner.Text()
		k, _ = strconv.Atoi(tmp)
	}

	var xs []int
	for i := 0; i < n; i++ {
		if scanner.Scan() {
			tmp := scanner.Text()
			x, _ := strconv.Atoi(tmp)
			xs = append(xs, x)
		}
	}

	min := math.MaxInt32
	for i := 0; i < n-k+1; i++ {
		var v int
		if xs[i] >= 0 && xs[i+k-1] >= 0 {
			v = xs[i+k-1]
		} else if xs[i] <= 0 && xs[i+k-1] >= 0 {
			if math.Abs(float64(xs[i])) < math.Abs(float64(xs[i+k-1])) {
				v = xs[i+k-1] - 2*xs[i]
			} else {
				v = 2*xs[i+k-1] - xs[i]
			}
		} else {
			v = xs[i]
		}
		if min > v {

			min = v
		}
	}
	fmt.Println(min)
}
//  [ruby]
n, k = gets.split.map(&:to_i)
xs = gets.chomp.split.map(&:to_i)
min = Float::INFINITY
(0..n-k).each do |i|
  l = xs[i]
  r = xs[i+k-1]
  dist = (l.abs > r.abs) ? r.abs + r - l : l.abs + r - l
  if min > dist
    min = dist
  end
end

puts min
//  [ruby]
n, k = gets.split.map(&:to_i)
x = gets.split.map(&:to_i)
min = Float::INFINITY
0.upto(n - k) do |i|
  b = x[i + k - 1] - x[i]
  b += x[i + k - 1].abs > x[i].abs ? x[i].abs : x[i + k - 1].abs
  min = b if min > b
end
puts min
//  [ruby]
N, K = gets.split.map(&:to_i)
x = gets.split.map(&:to_i)

ans = 10**9

bound =  x.bsearch_index{|e| e>=0}

#全部負の座標のとき
if bound == nil
  puts x[N-K].abs
  exit
end

#x>=0からi個，x<0からK-i個
0.upto(K) do |i|
  next if N-bound < i || bound < K-i
  right =
    if i != 0
      x[bound+i-1]
    else
      0
    end
  left =
    if i != K
      x[bound - (K-i)].abs
    else
      0
    end

  tmp =
    if right > left
      2*left + right
    else
      2*right + left
    end

  ans = tmp if tmp < ans
end

puts ans
//  [ruby]
n, k = gets.split.map &:to_i
l, r = gets.split.map(&:to_i).partition { |x| x < 0 }
d_min = 10**9

l_count = [0, l.size - k + 1].max
lr_count = [l.size, r.size, k-1].min

# 原点より左だけ
d_min = [d_min, -l[-k]].min if l.size >= k

# 左と右両方
lr_count.times do |i|
  x = -l[l_count + i]
  y = r[i]
  d_min = [d_min, x + y + [x, y].min].min
end

# 原点より右だけ
d_min = [d_min, r[k-1]].min if r.size >= k

p d_min
//  [ruby]
n, k = gets.split.map &:to_i
x = gets.split.map &:to_i
a_prev = [x[0].abs, x[k-1].abs].min
d_min = a_prev + (1...k).sum { |i| x[i] - x[i-1] }
(n - k).times do |i|
  d = d_min - a_prev - (x[i + 1] - x[i]) + (x[i + k] - x[i + k - 1])
  a_prev = [x[i + 1].abs, x[i + k].abs].min
  d += a_prev
  d_min = [d_min, d].min
end
p d_min
//  [ruby]
n, k = gets.split.map &:to_i
x = gets.split.map &:to_i
d_min = x[0].abs + (1...k).sum { |i| x[i] - x[i-1] }
(x.size - k).times do |i|
  d = d_min - x[i].abs - (x[i + 1] - x[i]) + x[i + 1].abs + (x[i + k] - x[i + k - 1])
  d_min = [d_min, d].min
end
p d_min
//  [rust]
use proconio::input;
 
fn main() {
  input! {
    n: usize,
    mut k: usize,
    mut vals: [isize;n]
  }
  
  for (i, v) in vals.iter().enumerate() {
    if v == &0 {
      k -= 1;
      vals.remove(i);
      break;
    }
  }
  
  if k == 0 {
    println!("0");
    return
  }
  
  
  let mut min = isize::max_value();
  let mut i = k-1;
  while i < vals.len() && vals[i] < 0 {
    min = std::cmp::min(vals[i-k+1].abs(), min);
    i += 1;
  }
  
  let mut temp = 0;
  let mut last = 0;
  for i in 0..k {
    temp += (last - vals[i]).abs();
    last = vals[i];
  }
  
  min = std::cmp::min(min, temp);
  for i in k..vals.len() {
    temp -= vals[i-k].abs();
    temp -= (vals[i-k+1] - vals[i-k]).abs();
    temp += vals[i-k+1].abs();
    temp += (vals[i] - last).abs();
    last = vals[i];
    min = std::cmp::min(min, temp);
  }
  
  println!("{}", min);
}
//  [rust]
use proconio::input;
 
fn main() {
  input! {
    n: usize,
    mut k: usize,
    mut vals: [isize;n]
  }
  
  for (i, v) in vals.iter().enumerate() {
    if v == &0 {
      k -= 1;
      vals.remove(i);
      break;
    }
  }
  
  if k == 0 {
    println!("0");
    return
  }
  
  let mut min = 0;
  let mut last = 0;
  for i in 0..k {
    min += (last - vals[i]).abs();
    last = vals[i];
  }
  
  let mut temp = min;
  for i in k..vals.len() {
    temp -= vals[i-k].abs();
    temp -= (vals[i-k+1] - vals[i-k]).abs();
    temp += vals[i-k+1].abs();
    temp += (vals[i] - last).abs();
    last = vals[i];
    min = std::cmp::min(min, temp);
  }
  
  println!("{}", min);
}
//  [rust]
use std::cmp::min;
use proconio::{input, fastout};

#[fastout]
fn main() {
    input! {
        n: usize, k: usize,
        arr: [isize; n],
    }

    let mut ans = std::isize::MAX;
    for i in 0..n-k+1 {
        let s = &arr[i..i+k];
        let &f = s.first().unwrap();
        let &l = s.last().unwrap();
        if f < 0 && l > 0 { ans = min(ans, l-2*f) }
        else if f < 0 { ans = min(ans, 0-f) }
        else { ans = min(ans, l) }
    }

    println!("{}", ans);
}
//  [swift]
let input = readLine()!.characters.split(" ").map{Int(String($0))!}
let (N, K) = (input[0], input[1]-1)
let x = readLine()!.characters.split(" ").map{Int(String($0))!}
var ans = 300000000
for i in 0..<N-K {
    let time = min(abs(x[i]), abs(x[i+K])) + abs(x[i]-x[i+K])
    ans = min(time, ans)
}
print(ans)
//  [swift]
let input = readLine()!.characters.split(" ").map{Int(String($0))!}
let (N, K) = (input[0], input[1]-1)
let x = readLine()!.characters.split(" ").map{Int(String($0))!}
var ans = 200000000
for i in 0..<N-K {
    let time = min(abs(x[i]), abs(x[i+K])) + abs(x[i]-x[i+K])
    ans = min(time, ans)
}
print(ans)
//  [swift]
let input = readLine()!.characters.split(" ").map{Int(String($0))!}
let (N, K) = (input[0], input[1])
let x = readLine()!.characters.split(" ").map{Int(String($0))!}
let plus = x.filter{$0>=0}
let minus = x.reverse().filter{$0<0}
var ans = 200000000
for i in max(0, K-minus.count-1)..<min(K, plus.count) {
    var dist = 0
    dist += plus[i]
    if i+1 < K {
        dist -= minus[K-i-2]*2
    }
    ans = min(dist, ans)
}
for i in max(0, K-plus.count-1)..<min(K, minus.count) {
    var dist = 0
    dist -= minus[i]
    if i+1 < K {
        dist += plus[K-i-2]*2
    }
    ans = min(dist, ans)
}
print(ans)
//  [swift]
let input = readLine()!.characters.split(" ").map{Int(String($0))!}
let (N, K) = (input[0], input[1])
let x = readLine()!.characters.split(" ").map{Int(String($0))!}
let plus = x.filter{$0>=0}
let minus = x.reverse().filter{$0<0}
var ans = 100000000
for i in max(0, K-minus.count-1)..<min(K, plus.count) {
    var dist = 0
    dist += plus[i]
    if i+1 < K {
        dist -= minus[K-i-2]*2
    }
    ans = min(dist, ans)
}
for i in max(0, K-plus.count-1)..<min(K, minus.count) {
    var dist = 0
    dist -= minus[i]
    if i+1 < K {
        dist += plus[K-i-2]*2
    }
    ans = min(dist, ans)
}
print(ans)
//  [typescript]
const p = (...args) => {
  process.env.NODE_ENV === "test" ? console.log(...args) : ""
}

const solve = (k, xs) => {
  if (xs.length == 1) {
    return xs[0]
  }
  let r = Infinity
  for (var i = 0; i < xs.length - k + 1; i++) {
    let p = xs[i]
    let n = xs[i + k - 1]
    let pa = Math.abs(p)
    let na = Math.abs(n)
    if (n * p > 0) {
      r = Math.min(r, Math.max(na, pa))
    } else {
      let a = pa * 2 + na
      let b = na * 2 + pa

      r = Math.min(r, a, b)
    }
  }
  return r
  // ...
}
const main = (input) => {
  const [head, rest] = input.split("\n")
  const [n, k] = head.split(" ").map(Number)

  return solve(k, rest.split(" ").map(Number))
}

if (process.env.NODE_ENV !== "test") {
  const input = require("fs").readFileSync("/dev/stdin", "utf8")
  console.log(main(input.trim()))
}

if (process.env.NODE_ENV === "test") {
  // module.exports = (input) => solve(input.trim())
  // main(``)
  // solve(1, [2, 3, 4])
}
//  [typescript]
import * as fs from 'fs';

const input: string = fs.readFileSync('/dev/stdin', 'utf8');
const args: string[][] =
  input.split('\n').map(l => l.split(' '));

const [n, k] = args[0].map(x => parseInt(x));
const x = args[1].map(x => parseInt(x));

type Interval = {
  left: number
  right: number
}

const intervals: Interval[] = [];
for (let i = 0; i + k - 1 < n; i++) {
  intervals.push({
    left: x[i],
    right: x[i + k - 1]
  });
}

const time = intervals.map(getTime).reduce((x, y) => Math.min(x, y));

console.log(time);

function getTime(interval: Interval): number {
  const l = interval.left;
  const r = interval.right;
  if (l * r >= 0) {
    return Math.max(Math.abs(l), Math.abs(r))
  } else {
    if (Math.abs(l) < Math.abs(r)) {
      return 2 * Math.abs(l) + Math.abs(r)
    } else {
      return Math.abs(l) + 2 * Math.abs(r)
    }
  }
}
//  [typescript]
const p = (...args) => {
  process.env.NODE_ENV === "test" ? console.log(...args) : ""
}

const solve = (k, xs) => {
  let loop = xs.length
  if (xs.length == 1) {
    return xs[0]
  }
  let r = Infinity
  for (var i = 0; i < k - 1; i++) {
    let p = xs[i]
    let n = xs[i + k - 1]
    if (n * p > 0) {
      r = Math.min(r, Math.max(Math.abs(n), Math.abs(p)))
    } else {
      let a = Math.abs(p) * 2 + Math.abs(n)
      let b = Math.abs(n) * 2 + Math.abs(p)

      r = Math.min(r, a, b)
    }
  }
  return r
  // ...
}
const main = (input) => {
  const [head, rest] = input.split("\n")
  const [n, k] = head.split(" ").map(Number)

  return solve(k, rest.split(" ").map(Number))
}

if (process.env.NODE_ENV !== "test") {
  const input = require("fs").readFileSync("/dev/stdin", "utf8")
  console.log(main(input.trim()))
}

if (process.env.NODE_ENV === "test") {
  // module.exports = (input) => solve(input.trim())
  // main(``)
  // solve(1, [2, 3, 4])
}
//  [typescript]

const solve = (k, xs) => {
  let loop = xs.length
  if (xs.length == 1) {
    return xs[0]
  }
  let r = Infinity
  for (var i = 0; i < k - 1; i++) {
    let p = xs[i]
    let n = xs[i + k - 1]
    if (n * p > 0) {
      r = Math.min(r, Math.max(Math.abs(n), Math.abs(p)))
    } else {
      let a = Math.abs(p) * 2 + Math.abs(n)
      let b = Math.abs(n) * 2 + Math.abs(p)

      r = Math.min(r, a, b)
    }
  }
  return r
  // ...
}
const main = (input) => {
  const [head, rest] = input.split("\n")
  const [n, k] = head.split(" ").map(Number)

  return solve(k, rest.split(" ").map(Number))
}
//  [typescript]

const solve = (k, xs) => {
  let loop = xs.length
  let r = Infinity
  for (var i = 0; i < k - 1; i++) {
    let p = xs[i]
    let n = xs[i + k - 1]
    if (n * p > 0) {
      r = Math.min(r, Math.max(Math.abs(n), Math.abs(p)))
    } else {
      let a = Math.abs(p) * 2 + Math.abs(n)
      let b = Math.abs(n) * 2 + Math.abs(p)

      r = Math.min(r, a, b)
    }
  }
  return r
  // ...
}
const main = (input) => {
  const [head, rest] = input.split("\n")
  const [n, k] = head.split(" ").map(Number)

  return solve(k, rest.split(" ").map(Number))
}

if (process.env.NODE_ENV !== "test") {
  const input = require("fs").readFileSync("/dev/stdin", "utf8")
  console.log(main(input.trim()))
}
//  [python]
# C - Peaks
def main():
    N, M, *HAB = map(int, open(0).read().split())
    H, AB = [0] + HAB[:N], HAB[N:]
    is_good = [False] + [True] * N
    for a, b in zip(*[iter(AB)] * 2):
        if H[a] >= H[b]:
            is_good[b] = False
        if H[b] >= H[a]:
            is_good[a] = False
    print(sum(is_good))


if __name__ == "__main__":
    main()
//  [python]
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
INF = 10**10
MOD = 10**9 + 7

N, M = map(int,input().split())
ok = [1] * N
H = list(map(int,input().split()))
for m in range(M):
    a, b = map(int,input().split())
    a-=1
    b-=1
    if H[a] < H[b]:
        ok[a] = 0
    elif H[a] > H[b]:
        ok[b] = 0
    else:
        ok[a] = 0
        ok[b] = 0

print (sum(ok))
//  [python]
import sys
input = sys.stdin.readline
input = sys.stdin.buffer.readline

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    N, M = MI()
    H = LI()
    H.insert(0,0)
    A = [0]*M
    B = [0]*M
    ans = 0
    for i in range(M):
        A[i], B[i]=MI()

    Hnum = [0]*(N+1)
    Hnum[0] = 1

    for i in range(M):
        if H[A[i]]>H[B[i]]:
            Hnum[B[i]]+=1
        elif H[A[i]]<H[B[i]]:
            Hnum[A[i]]+=1
        elif H[A[i]]==H[B[i]]:
            Hnum[A[i]]+=1
            Hnum[B[i]]+=1
    print(Hnum.count(0))


    """
    for i in range(1,N+1):
        ng = 0
        for j in range(M):
            if A[j] == i:
                if H[i]<=H[B[j]]:
                    ng += 1
                    break
            if B[j] == i:
                if H[i]<=H[A[j]]:
                    ng += 1
                    break
        if ng == 0:
            ans += 1
    print(ans)"""


    


if __name__ == "__main__":
	main()
//  [python]
#ABC166
N,M=map(int,input().split())
H =list(map(int,input().split()))  #展望台の標高
A = list(range(1,N+1))
for i in range(M):
    a, b = map(int, input().split())
    if H[a-1] > H[b-1]:
        A[b] = 0
    elif H[a-1] < H[b-1]:
        A[a] = 0
    else:
        A[a] = 0
        A[b] = 0
print(N-A.count(0))
//  [python]
#ABC166
N,M=map(int,input().split())
H =list(map(int,input().split()))  #展望台の標高
A = list(range(N))
for i in range(M):
    a, b = map(int, input().split())
    if H[a-1] > H[b-1]:
        A[b-1] = 0
    elif H[a-1] < H[b-1]:
        A[a-1] = 0
    else:
        A[a-1] = 0
        A[b-1] = 0
print(N-A.count(0))
//  [python]
#ABC166
N,M=map(int,input().split())
H =list(map(int,input().split()))  #展望台の標高
A = list(range(N))
for i in range(M):
    a, b = map(int, input().split())
    if H[a-1] > H[b-1]:
        A[b-1] = 0
    elif H[a-1] < H[b-1]:
        A[a-1] = 0
    else:
        A[a-1] = 0 and A[b-1] = 0

print(N-A.count(0))
//  [java]
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        Main m = new Main(sc);
        m.show(m.solve());
        sc.close();
    }
    
	//フィールド
    Scanner sc;
    int N;
    int M;
    int[] H;
    int[] A;
    int[] B;
    
    //入力
    Main(Scanner sc) {
        this.sc = sc;
        this.N = sc.nextInt();
        this.M = sc.nextInt();
        this.H = new int[N];
        this.A = new int[N];
        this.B = new int[N];
        for (int i = 0; i < N; i++) {
        	this.H[i] = sc.nextInt();
        }
        for (int i = 0; i < M; i++) {
        	this.A[i] = sc.nextInt();
        	this.B[i] = sc.nextInt();
        }
    }
    
    //解答処理
    private int solve() {
        int count = 0;
        int[] m = new int[N];
        
        for (int i = 0; i < M; i++) {
        	m[A[i]-1] = Math.max(m[A[i]-1], H[B[i]-1]);
        	m[B[i]-1] = Math.max(m[B[i]-1], H[A[i]]-1);
        }
        
        for (int i = 0; i < N; i++) {
        	if (m[i] < H[i]) {
        		count++;
        	}
        }
        
        return count;
    }
    
    //出力
    public void show(int answer) {
        System.out.println(answer);
    }

}
//  [java]
import java.util.*;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		// 4 3
		// 1 2 3 4
		// 1 3
		// 2 3
		// 2 4
		Set<Integer> ans = new HashSet<>();
		for (int i = 0; i < n; i++) {
			ans.add(i + 1);
		}
		int[] heights = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			heights[i] = sc.nextInt();
		}
		
		for (int i = 0; i < m; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			if (heights[b] > a) {
				ans.remove(a);
			}
			if (heights[a] > b) {
				ans.remove(b);
			}
		}
		System.out.println(ans.size());
	}
}
//  [java]
import java.util.*;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		// 4 3
		// 1 2 3 4
		// 1 3
		// 2 3
		// 2 4
		Map<Integer, PriorityQueue<Integer>> graph = new HashMap<>();
		for (int i = 0; i < n; i++) {
			graph.put(i + 1, new PriorityQueue<>(m, Collections.reverseOrder()));
		}
		int[] heights = new int[n + 1];
		for (int i = 1; i <= n; i++) {
			heights[i] = sc.nextInt();
		}
		for (int i = 0; i < m; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			graph.get(a).add(heights[b]);
			graph.get(b).add(heights[a]);
		}
		int ans = 0;
		for (Map.Entry<Integer, PriorityQueue<Integer>> entry : graph.entrySet()) {
			int node = entry.getKey();
			PriorityQueue<Integer> maxHeap = entry.getValue();
			if (maxHeap.isEmpty() || maxHeap.peek() < heights[node]) {
				ans++;
			}
		}
		System.out.println(ans);
	}
}
//  [cpp]
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 130000, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = ((tmp & ma0) ^ ma0) ? 68 - __builtin_ctzll((tmp & ma0) ^ ma0) : 0;
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += (64 - dig >> 3);
	while ((ct = *ci++) >= '0') tmp = tmp * 10 + ct - '0';
	return tmp;
}

int H[100001];
bool NG[100001];
int main() {
	//cin.tie(0);
	//ios::sync_with_stdio(false);


	int N = getint(), M = getint();
	rep1(i, N) H[i] = getint();
	rep(i, M) {
		int a = getint(), b = getint();
		if (H[a] >= H[b]) NG[b] = 1;
		if (H[a] <= H[b]) NG[a] = 1;
	}
	int kotae = N;
	rep1(i, N) kotae -= NG[i];

	printf("%d", kotae);

	Would you please return 0;
}
//  [cpp]
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>

//同じコードでそんなにばらつくんですか

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 130000, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = ((tmp & ma0) ^ ma0) ? 68 - __builtin_ctzll((tmp & ma0) ^ ma0) : 0;
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += (64 - dig >> 3);
	while ((ct = *ci++) >= '0') tmp = tmp * 10 + ct - '0';
	return tmp;
}

int H[100001];
bool NG[100001];
int main() {
	//cin.tie(0);
	//ios::sync_with_stdio(false);


	int N = getint(), M = getint();
	rep1(i, N) H[i] = getint();
	rep(i, M) {
		int a = getint(), b = getint();
		if (H[a] >= H[b]) NG[b] = 1;
		if (H[a] <= H[b]) NG[a] = 1;
	}
	int kotae = N;
	rep1(i, N) kotae -= NG[i];

	printf("%d", kotae);

	Would you please return 0;
}
//  [cpp]
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>

//同じコードでそんなにばらつくんですか

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 130000, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = ((tmp & ma0) ^ ma0) ? 68 - __builtin_ctzll((tmp & ma0) ^ ma0) : 0;
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += (64 - dig >> 3);
	while ((ct = *ci++) >= '0') tmp = tmp * 10 + ct - '0';
	return tmp;
}

bool NG[100001];
int main() {
	//cin.tie(0);
	//ios::sync_with_stdio(false);


	int N = getint(), M = getint();
	int H[100001];
	rep1(i, N) H[i] = getint();
	rep(i, M) {
		int a = getint(), b = getint();
		if (H[a] >= H[b]) NG[b] = 1;
		if (H[a] <= H[b]) NG[a] = 1;
	}
	int kotae = N;
	rep1(i, N) kotae -= NG[i];

	printf("%d", kotae);

	Would you please return 0;
}
//  [cpp]
#include<bits/stdc++.h>
using namespace std;

int main(){
  int N,M;cin>>N>>M;
  vector<int> H(N+1,);
  for(int i=1;i<=N;i++) cin>>H.at(i);
  vector<bool> good(N+1,true);
  int count=N;
  for(int j=0;j<M;j++){
    int a,b;cin>>a>>b;
    if(H.at(a)>=H.at(b) && good.at(b)){
      good.at(b)=false;
      count--;
    }
    if(H.at(a)<=H.at(b) && good.at(a)){
      good.at(a)=false;
      count--;
    }
  }
  cout<<count<<endl;
}
//  [cpp]
#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

// #define N 100010

// int main(){
//     int n, m, h[N], ma[N];
//     cin >> n >> m;
//     rep(i,n){
//         cin >> h[i];
//         ma[i] = 0;
//     }
//     rep(i,m){
//         int a, b;
//         cin >> a >> b;
//         ma[a-1] = max(ma[a-1], h[b-1]);
//         ma[b-1] = max(ma[b-1], h[a-1]);
//     }
//     int ans = 0;
//     rep(i,n) {
//         if(h[i] > ma[i]) ans++;
//     }
//     cout << ans << endl;
//     return 0;
// }

int main(){
    int n, m;
    cin  >> n >> m;
    vector<int> h(n);
    vector<bool> ok(n, true);
    rep(i,m) cin >> h[i];
    rep(i,m) {
        int a,b;
        cin >> a >> b;
        if(h[a-1] <= h[b-1]) ok[a-1] = false;
        if(h[b-1] <= h[a-1]) ok[b-1] = false;
    }
    int ans = 0;
    rep(i,n) if(ok[i]) ans++;
    cout << ans << endl;
	return 0;
}
//  [cpp]
#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

#define N 100010

int main(){
    int n, m, h[N], ma[N];
    cin >> n >> m;
    for(int i = 0; i <= n; i++){
        cin >> h[i];
        ma[i] = 0;
    }
    rep(i,m){
        int a, b;
        cin >> a >> b;
        ma[a] = max(ma[a], h[b]);
        ma[b] = max(ma[b], h[a]);
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        ans += h[i] > ma[i];
    }
    cout << ans << endl;
    return 0;
}
//  [c]
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
#ifdef __cplusplus
#include <bits/stdc++.h>
#endif

char stdibuf[2500015];
uint32_t stdibufl;

bool arr[101];

uint32_t nextpint(void){ char c=stdibuf[stdibufl++]; while(c<'0'||'9'<c) c=stdibuf[stdibufl++]; uint32_t x=0; while('0'<=c && c<='9'){ x=x*10+c-'0'; c=stdibuf[stdibufl++]; } return x; }
void printint(uint32_t x){ if(x<0){x=-x; putchar_unlocked('-');}else if(x==0){putchar_unlocked('0');} int d=0; char s[10]; while(x){ s[d]='0'+x%10; x/=10; d++; } for (int i=d-1; i>=0; i--) putchar_unlocked(s[i]); putchar_unlocked('\n'); }

uint32_t h[100000];
uint8_t is[100000];

int main(void){
	read(0, stdibuf, 2500014);
	uint32_t n=nextpint();
	uint32_t m=nextpint();
	for(uint32_t i=0; i<n; i++){
		h[i]=nextpint();
	}
	for(uint32_t i=0; i<m; i++){
		uint32_t a=nextpint()-1;
		uint32_t b=nextpint()-1;
		if(h[a]<=h[b]) is[a]=1;
		if(h[a]>=h[b]) is[b]=1;
	}
	uint32_t ans=0;
	for(uint32_t i=0; i<n; i++){
		ans+=is[i];
	}
	printint(n-ans);
}
//  [c]
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
#ifdef __cplusplus
#include <bits/stdc++.h>
#endif

bool arr[101];

uint32_t nextpint(void){ char c=getchar_unlocked(); while(c<'0'||'9'<c) c=getchar_unlocked(); uint32_t x=0; while('0'<=c && c<='9'){ x=x*10+c-'0'; c=getchar_unlocked(); } return x; }
void printint(uint32_t x){ if(x<0){x=-x; putchar_unlocked('-');}else if(x==0){putchar_unlocked('0');} int d=0; char s[10]; while(x){ s[d]='0'+x%10; x/=10; d++; } for (int i=d-1; i>=0; i--) putchar_unlocked(s[i]); putchar_unlocked('\n'); }

uint32_t h[100000];
bool is[100000];

int main(void){
	uint32_t n=nextpint();
	uint32_t m=nextpint();
	for(uint32_t i=0; i<n; i++){
		h[i]=nextpint();
	}
	for(uint32_t i=0; i<m; i++){
		uint32_t a=nextpint()-1;
		uint32_t b=nextpint()-1;
		if(h[a]>h[b]){
			is[b]=true;
		}else if(h[a]<h[b]){
			is[a]=true;
		}else{
			is[a]=true;
			is[b]=true;
		}
	}
	uint32_t ans=0;
	for(uint32_t i=0; i<n; i++){
		if(!is[i]) ans++;
	}
	printint(ans);
}
//  [c]
#include <stdio.h>

int main(void)
{
    int n=0,m=0,a=0,b=0,count=0;
    int H[100000]={},flag[100000]={};
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%d",&H[i]);
    }
    for(int i=0;i<m;i++){
        scanf("%d %d",&a,&b);
        if(H[a-1]>H[b-1]){
            flag[a-1]=1;
            flag[b-1]=-1;
        } else if(H[a-1]==H[b-1]){
            flag[a-1]=-1;
            flag[b-1]=-1;
        } else {
            flag[b-1]=1;
            flag[a-1]=-1;
        }
    }
    for(int i=0;i<n;i++){
        if(flag[i]>=0) count+=1;
    }

    printf("%d\n",count);
    return 0;
}
//  [c]
#include<stdio.h>
int main(){
	int n,m,i,a,b,ans;
	scanf("%d %d",&n,&m);
	int h[n];
	int count[n];
	for(i=1;i<=n;i++){
	scanf("%d",&h[i]);
	count[i]=1;
	}
	for(i=1;i<=m;i++){
		scanf("%d %d",&a,&b);
		if(h[a]>=h[b]){
			count[b]=0;
		}
		if(h[a]<=h[b])
		count[a]=0;
	}
	int count=0;
	for(i=1,ans=0;i<=n;i++){
		if(count[i]==1)
		ans++;
	}
	printf("%d",ans);
}
//  [c]
#include<stdio.h>
int main(){
	int n,m,a,b,i,ans;
	scanf("%d %d",&n,&m);
	int h[n+1],count[n+1];
	for(i=1;i<=n;i++){
	scanf("%d",&h[i]);
	count[i]=1;
	}
	for(i=1;i<=m;i++){
		scanf("%d %d",&a,&b);
		if(h[a]>=h[b]){
			count[b]=0;
		}
		if(h[a]<=h[b])
		count[a]=0;
    }
	for(int i=1;i<=n;i++){
		if(count[i]==1)
		ans++;
	}
	printf("%d",ans);
}
//  [javascript]
"use strict";
var input = require("fs").readFileSync("/dev/stdin", "utf8");
var cin = input.split(/ |\n/), cid = 0;
function next(a) { return a ? cin[cid++] : +cin[cid++]; }
function nexts(n, a) { return a ? cin.slice(cid, cid += n) : cin.slice(cid, cid += n).map(a => +a); }
function nextm(h, w, a) { var r = [], i = 0; if (a) for (; i < h; i++)r.push(cin.slice(cid, cid += w)); else for (; i < h; i++)r.push(cin.slice(cid, cid += w).map(a => +a)); return r; }
function xArray(v) { var a = arguments, l = a.length, r = "Array(a[" + --l + "]).fill().map(x=>{return " + v + ";})"; while (--l) r = "Array(a[" + l + "]).fill().map(x=>" + r + ")"; return eval(r); }

var myOut = main();
if (myOut !== undefined) console.log(myOut);

function main() {
    //console.log(cin);
    cin = cin.map(i => +i);
    let n = cin.shift();
    let m = cin.shift();
    let h = cin.slice(0, n); cin = cin.slice(n);
    h.unshift(0);
    let a;
    let b;
    let good = Array(n + 1).fill(1);
    good[0] = 0;
    for (let i = 0; i < m; i++) {
        // A.push(cin[2 * i]);
        // B.push(cin[2 * i + 1]);
        a = cin[2 * i];
        b = cin[2 * i + 1];
        if (h[a] >= h[b]) {
            good[b] = 0;
        }
        if (h[b] >= h[a]) {
            good[a] = 0;
        }
    }
    // console.log("n", n);
    // console.log("m", m);
    // console.log("h", h);
    // console.log("A", A);
    // console.log("B", B);
    let ans = 0;
    good.forEach(i => { ans += i; });

    return ans;
}