//q4

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

public class q4 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));

        int query = in.nextInt();

        while (query -- > 0) {
            int n = in.nextInt();
            int k = in.nextInt();

            char[] arr = new char[n];
            //slot all n into char array
            String code = in.next();
            for (int i = 0; i < n; i++) {
                arr[i] = code.charAt(i);

            }

            //R, G, B cycle
            int r = 0;
            int g = 0;
            int b = 0;

            for (int i = 0; i < k; i++) {
                if (i % 3 == 0) {
                    if (arr[i] == 'R') {g++; b++;}
                    else if (arr[i] == 'G') {r++; b++;}
                    else {r++; g++;} //if is 'B'
                } else if (i % 3 == 1) {
                    if (arr[i] == 'G') {g++; b++;}
                    else if (arr[i] == 'B') {r++; b++;}
                    else {r++; g++;} //if is 'R'
                } else { //if mod 3 is 2
                    if (arr[i] == 'B') {g++; b++;}
                    else if (arr[i] == 'R') {r++; b++;}
                    else {r++; g++;} //if is 'G'
                }
            }

            //starting from kth position, if different then add 1, and check (j-k)th position
            int rMin = r;
            int gMin = g;
            int bMin = b;
            for (int j = k; j < n; j++) {
                //R cycle
                if ((j % 3 == 0 && arr[j] != 'R') ||
                    (j % 3 == 1 && arr[j] != 'G') ||
                    (j % 3 == 2 && arr[j] != 'B')) {
                    r++;
                }
                //R cycle
                if (((j - k) % 3 == 0 && arr[j - k] != 'R') ||
                    ((j - k) % 3 == 1 && arr[j - k] != 'G') ||
                    ((j - k) % 3 == 2 && arr[j - k] != 'B')) {
                    r--;
                }
                rMin = Math.min(r, rMin);
                //G cycle
                if ((j % 3 == 0 && arr[j] != 'G') ||
                    (j % 3 == 1 && arr[j] != 'B') ||
                    (j % 3 == 2 && arr[j] != 'R')) {
                    g++;
                }
                if (((j - k) % 3 == 0 && arr[j - k] != 'G') ||
                    ((j - k) % 3 == 1 && arr[j - k] != 'B') ||
                    ((j - k) % 3 == 2 && arr[j - k] != 'R')) {
                    g--;
                }
                gMin = Math.min(gMin, g);
                //B cycle
                if ((j % 3 == 0 && arr[j] != 'B') ||
                    (j % 3 == 1 && arr[j] != 'R') ||
                    (j % 3 == 2 && arr[j] != 'G')) {
                    b++;
                }
                if (((j - k) % 3 == 0 && arr[j - k] != 'B') ||
                    ((j - k) % 3 == 1 && arr[j - k] != 'R') ||
                    ((j - k) % 3 == 2 && arr[j - k] != 'G')) {
                    b--;
                }
                bMin = Math.min(bMin, b);

            }

            out.println(Math.min(Math.min(rMin, gMin), bMin));

        }
        out.flush();

    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.TreeMap;
import java.util.Map;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Ribhav
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CGlassCarving solver = new CGlassCarving();
        solver.solve(1, in, out);
        out.close();
    }

    static class CGlassCarving {
        public void solve(int testNumber, FastReader s, PrintWriter out) {
            TreeMap<Long, Integer> mapH = new TreeMap<>();
            TreeMap<Long, Integer> mapV = new TreeMap<>();
            TreeMap<Long, Integer> hDiff = new TreeMap<>();
            TreeMap<Long, Integer> vDiff = new TreeMap<>();
            long width = s.nextInt();
            long height = s.nextInt();
            mapH.put(0L, 1);
            mapV.put(0L, 1);
            mapV.put(width, 1);
            mapH.put(height, 1);
            vDiff.put(width, 1);
            hDiff.put(height, 1);
            long maxV = height;
            long maxH = width;
            int n = s.nextInt();
            for (int i = 0; i < n; i++) {
                char ch = s.nextCharacter();
                long cut = s.nextInt();
                if (ch == 'H') {
                    Long next = mapH.higherKey(cut);
                    Long prev = mapH.lowerKey(cut);
                    Long diff = next - prev;
                    int freq = hDiff.get(diff);
                    if (freq == 1) {
                        hDiff.remove(diff);
                    } else {
                        hDiff.put(diff, freq - 1);
                    }
                    hDiff.put(next - cut, hDiff.getOrDefault(next - cut, 0) + 1);
                    hDiff.put(cut - prev, hDiff.getOrDefault(cut - prev, 0) + 1);
                    mapH.put(cut, mapH.getOrDefault(cut, 0) + 1);
                } else {
                    Long next = mapV.higherKey(cut);
                    Long prev = mapV.lowerKey(cut);
                    Long diff = next - prev;
                    int freq = vDiff.get(diff);
                    if (freq == 1) {
                        vDiff.remove(diff);
                    } else {
                        vDiff.put(diff, freq - 1);
                    }
                    vDiff.put(next - cut, vDiff.getOrDefault(next - cut, 0) + 1);
                    vDiff.put(cut - prev, vDiff.getOrDefault(cut - prev, 0) + 1);
                    mapV.put(cut, mapV.getOrDefault(cut, 0) + 1);
                }

                out.println(hDiff.lastKey() * vDiff.lastKey());
            }
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(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 nextInt() {
            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 boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public char nextCharacter() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            return (char) c;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}


import java.io.*;

public class First {
    StreamTokenizer in;
    PrintWriter out;

    int nextInt() throws IOException {
        in.nextToken();
        return (int)in.nval;
    }

    long nextLong() throws IOException {
        in.nextToken();
        return (long) in.nval;
    }

    String nextString() throws IOException {
        in.nextToken();
        return in.sval;
    }

    void run() throws IOException {
        in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }

    void solve() throws IOException {
        int n = nextInt(), k = nextInt(), sum = 0, count = 0;
        String str = nextString();
        char[] arr = str.toCharArray();
        boolean[] bool = new boolean[26];
        for(char ch: arr){
            bool[((int)ch)-97] = true;
        }
        for(int i = 0; i < 26; i++){
            if(bool[i]){
                sum += i+1;
                count++;
                i += 1;
            }
            if(count == k) break;
        }
        if(count == k) out.println(sum);
        else out.println(-1);
    }

    public static void main(String[] args) throws IOException {
        new First().run();
    }
}


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

import static java.lang.System.out;

public class Main {

    private FastScanner scanner = new FastScanner();

    public static void main(String[] args) {
        new Main().solve();
    }

    private List<Integer>[] gr = new ArrayList[1000_000+5];
    private int dp[][] = new int[21][1000_000+5];
    private boolean used[] = new boolean[1000_000+5];

    void init(int v, int p) {
        Stack<Integer> st = new Stack<>();
        st.push(v);
        st.push(p);
        while (!st.isEmpty()) {
            p = st.pop();
            v = st.pop();
            used[v] = true;

            dp[0][v] =  p;
            for (int i = 1; i <= 20; i++) {
                if (dp[i - 1][v] != -1) {
                    dp[i][v] = dp[i - 1][dp[i - 1][v]];
                }
            }

            for (int next : gr[v]) {
                if (!used[next]) {
                    st.push(next);
                    st.push(v);
                }
            }

        }
    }

    private void solve() {
        int n = scanner.nextInt(), k = scanner.nextInt();
        boolean[] ans = new boolean[1000_000 + 5];

        for (int i = 0; i < n; i++) {
            gr[i] = new ArrayList<>();
        }

        for (int i = 0; i < n - 1; i ++) {
            int u = scanner.nextInt() - 1, v = scanner.nextInt() - 1;
            gr[u].add(v);
            gr[v].add(u);
        }

        k = n - k - 1;
        ans[n - 1] = true;

        init(n - 1 , n - 1);

        int t, d, next;
        for (int i = n - 2; i >= 0; i--) {
            t = i;
            d = 1;
            if (ans[i]) {
                continue;
            }
            for (int j = 20; j >= 0; j--){
                next = dp[j][t];
                if (next != -1 && !ans[next]) {
                    t = next;
                    d +=  1 << j;
                }
            }

            if (d <= k) {
                k -=d;
                t = i;

                while (!ans[t]) {
                    ans[t] = true;
                    t = dp[0][t];
                }
            }
            if (k == 0) {
                break;
            }
        }
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < n; i++) {
            if (!ans[i]) {
                sb.append(i + 1).append(" ");
            }
        }
        System.out.println(sb.toString());
    }

    class FastScanner {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public FastScanner() {
            reader = new BufferedReader(new InputStreamReader(System.in), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

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

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String nextLine() {
            try {
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}


import java.util.*;

public class Main{
    private static final int MAX_SIZE = 100005;
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int a = sc.nextInt();
        int b = sc.nextInt();
        if(((m + 1) / 60 < a) || ((m + 1) / 60 == a && (m + 1) % 60 <= b)) {
            out(0, 0);
            System.exit(0);
        }
        for(int i = 2; i <= n; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();

            int bb = b + 2 * m + 2;
            int aa = a + bb / 60;
            bb %= 60;

            if((aa < x) || (aa == x && bb <= y)) {
                b = b + m + 1;
                a = a + b / 60;
                b %= 60;
                out(a, b);
                System.exit(0);
            }
            a = x;
            b = y;
        }
        b = b + m + 1;
        a = a + b / 60;
        b = b % 60;
        out(a, b);
    }

    private static void out(int a, int b) {
        cout(a);
        cout(" ");
        cout(b);
    }

    private static void cout(Object a) {
        System.out.print(a);
    }
}


import java.util.Scanner;

public class Solution {

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		String s = sc.next();

		StringBuilder ans = new StringBuilder();
		int count = 0;
		int open = 0;
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) == '(') {
				ans.append("(");
				count++;
				open++;
			} else {
				ans.append(")");
				open--;
			}
			if (count == k / 2) {
				break;
			}
		}

		while (open > 0) {
			ans.append(")");
			open--;
		}
		System.out.println(ans.toString());
	}
}


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

import static java.lang.Math.max;
import static java.lang.Math.min;

public class Main {
    FastScanner in;
    PrintWriter out;

    void run() {
        in = new FastScanner();
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }

    class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastScanner(String s) {
            try {
                br = new BufferedReader(new FileReader(s));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                }
            }
            return st.nextToken();
        }

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

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

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

    public static void main(String[] args) {
        new Main().run();
    }

    void solve() {
        int t = in.nextInt();

        for (int sdfsdf = 0; sdfsdf < t; sdfsdf++) {
            long n = in.nextLong();
            long k = in.nextLong();

            if (n == 1) {
                if (k == 1)  {
                    out.println("YES 0");
                } else {
                    out.println("NO");
                }
                continue;
            }

            if (k == 3) {
                if (n == 2) {
                    out.println("NO");
                } else {
                    out.println("YES " + (n - 1));
                }
                continue;
            }

            long cuts = 1;
            long squares = 4;
            int zoom = 1;

            while (k > cuts + squares) {
                cuts += squares;
                squares *= 4;
                zoom++;
            }

            if (zoom > n) {
                out.println("NO");
                continue;
            }

            if (zoom == n && k > cuts) {
                out.println("NO");
                continue;
            }

            long current_cuts = k - cuts;

            if (current_cuts > squares - (2L * Math.sqrt(squares) - 1L)) {
                out.println("YES " + (n - zoom - 1L));
            } else {
                out.println("YES " + (n - zoom));
            }
        }
    }
}


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
//package newpackage;
import java.util.*;
/**
 *
 * @author parpaorsa
 */
public class NewClass {
    static Scanner in=new Scanner(System.in);
    public static void main(String[] args) {
        int n = in.nextInt(),ans=Integer.MAX_VALUE,t=0;
        String x = in.next();
        for (int i = 0; i < n; i++) {
            if(x.charAt(i)=='-')t--;
            else t++;
            ans=Math.min(ans,t);
        }
           if(ans <= 0)
               System.out.println(Math.abs(ans)+t);
           else
               System.out.println(t);
    }

}


import java.util.*;
import java.io.*;
import java.math.BigInteger;

public class Problem
{

    static int mod = (int) (1e9+7);
    static InputReader in;
    static PrintWriter out;
    static int[] rt;
    static int[] size;

    static void initialize(int n){
        rt = new int[n + 1];
        size = new int[n + 1];
        for(int i = 0; i < rt.length; i++){
            rt[i] = i;
            size[i] = 1;
        }
    }

    static int root(int x){
        while(rt[x] != x){
            rt[x] = rt[rt[x]];
            x = rt[x];
        }
        return x;
    }

    static long union(int x,int y){
        int root_x = root(x);
        int root_y = root(y);
        if(root_x == root_y) return 0;
        long val = size[root_x] *1l* size[root_y];
        if(size[root_x]<size[root_y]){
            rt[root_x] = rt[root_y];
            size[root_y] += size[root_x];
        }
        else{
            rt[root_y] = rt[root_x];
            size[root_x] += size[root_y];
        }

        return val;
    }

    static void solve()
    {
        in = new InputReader(System.in);
        out = new PrintWriter(System.out);

        int t = 1;

        while(t-- > 0){
            int n = in.nextInt();
            int[] arr = in.nextIntArray(n);
            ArrayList<Pair> list = new ArrayList<>();

            for(int i = 1; i < n; i++){
                int u = in.nextInt() - 1;
                int v = in.nextInt() - 1;
                list.add(new Pair(u, v, Math.max(arr[u],arr[v])));
            }
            list.sort((p1,p2) -> Integer.compare(p1.i, p2.i));
            initialize(n);
            long s1 = 0;
            for(int i = 0; i < list.size(); i++){
                s1 += union(list.get(i).x, list.get(i).y) * list.get(i).i;
            }
            for(int i = 0; i < list.size(); i++){
                Pair p = list.get(i);
                p.i = Math.min(arr[p.x],arr[p.y]);
            }
            list.sort((p1,p2) -> -Integer.compare(p1.i, p2.i));
            initialize(n);
            long s2 = 0;
            for(int i = 0; i < list.size(); i++){
                s2 += union(list.get(i).x, list.get(i).y) * list.get(i).i;
            }

            out.println(s1 - s2);
        }

        out.close();
    }

    public static void main(String[] args)
    {
        new Thread(null ,new Runnable(){
            public void run(){
                try{
                    solve();
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        },"1",1<<26).start();

    }

    static class Pair implements Comparable<Pair>
    {

        int x,y;
        int i;

        Pair (int x,int y)
        {
                this.x = x;
                this.y = y;
        }

        Pair (int x,int y, int i)
        {
                this.x = x;
                this.y = y;
                this.i = i;
        }

        public int compareTo(Pair o)
        {
            if(this.x != o.x)
                return -Integer.compare(this.x, o.y);
            return -Integer.compare(this.y,o.y);
                //return 0;
        }

        public boolean equals(Object o)
        {
            if (o instanceof Pair)
            {
                Pair p = (Pair)o;
                return p.x == x && p.y==y;
            }
            return false;
        }

        @Override
        public String toString()
        {
            return x + " "+ y + " "+i;
        }

        /*public int hashCode()
        {
            return new Long(x).hashCode() * 31 + new Long(y).hashCode();
        }*/

    }

    static long add(long a,long b){
        long x=(a+b);
        while(x>=mod) x-=mod;
        return x;
    }

    static long sub(long a,long b){
        long x=(a-b);
        while(x<0) x+=mod;
        return x;
    }

    static long mul(long a,long b){
        long x=(a*b);
        while(x>=mod) x-=mod;
        return x;
    }

    static String rev(String s){
        StringBuilder sb=new StringBuilder(s);
        sb.reverse();
        return sb.toString();
    }

    static long gcd(long x,long y)
    {
        if(y==0)
                return x;
        else
                return gcd(y,x%y);
    }

    static int gcd(int x,int y)
    {
        if(y==0)
                return x;
        else
                return gcd(y,x%y);
    }

    static long pow(long n,long p,long m)
    {
         long  result = 1;
          if(p==0){
            return n;
          }

        while(p!=0)
        {
            if(p%2==1)
                result *= n;
            if(result >= m)
               result %= m;
            p >>=1;
            n*=n;
            if(n >= m)
                n%=m;
        }

        return result;
    }

    static long pow(long n,long p)
    {
        long  result = 1;
          if(p==0)
            return 1;

        while(p!=0)
        {
            if(p%2==1)
                result *= n;
            p >>=1;
            n*=n;
        }
        return result;
    }

    static void debug(Object... o)
    {
            System.out.println(Arrays.deepToString(o));
    }

    static class InputReader
    {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream)
        {
                this.stream = stream;
        }

        public int snext()
        {
                if (snumChars == -1)
                        throw new InputMismatchException();
                if (curChar >= snumChars)
                {
                        curChar = 0;
                        try
                        {
                                snumChars = stream.read(buf);
                        } catch (IOException e)
                        {
                                throw new InputMismatchException();
                        }
                        if (snumChars <= 0)
                                return -1;
                }
                return buf[curChar++];
        }

        public int nextInt()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                int sgn = 1;
                if (c == '-')
                {
                        sgn = -1;
                        c = snext();
                }
                int res = 0;
                do
                {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        res *= 10;
                        res += c - '0';
                        c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
        }

        public long nextLong()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                int sgn = 1;
                if (c == '-')
                {
                        sgn = -1;
                        c = snext();
                }
                long res = 0;
                do
                {
                        if (c < '0' || c > '9')
                                throw new InputMismatchException();
                        res *= 10;
                        res += c - '0';
                        c = snext();
                } while (!isSpaceChar(c));
                return res * sgn;
        }

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

        public long[] nextLongArray(int n)
        {
                long a[] = new long[n];
                for (int i = 0; i < n; i++)
                {
                        a[i] = nextLong();
                }
                return a;
        }

        public String readString()
        {
                int c = snext();
                while (isSpaceChar(c))
                {
                        c = snext();
                }
                StringBuilder res = new StringBuilder();
                do
                {
                        res.appendCodePoint(c);
                        c = snext();
                } while (!isSpaceChar(c));
                return res.toString();
        }

        public String nextLine()
        {
                int c = snext();
                while (isSpaceChar(c))
                        c = snext();
                StringBuilder res = new StringBuilder();
                do
                {
                        res.appendCodePoint(c);
                        c = snext();
                } while (!isEndOfLine(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;
        }

        private boolean isEndOfLine(int c)
        {
                return c == '\n' || c == '\r' || c == -1;
        }

        public interface SpaceCharFilter
        {
                public boolean isSpaceChar(int ch);
        }

    }
}


import java.util.*;
public class TestClass
{
   public static void main(String args[])
   {
   	Scanner in = new Scanner(System.in);
   	int n = in.nextInt();
   	int m = in.nextInt();
   	int k = in.nextInt();
   	int arr[] = new int[n+1];
   	for(int i =0;i<n;i++)
   	 arr[i+1]= in.nextInt();

   	long sum[] = new long [n+1];

   	for(int i=1;i<=n;i++)
   	 sum[i]=sum[i-1]+arr[i];

    long dp[] = new long[n+1];

    for(int i =1;i<=n;i++)
    {
      for(int j=i;j>i-m&&j>=1;j--)
      {
        long val = sum[i]-sum[j-1]+dp[j-1]-k;
        dp[i]= Math.max(dp[i],val);
      }
    }
    long max =0;
    for(int i =1;i<=n;i++)
     max=Math.max(max,dp[i]);

    System.out.println(max);

   }
}


import java.util.*;
public class test{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int m=s.nextInt();
int arr[]=new int[n];
int max = Integer.MIN_VALUE;
        long sum = 0;
        for(int i=0;i<n;i++)
        {
            arr[i] = s.nextInt();
            sum = sum + arr[i];
            max = Math.max(max,arr[i]);
        }
        Arrays.sort(arr);
        int i = 0;
        int count = 0;
        int d = 0;
        for(i=0; i<n; i++)
        {
            if(arr[i] > d)
            {
                count++;
                d++;
            }
            else if(arr[i] == d && arr[i] > 0)
            {
                count++;
            }
        }
        //System.out.println(count + " " + max);
        if(max - d > 0)
        {
            count = count + max - d;
        }
        System.out.println(sum - count);}}


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

public class tr {
	static int[][] ad;
    static boolean []vis;
    static int []goods;
    static int[][]sol;
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		int n=sc.nextInt();
		int []a=new int [n];
		int max=0;
		for(int i=0;i<n;i++)
			a[i]=sc.nextInt();
		Stack<Integer> s=new Stack<>();
		boolean f=true;
				for(int i=0;i<n;i++) {
					max=Math.max(max,a[i]);
					if(!s.isEmpty() && a[i]>s.peek())
						f=false;
					s.push(a[i]);
					while(!s.isEmpty()) {
						int high=s.pop();
						if(s.isEmpty() || s.peek()!=high) {
							s.push(high);
							break;
						}
						s.pop();
					}
				//	System.out.println(s+" "+max);
				}
				//System.out.println(f+" "+max);
				if(f && s.size()==0)
					out.println("YES");
				else if(f && s.size()==1 && s.peek()==max)
					out.println("YES");
				else
					out.println("NO");
		 out.flush();
	}
	static int gcd(int a, int b) {
		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	static class pair implements Comparable<pair> {
		int a;
		int b;

		pair(int a, int b) {
			this.a = a;
			this.b = b;
		}

		public String toString() {
			return a + " " + b;
		}

		@Override
		public int compareTo(pair o) {
			return  o.a-a ;
		}
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		public String nextLine() throws IOException {
			return br.readLine();
		}

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

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

		public char nextChar() throws IOException {
			return next().charAt(0);
		}

		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}


import java.util.Scanner;

public class MargariteBestPresent_1080B {

	private static int f(int x) {
		return (x%2==0)?x/2:(x-1)/2-x;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n,r,l;
		n = sc.nextInt();
		while(n-->0) {
			l = sc.nextInt();
			r = sc.nextInt();

			System.out.println(f(r)-f(l-1));
		}
		sc.close();
	}

}


import java.util.*;

public class helloWorld
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int m = in.nextInt();
		int l = 1000, r = 0, u = 1000, b = 0;

		for(int i = 0; i < n; i++ ) {
			String str = in.next();
			for(int j = 0; j < m; j++)
				if(str.charAt(j) == 'B') {
					l = Math.min(j+1, l);
					r = Math.max(j+1, r);
					u = Math.min(i+1, u);
					b = Math.max(i+1, b);
				}
		}

		System.out.println((u+b)/2 + " " + (l+r)/2);

		in.close();
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author pandusonu
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            // out.print("Case #" + testNumber + ": ");
            int n = in.readInt();
            int[] a = in.readIntArray(n);
            int[][] sol = new int[n][n];
            for (int i = 0; i < n; i++) {
                sol[0][i] = a[i];
            }
            for (int i = 1; i < n; i++) {
                for (int j = 0; j < n - i; j++) {
                    sol[i][j] = sol[i - 1][j] ^ sol[i - 1][j + 1];
                }
            }
            for (int i = 1; i < n; i++) {
                for (int j = 0; j < n - i; j++) {
                    sol[i][j] = Math.max(sol[i][j], Math.max(sol[i - 1][j], sol[i - 1][j + 1]));
                }
            }
            int q = in.readInt();
            for (int i = 0; i < q; i++) {
                int l = in.readInt() - 1;
                int r = in.readInt() - 1;
                out.println(sol[r - l][l]);
            }
        }

    }

    static class InputReader {
        private final InputStream stream;
        private final byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int read() {
            try {
                if (curChar >= numChars) {
                    curChar = 0;
                    numChars = stream.read(buf);
                    if (numChars <= 0)
                        return -1;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return buf[curChar++];
        }

        public int readInt() {
            return (int) readLong();
        }

        public long readLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
                if (c == -1) throw new RuntimeException();
            }
            boolean negative = false;
            if (c == '-') {
                negative = true;
                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 negative ? (-res) : (res);
        }

        public int[] readIntArray(int size) {
            int[] arr = new int[size];
            for (int i = 0; i < size; i++) arr[i] = readInt();
            return arr;
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


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

/**
 * @author Don Li
 */
public class EhabAndAComponentChoosingProblem {

    long INF = (long) 1e18;

    int n;
    int[] a;

    int[][] G;

    void solve() {
        n = in.nextInt();
        a = new int[n];
        for (int i = 0; i < n; i++) a[i] = in.nextInt();
        int[] fr = new int[n - 1], to = new int[n - 1];
        for (int i = 0; i < n - 1; i++) {
            fr[i] = in.nextInt() - 1;
            to[i] = in.nextInt() - 1;
        }
        G = build_graph(n, fr, to);

        int[][] ret = bfs(G, 0);
        int[] par = ret[0], ord = ret[2];

        long best = -INF;
        long[] dp = new long[n];
        for (int i = n - 1; i >= 0; i--) {
            int u = ord[i];
            dp[u] = a[u];
            for (int v : G[u]) {
                if (v != par[u]) {
                    if (dp[v] > 0) dp[u] += dp[v];
                }
            }
            best = Math.max(best, dp[u]);
        }

        int k = 0;
        for (int i = n - 1; i >= 0; i--) {
            int u = ord[i];
            dp[u] = a[u];
            for (int v : G[u]) {
                if (v != par[u]) {
                    if (dp[v] > 0) dp[u] += dp[v];
                }
            }
            if (dp[u] == best) {
                dp[u] = -INF;
                k++;
            }
        }

        out.printf("%d %d%n", best * k, k);
    }

    int[][] bfs(int[][] G, int root) {
        int n = G.length;

        int[] par = new int[n];
        Arrays.fill(par, -1);

        int[] dep = new int[n];
        dep[root] = 0;

        int[] qu = new int[n];
        qu[0] = root;
        for (int l = 0, r = 1; l < r; l++) {
            int u = qu[l];
            for (int v : G[u]) {
                if (v != par[u]) {
                    qu[r++] = v;
                    par[v] = u;
                    dep[v] = dep[u] + 1;
                }
            }
        }

        return new int[][]{par, dep, qu};
    }

    int[][] build_graph(int n, int[] from, int[] to) {
        int[][] G = new int[n][];
        int[] cnt = new int[n];
        for (int i = 0; i < from.length; i++) {
            cnt[from[i]]++;
            cnt[to[i]]++;
        }
        for (int i = 0; i < n; i++) G[i] = new int[cnt[i]];
        for (int i = 0; i < from.length; i++) {
            G[from[i]][--cnt[from[i]]] = to[i];
            G[to[i]][--cnt[to[i]]] = from[i];
        }
        return G;
    }

    public static void main(String[] args) {
        in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
        out = new PrintWriter(System.out);
        new EhabAndAComponentChoosingProblem().solve();
        out.close();
    }

    static FastScanner in;
    static PrintWriter out;

    static class FastScanner {
        BufferedReader in;
        StringTokenizer st;

        public FastScanner(BufferedReader in) {
            this.in = in;
        }

        public String nextToken() {
            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(in.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

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

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

1088_E. Ehab and a component choosing problem


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class BOOL {
    static char [][]ch;
    static int n,m;
private static FastReader in =new FastReader();
    public static void main(String[] args) {
    int n=in.nextInt();
    int a[]=new int[1000002];
    int dp[]=new int[1000002],ans=0;
    for(int i=0;i<n;i++){a[in.nextInt()]=in.nextInt();}
    dp[0]=a[0]==0?0:1;
    for(int i=1;i<1000002;i++){
    if(a[i]==0){dp[i]=dp[i-1];}
    else{
    if(a[i]>=i){dp[i]=1;}
    else{
    dp[i]=dp[i-a[i]-1]+1;
    }}
    if(dp[i]>=ans)ans=dp[i];
    }
        System.out.println(n-ans);
    }}
class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                     InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

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

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
}


import java.util.*;

import java.lang.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;

public class Main {

    public static void main(String[] args) throws Exception{
        FastReader sc=new FastReader();
        OutputStream outputStream = System.out;
        PrintWriter out = new PrintWriter(outputStream);
        int n=sc.nextInt();
        int[] font=new int[n];
        int[] cost=new int[n];
        for(int i=0;i<n;i++) {
            font[i]=sc.nextInt();

        }
        for(int i=0;i<n;i++) {
            cost[i]=sc.nextInt();
        }
        int[] dou= new int[n];
        for(int i=0;i<n;i++) {
            int min=Integer.MAX_VALUE;
            for(int j=0;j<i;j++) {
                if(font[j]<font[i]) {
                    if(min>cost[i]+cost[j]) {
                        min=cost[i]+cost[j];
                    }
                }
            }
            dou[i]=min;
        }
        int ans=Integer.MAX_VALUE;
        for(int i=0;i<n;i++) {
            int min=Integer.MAX_VALUE;
            for(int j=0;j<i;j++) {
                if(dou[j]!=Integer.MAX_VALUE && font[j]<font[i]) {
                    if(min>dou[j]+cost[i]) {
                        min=dou[j]+cost[i];
                    }
                }
            }
            if(min<ans) {
                ans=min;
            }
        }
        if(ans==Integer.MAX_VALUE) {
            System.out.println(-1);
        }
        else {
            System.out.println(ans);
        }
    }
}

class FastReader
{
    BufferedReader br;
    StringTokenizer st;

    public FastReader()
    {
        br = new BufferedReader(new
                 InputStreamReader(System.in));
    }

    String next()
    {
        while (st == null || !st.hasMoreElements())
        {
            try
            {
                st = new StringTokenizer(br.readLine());
            }
            catch (IOException  e)
            {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

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

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

    double nextDouble()
    {
        return Double.parseDouble(next());
    }

    String nextLine()
    {
        String str = "";
        try
        {
            str = br.readLine();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return str;
    }
}


/* package codechef; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner in=new Scanner(System.in);
		long x=in.nextLong();
		long k=in.nextLong();

		long mod=1000000007;
		long get=power(2,k,mod);
		long ans=((get%mod)*((2*x)%mod))%mod-get+1;
		if(ans<0)
		ans+=mod;
		if(x==0)
		ans=0;
		System.out.println(ans);

	}
	static long power(long x, long y, long p)
    {
        // Initialize result
        long res = 1;

        // Update x if it is more
        // than or equal to p
        x = x % p;

        while (y > 0)
        {
            // If y is odd, multiply x
            // with result
            if((y & 1)==1)
                res = (res * x) % p;

            // y must be even now
            // y = y / 2
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }
}


 import java.util.Scanner;
    public class New_Year_and_Curling {
        static final double E = 0.00001;

        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);

            int n = sc.nextInt();
            int r = sc.nextInt();
            double[] y = new double[n];
            int arr[] = new int[n];

            for (int i = 0; i < n; i++) {
                arr[i] =sc.nextInt();
                double top = r; // if we make it 0 and subtract from the result will get WA (do not know why!!!)
                int x = arr[i];
                for(int  j =0 ;j<i;j++)
                {
                    if(Math.abs(arr[j] -x )<=2*r) {

                            top = Math.max(top  , y[j] + Math.sqrt((4 * r * r) - ((arr[j] - x) * (arr[j] - x))));

                    }
                }
                y[i] = top ;
                double res = y[i] ;
                System.out.print(res+" ");
            }

        }

    }


import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.StringTokenizer;

public class ChainReaction implements Closeable {

  private InputReader in = new InputReader(System.in);
  private PrintWriter out = new PrintWriter(System.out);

  private class Beacon implements Comparable<Beacon> {
    private int position, range, score;

    private Beacon(int position, int range) {
      this.position = position;
      this.range = range;
    }

    public void setScore(int score) {
      this.score = score;
    }

    @Override
    public int compareTo(Beacon o) {
      return Integer.compare(this.position, o.position);
    }
  }

  public void solve() {
    int n = in.ni();
    if (n == 1) {
      out.println(0);
      return;
    }
    beacons = new ArrayList<>();
    for (int i = 0; i < n; i++) {
      beacons.add(new Beacon(in.ni(), in.ni()));
    }
    beacons.sort(Comparator.naturalOrder());
    for (int i = 1; i < n; i++) {
      int left = 0, right = i - 1, position = beacons.get(i).position, range = beacons.get(i).range;
      int leftmost = i;
      while (left <= right) {
        int mid = left + (right - left) / 2;
        if (position - range <= beacons.get(mid).position) {
          leftmost = Math.min(leftmost, mid);
          right = mid - 1;
        } else {
          left = mid + 1;
        }
      }
      beacons.get(i).setScore(i - leftmost);
    }
    dp = new Integer[n];
    int ans = Integer.MAX_VALUE;
    for (int i = n - 1; i >= 0; i--) {
      ans = Math.min(n - 1 - i + recurse(i), ans);
    }
    out.println(ans);
  }

  private List<Beacon> beacons;
  private Integer[] dp;

  private int recurse(int idx) {
    if (idx <= 0) return 0;

    if (dp[idx] != null) return dp[idx];

    int destroyed = beacons.get(idx).score;
    int ans = destroyed + recurse(idx - destroyed - 1);
    return dp[idx] = ans;
  }

  @Override
  public void close() throws IOException {
    in.close();
    out.close();
  }

  static class InputReader {
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream) {
      reader = new BufferedReader(new InputStreamReader(stream), 32768);
      tokenizer = null;
    }

    public String next() {
      while (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public int ni() {
      return Integer.parseInt(next());
    }

    public long nl() {
      return Long.parseLong(next());
    }

    public void close() throws IOException {
      reader.close();
    }
  }

  public static void main(String[] args) throws IOException {
    try (ChainReaction instance = new ChainReaction()) {
      instance.solve();
    }
  }
}


import java.util.Scanner;
import java.util.Vector;

public class Main {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int a[] = new int[n];
        int b[] = new int[n];
        for (int i = 0; i < n; i++) a[i] = sc.nextInt();
        for (int i = 0; i < n; i++) b[i] = sc.nextInt();
        int c[] = new int[2 * n];
        c[0] = a[0];
        for (int i = 1; i < n; i++) {
            c[i * 2] = a[i];
            c[i * 2 - 1] = b[i];
            if (a[i] == 1 || b[i] == 1) {
                System.out.print(-1);
                System.exit(0);
            }
        }
        c[2 * n - 1] = b[0];
        if (a[0] == 1 || b[0] == 1) {
            System.out.print(-1);
            System.exit(0);
        }
        System.out.println(bin_search(c, m));
    }

    private static double bin_search(int[] c, int m) {
        double start = 0;
        double end = Integer.MAX_VALUE;
        double mid;
        while (start + 0.0000001 < end) {
            mid = (start + end) / 2;
            if (test(mid, m, c)) end = mid;
            else start = mid;
        }
        return end;
    }

    private static boolean test(double fuel, int m, int[] c) {
        for (int i = 0; i < c.length; i++) {
            fuel -= (m + fuel) / c[i];
            if (fuel < 0) {
                return false;
            }
        }
        return true;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main (String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        try {
            String parameterStringList[] = reader.readLine().split(" ");
            int x = Integer.parseInt(parameterStringList[0]);
            int y = Integer.parseInt(parameterStringList[1]);
            int z = Integer.parseInt(parameterStringList[2]);
            int t1 = Integer.parseInt(parameterStringList[3]);
            int t2 = Integer.parseInt(parameterStringList[4]);
            int t3 = Integer.parseInt(parameterStringList[5]);

            int T1 = Math.abs(x-y) * t1;
            int T2 = Math.abs(x-z) * t2 + 3*t3 + Math.abs(x-y) * t2;

            if(T2 <= T1) System.out.println("YES");
            else System.out.println("NO");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


import java.util.Scanner;

public class GenerateLogin {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String a = scan.next();
		String b = scan.next();
		char last = b.charAt(0);
		String ans = ""+a.charAt(0);
		for(int i = 1;i<a.length();i++){
			if(a.charAt(i)>=last)break;
			ans+=a.charAt(i);
		}
		ans+=last;
		System.out.println(ans);
	}

}


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

/**
 * CodeForces Round 5D. Follow Traffic Rules
 * Created by Darren on 14-9-14.
 */
public class Main {
    Reader in = new Reader(System.in);
    PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        new Main().run();
    }

    int a, v;
    int l, d, w;

    void run() throws IOException {
        a = in.nextInt();
        v = in.nextInt();
        l = in.nextInt();
        d = in.nextInt();
        w = in.nextInt();

        double totalTime = 0.0;
        if (v >= w) {
            if (w*w >= 2*a*d) {
                double x = Math.sqrt(2*a*d);
                totalTime = x / a;
                if ((v*v-x*x) >= 2*a*(l-d))
                    totalTime += (-2*x+Math.sqrt(4*x*x+8*a*(l-d))) / (2*a);
                else
                    totalTime += (v-x)/(1.0*a) + (l-d-(v*v-x*x)/(2.0*a))/v;
            } else {
//                totalTime = (-2*w+Math.sqrt(4*w*w+8*a*(l-d))) / (2*a);
                if (2*v*v-w*w <= 2*a*d) {
                    totalTime = v / (1.0*a) + (v-w) / (1.0*a) + (d-(2*v*v-w*w)/(2.0*a)) / v;
                } else {
                    double x = Math.sqrt((2*a*d+w*w)/2.0);
                    totalTime = x / a + (x-w) / a;
                }
                if ((v*v-w*w) >= 2*a*(l-d))
                    totalTime += (-2*w+Math.sqrt(4*w*w+8*a*(l-d))) / (2*a);
                else
                    totalTime += (v-w)/(1.0*a) + (l-d-(v*v-w*w)/(2.0*a))/v;
            }
        } else {
            if (v*v >= 2*a*l)
                totalTime = Math.sqrt(l*2.0/a);
            else
                totalTime = v / (1.0*a) + (l-v*v/(2.0*a)) / v;
        }

        out.printf("%.10f", totalTime);
        out.flush();
    }

    void solve() throws IOException {

    }

    static class Reader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public Reader(InputStream input) {
            reader = new BufferedReader(new InputStreamReader(input));
            tokenizer = new StringTokenizer("");
        }

        /** get next word */
        String nextToken() throws IOException {
            while ( ! tokenizer.hasMoreTokens() ) {
                //TODO add check for eof if necessary
                tokenizer = new StringTokenizer( reader.readLine() );
            }
            return tokenizer.nextToken();
        }

        String readLine() throws IOException {
            return reader.readLine();
        }

        char nextChar() throws IOException {
            return (char)reader.read();
        }

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

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

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


/*
 * Created on 17.05.2019
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Wolfgang Weck
 */
public class C01Easy {
	public static void main(String[] args) {
		try (BufferedReader r = new BufferedReader(new InputStreamReader(System.in))) {
			final String[] line = r.readLine().split(" ");
			final int N = Integer.parseInt(line[0]), P = Integer.parseInt(line[1]);
			final String[] numS = r.readLine().split(" ");
			if (numS.length != N) throw new IllegalArgumentException();
			final int[] n = new int[N];
			int sum1 = 0, sum2 = 0;
			for (int i = 0; i < N; i++) {
				n[i] = Integer.parseInt(numS[i]) % P;
				sum2 += n[i];
				if (sum2 >= P) sum2 -= P;
			}
			int max = sum2;
			for (int i = 0; i < N; i++) {
				sum1 += n[i];
				if (sum1 >= P) sum1 -= P;
				sum2 -= n[i];
				if (sum2 < 0) sum2 += P;
				final int s = sum1 + sum2;
				if (s > max) max = s;
			}
			System.out.println(max);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}


import java.util.*;
public class A
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        long n=sc.nextLong();
        if(n==0)
        System.out.println(0);
        else if(n%2==1)
        System.out.println((n+1)/2);
        else
        System.out.println(n+1);
    }
}


import java.util.*;
import java.io.*;
import static java.lang.System.in;

public class Main {
    public static void main(String[] args)throws IOException{
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] point = new int[n][];
        for(int i=0;i<n;i++) point[i] = new int[]{sc.nextInt(),sc.nextInt()};
        Arrays.sort(point,(a,b)->((a[0]-a[1])-(b[0]-b[1])));
        TreeMap<Integer,Integer> tm = new TreeMap<>();
        int ans = 0;
        for(int i=n-1;i>=0;i--){
            int x = point[i][0], w = point[i][1];
            Map.Entry<Integer,Integer> cur =  tm.ceilingEntry(x+w);
            int curRes;
            if(cur==null) curRes = 1;
            else curRes = cur.getValue()+1;
            ans = Math.max(ans,curRes);
            Map.Entry<Integer,Integer> upper = tm.ceilingEntry(x-w);
            if(upper==null||upper.getValue()<curRes) tm.put(x-w,curRes);
            //Integer key = tm.
        }
        System.out.println(ans);
    }
}


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

public class bhaa {

    InputStream is;
    PrintWriter o;
    /////////////////// CODED++ BY++ ++ ++ ++ BHAVYA++ ARORA++ ++ ++ ++ FROM++ JAYPEE++ INSTITUTE++ OF++ INFORMATION++ TECHNOLOGY++ ////////////////

///////////////////////// Make it work, make it right, make it fast. Make it work, make it right, make it fast. Make it work, make it right, make it fast. Make it work, make it right, make it fast. /////////////////

    boolean chpr(int n)
    {
    	if(n==1)
    	{
    		return true;
    	}if(n==2)
    	{
    		return true;
    	}
    	if(n==3)
    	{
    		return true;
    	}
    	if(n%2==0)
    	{
    		return false;

    	}
    	if(n%3==0)
    	{
    		return false;
    	}

    	int w=2;
 		int i=5;
    	while(i*i<=n)
    	{
    		if(n%i==0)
    		{
    			return false;
    		}
    		i+=w;
    		w=6-w;
    	}
    	return true;

    }

    void solve() {

        int n=ni();
        int k=ni();
        int rr=2*n;
        int gr=5*n;
        int br=8*n;
        o.println((long)(Math.ceil(rr*1.0/k)+Math.ceil(gr*1.0/k)+Math.ceil(br*1.0/k)));

    }

    //---------- I/O Template ----------

    public static void main(String[] args) { new bhaa().run(); }
    void run() {
        is = System.in;
        o = new PrintWriter(System.out);
        solve();
        o.flush();
    }

    byte input[] = new byte[1024];
    int len = 0, ptr = 0;

    int readByte() {
        if(ptr >= len) { ptr = 0;
            try { len = is.read(input); }
            catch(IOException e) { throw new InputMismatchException(); }
            if(len <= 0) { return -1; }
        } return input[ptr++];
    }
    boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
    int skip() {
        int b = readByte();
        while(b != -1 && isSpaceChar(b)) { b = readByte(); }
        return b;
    }

    char nc() { return (char)skip(); }
    String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); }
        return sb.toString();
    }
    String nLine() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); }
        return sb.toString();
    }
    int ni() {
        int n = 0, b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        if(b == -1) { return -1; }  //no input
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }
    long nl() {
        long n = 0L;    int b = readByte();
        boolean minus = false;
        while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
        if(b == '-') { minus = true; b = readByte(); }
        while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
        return minus ? -n : n;
    }

    double nd() { return Double.parseDouble(ns()); }
    float nf() { return Float.parseFloat(ns()); }
    int[] nia(int n) {
        int a[] = new int[n];
        for(int i = 0; i < n; i++) { a[i] = ni(); }
        return a;
    }
    long[] nla(int n) {
        long a[] = new long[n];
        for(int i = 0; i < n; i++) { a[i] = nl(); }
        return a;
    }
    int [][] nim(int n)
    {
        int mat[][]=new int[n][n];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                mat[i][j]=ni();
            }
        }
        return mat;
    }
    long [][] nlm(int n)
    {
        long mat[][]=new long[n][n];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                mat[i][j]=nl();
            }
        }
        return mat;
    }

    char[] ns(int n) {
        char c[] = new char[n];
        int i, b = skip();
        for(i = 0; i < n; i++) {
            if(isSpaceChar(b)) { break; }
            c[i] = (char)b; b = readByte();
        } return i == n ? c : Arrays.copyOf(c,i);
    }
    void piarr(int arr[])
    {
        for(int i=0;i<arr.length;i++)
        {
            o.print(arr[i]+" ");

        }
        o.println();
    }
    void plarr(long arr[])
    {
        for(int i=0;i<arr.length;i++)
        {
            o.print(arr[i]+" ");

        }
        o.println();
    }

    void pimat(int mat[][])
    {
        for(int i=0;i<mat.length;i++)
        {
            for(int j=0;j<mat[0].length;j++)
            {
                o.print(mat[i][j]);
            }
            o.println();
        }
    }
    void plmat(long mat[][])
    {
        for(int i=0;i<mat.length;i++)
        {
            for(int j=0;j<mat[0].length;j++)
            {
                o.print(mat[i][j]);
            }
            o.println();
        }

    }

    //////////////////////////////////// template finished //////////////////////////////////////

}


import java.util.*;

public class Solution {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		Comp c1 = getComp(scanner);
		Comp c2 = getComp(scanner);
		c1.sortByPrice();
		c2.sortByPrice();
		int i = 0;
		int j = 0;
		while(i < c1.num || j < c2.num) {
			Elem xi = (i < c1.num) ? c1.elems.get(i) : null;
			Elem yj = (j < c2.num) ? c2.elems.get(j) : null;
			if(xi != null && yj != null) {
				if(xi.price >= yj.price) {
					if(!c2.resultSet.contains(xi)) {
						c1.resultSet.add(xi);
					}
					i++;
				} else {
					if(!c1.resultSet.contains(yj)) {
						c2.resultSet.add(yj);
					}
					j++;
				}
			} else
			if(xi != null) {
				if(!c2.resultSet.contains(xi)) {
					c1.resultSet.add(xi);
				}
				i++;
			} else {
				if(!c1.resultSet.contains(yj)) {
					c2.resultSet.add(yj);
				}
				j++;
			}
		}

		long result = c1.getResultPrice() + c2.getResultPrice();
		System.out.println(result);
   }

   private static Comp getComp(Scanner scanner) {
	   Comp c = new Comp();
	   c.num = scanner.nextInt();
	   for(int i = 0; i < c.num; i++) {
		   c.addElem(scanner.nextLong(), scanner.nextLong());
	   }
	   return c;
   }
}

class Comp {
	int num;
	List<Elem> elems = new ArrayList<>();
	Set<Elem> resultSet = new HashSet<>();

	void addElem(long el, long pr) {
		Elem elem = new Elem(el, pr);
		elems.add(elem);
	}

	void sortByPrice() {
		Collections.sort(elems);
	}

	long getResultPrice() {
		long sumPrice = 0;
		for(Elem elem : resultSet) {
			sumPrice += elem.price;
		}

		return sumPrice;
	}
}

class Elem implements Comparable<Elem> {
	long elem;
	long price;

	public Elem(long el, long pr) {
		this.elem = el;
		this.price = pr;
	}

	public int compareTo(Elem other) {
		return (int) (other.price - price);
	}

	public boolean equals(Object o) {
		if(!(o instanceof Elem)) {
			return false;
		}

		Elem other = (Elem) o;
		return (other.elem == elem);
	}

	public int hashCode() {
		return (int) elem;
	}

	public String toString() {
		return "(" + elem + ", " + price + ")";
	}
}


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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new PrintStream(System.out));
        StringTokenizer st = new StringTokenizer(f.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        long[] arrB = new long[n];
        long[] arrG = new long[m];
        st=new StringTokenizer(f.readLine());
        for(int i=0;i<n;i++){
            arrB[i]=Long.parseLong(st.nextToken());
        }
        st=new StringTokenizer(f.readLine());
        for(int j=0;j<m;j++){
            arrG[j]=Long.parseLong(st.nextToken());
        }
        Arrays.sort(arrB);
        Arrays.sort(arrG);
        long ans = 0;
//        for (int i = 0; i < n; i++) ans += arrB[i] * m;
//        for (int i = 0; i < m - 1; i++) ans += arrG[i] - arrB[0];
//        if (arrB[m - 1] != arrB[0]) {
//            if (arrB.length == 1) {
//                ans=-1;
//            }
//            else ans += arrG[m - 1] - arrB[1];
//        }
//        if (arrG[m-1] < arrB[0]) {
//            ans=-1;
//        }
        for(int i=0;i<n;i++){
            ans+=arrB[i]*(long)m;
        }
        for(int i=1;i<m;i++){
            ans+=arrG[i]-arrB[n-1];
        }
        if(arrB[n-1]!=arrG[0]){
            if(n==1){
                ans=-1;
            }
            else{
                //smallest g goes to second to last
                ans+=arrG[0]-arrB[n-2];
            }
        }
        if(arrB[n-1]>arrG[0]){
            ans=-1;
        }
        System.out.println(ans);
        f.close();
        out.close();
    }
}


import java.util.*;
public class mad{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int cura = 0,curb = 0;
        int ver;
        System.out.println("? 0 0");
        System.out.flush();
        ver = sc.nextInt();
        for(int i=29;i>=0;i--){
            System.out.println("? "+(cura+(1<<i))+" "+curb);
            System.out.flush();
            int temp1 = sc.nextInt();
            System.out.println("? "+cura+" "+(curb+(1<<i)));
            System.out.flush();
            int temp2 = sc.nextInt();
            if(temp1!=temp2){
                if(temp2==1){
                    cura += (1<<i);
                    curb += (1<<i);
                }
            }
            else{
                if(ver==1) cura += (1<<i);
                if(ver==-1) curb += (1<<i);

                ver = temp1;
            }
        }
        System.out.println("! "+cura+" "+curb);
    }

}

1088_D. Ehab and another another xor problem


/* package whatever; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
			long n,s,p;
			Scanner in=new Scanner(System.in);
			n=in.nextLong();
			s=in.nextLong();
			if(n==1 && s<=1)
			{
			    System.out.print(n-1);
			}
	        else if(s<n)
	        {
	            if(s%2!=0)
		        {System.out.print(s/2);}
		        else
		        {System.out.print(s/2-1);}
	        }
	        else if(s==n)
	{
	    if(s%2==0)
		{System.out.println((n/2)-1);}
		else
		{System.out.println(n/2);}
	}
	else if(s<=(2*n-1))
	{
		System.out.print((2*n+1-s)/2);
	}
	else
	{
		System.out.print(0);
	}
	}
}


/*
 * Created on 17.05.2019
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author Wolfgang Weck
 */
public class A01Easy {
	private static interface Matrix {
		boolean get(int i, int j);

		int size();
	}

	private static class MData implements Matrix {
		private final boolean[][] m;

		MData(boolean[][] m) {
			this.m = m;
		}

		@Override
		public boolean get(int i, int j) {
			return m[i][j];
		}

		@Override
		public int size() {
			return m.length;
		}
	}

	private static abstract class MDecorator implements Matrix {
		protected final Matrix inner;

		MDecorator(Matrix inner) {
			this.inner = inner;
		}

		@Override
		public int size() {
			return inner.size();
		}
	}

	private static class MHFlip extends MDecorator {
		MHFlip(Matrix inner) {
			super(inner);
		}

		@Override
		public boolean get(int i, int j) {
			return inner.get(size() - 1 - i, j);
		}
	}

	private static class MVFlip extends MDecorator {
		MVFlip(Matrix inner) {
			super(inner);
		}

		@Override
		public boolean get(int i, int j) {
			return inner.get(i, size() - 1 - j);
		}
	}

	private static class MRot extends MDecorator {
		MRot(Matrix inner) {
			super(inner);
		}

		@Override
		public boolean get(int i, int j) {
			return inner.get(j, size() - 1 - i);
		}
	}

	public static void main(String[] args) {
		try (BufferedReader r = new BufferedReader(new InputStreamReader(System.in))) {
			final int N = Integer.parseInt(r.readLine());
			Matrix m1 = readMatrix(r, N), m2 = readMatrix(r, N);
			boolean matched = matchesFlipped(m1, m2);
			int i = 0;
			while (i < 3 && !matched) {
				m1 = new MRot(m1);
				matched = matchesFlipped(m1, m2);
				i++;
			}
			System.out.println(matched ? "Yes" : "No");
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static Matrix readMatrix(BufferedReader r, int n) throws IOException {
		boolean[][] m = new boolean[n][n];
		for (int i = 0; i < n; i++) {
			String line = r.readLine();
			for (int j = 0; j < n; j++) {
				m[i][j] = line.charAt(j) == 'X';
			}
		}
		return new MData(m);
	}

	private static boolean matches(Matrix m1, Matrix m2) {
		int i = 0, j = 0, n = m1.size();
		while (i < n && m1.get(i, j) == m2.get(i, j)) {
			j++;
			if (j == n) {
				j = 0;
				i++;
			}
		}
		return i == n;
	}

	private static boolean matchesFlipped(Matrix m1, Matrix m2) {
		return matches(m1, m2) || matches(new MHFlip(m1), m2) || matches(new MVFlip(m1), m2);
	}
}


//Author: Patel Rag
//Java version "1.8.0_211"
import java.util.*;
import java.io.*;

public class Main
{
  static class FastReader
  {
    BufferedReader br;
    StringTokenizer st;

    public FastReader(){ br = new BufferedReader(new InputStreamReader(System.in)); }

    String next()
    {
      while (st == null || !st.hasMoreElements())
      {
        try
        {
          st = new StringTokenizer(br.readLine());
        }
        catch (IOException  e)
        {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

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

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

    double nextDouble(){ return Double.parseDouble(next()); }

    float nextFloat() { return Float.parseFloat(next()); }

    boolean nextBoolean() { return Boolean.parseBoolean(next()); }

    String nextLine()
    {
      String str = "";
      try
      {
        str = br.readLine();
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
      return str;
    }
  }
  static long modExp(long x, long n, long mod)    //binary Modular exponentiation
  {
    long result = 1;
    while(n > 0)
    {
        if(n % 2 == 1)
            result = (result%mod * x%mod)%mod;
        x = (x%mod * x%mod)%mod;
        n=n/2;
    }
    return result;
  }
  static long gcd(long a, long b)
  {
    if(a==0) return b;
    return gcd(b%a,a);
  }
  public static void main(String[] args)
  throws IOException
  {
    FastReader fr = new FastReader();
    int n = fr.nextInt();
    int q = fr.nextInt();
    long[] a = new long[n];
    long[] k = new long[q];
    for(int i = 0; i < n; i++) a[i] = fr.nextLong();
    for(int i = 0; i < q; i++) k[i] = fr.nextLong();
    long[] pre = new long[n];
    pre[0] = a[0];
    for(int i = 1; i < n; i++) pre[i] = pre[i-1] + a[i];
    long pd = 0;
    for(int i = 0; i < q; i++)
    {
      int l = 0;
      int r = n - 1;
      while(r > l)
      {
        int mid = (l + r) >> 1;

        if(pre[mid] - pd < k[i])
        {
          l = mid + 1;
        }
        else if(pre[mid] - pd > k[i])
        {
          r = mid - 1;
        }
        else
        {
          l = r = mid;
        }
      }
      int ans = 0;
      if(pre[l] - pd <= k[i])
      {
        ans = n - l - 1;
      }
      else
      {
        ans = n - l;
      }
      if(ans == 0) ans  = n;
      pd = pd + k[i];
      if(pd >= pre[n-1]) pd = 0;
      System.out.println(ans);
    }
  }
}
class pair
{
  public int first;
  public int second;
  public pair(int first,int second)
  {
    this.first = first;
    this.second = second;
  }
  public pair(pair p)
  {
    this.first = p.first;
    this.second = p.second;
  }
  public int first() { return first; }
  public int second() { return second; }
  public void setFirst(int first) { this.first = first; }
  public void setSecond(int second) { this.second = second; }
}
class myComp implements Comparator<pair>
{
  public int compare(pair a,pair b)
  {
    if(a.first != b.first) return (a.first - b.first);
    return (b.second - a.second);
  }
}
class BIT       //Binary Indexed Tree aka Fenwick Tree
{
  public long[] m_array;

  public BIT(long[] dat)
  {
    m_array = new long[dat.length + 1];
    Arrays.fill(m_array,0);
    for(int i = 0; i < dat.length; i++)
    {
      m_array[i + 1] = dat[i];
    }
    for(int i = 1; i < m_array.length; i++)
    {
      int j = i + (i & -i);
      if(j < m_array.length)
      {
        m_array[j] = m_array[j] + m_array[i];
      }
    }
  }

  public final long prefix_query(int i)
  {
    long result = 0;
    for(++i; i > 0; i = i - (i & -i))
    {
      result = result + m_array[i];
    }
    return result;
  }

  public final long range_query(int fro, int to)
  {
    if(fro == 0)
    {
      return prefix_query(to);
    }
    else
    {
      return (prefix_query(to) - prefix_query(fro - 1));
    }
  }

  public void update(int i, long add)
  {
    for(++i; i < m_array.length; i = i + (i & -i))
    {
      m_array[i] = m_array[i] + add;
    }
  }
}


import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.lang.reflect.Array;
import java.math.*;
import java.security.SecureRandom;

public class WCS {

	public static class Vector implements Comparable <Vector>  {
		long x, y;
		int position;
		Vector first, second;
		boolean toReverse;

		public Vector(long xx, long yy, int p) {
			x = xx;
			y = yy;
			position = p;
			first = null;
			second = null;
			toReverse = false;
		}

		public Vector negate() {
			Vector vv = new Vector(-x, -y, position);
			vv.first = first;
			vv.second = second;
			vv.toReverse = !toReverse;
			return vv;
		}

		public Vector add(Vector v) {
			Vector sum = new Vector(this.x + v.x, this.y + v.y, position);
			sum.first = this;
			sum.second = v;
			return sum;
		}

		public Vector subtract(Vector v) {
			return this.add(v.negate());
		}

		public double euclideanNorm() {
			return Math.sqrt(x * x + y * y);
		}

		@Override
		public int compareTo(Vector v) {
			double thisa = Math.atan2(this.y, this.x);
			double va = Math.atan2(v.y, v.x);
			if(thisa < 0)
				thisa += 2 * Math.PI;
			if(va < 0)
				va += 2 * Math.PI;
			if(thisa < va)
				return -1;
			if(thisa > va)
				return 1;
			return Integer.compare(this.position, v.position);
		}

		@Override
		public String toString() {
			return x + " " + y;
		}
	}

	public static void dfs(Vector curr, int[] ans) {
		if(curr.first == null) {
			ans[curr.position] = curr.toReverse ? -1 : 1;
			return;
		}
		curr.first.toReverse ^= curr.toReverse;
		curr.second.toReverse ^= curr.toReverse;
		dfs(curr.first, ans);
		dfs(curr.second, ans);
	}

	public static boolean ok(Vector v1, Vector v2) {
		return v1.add(v2).euclideanNorm() <= Math.max(v1.euclideanNorm(), v2.euclideanNorm());
	}

	public static void stop(long k) {
		long time = System.currentTimeMillis();
		while(System.currentTimeMillis() - time < k);
	}

	public static void main(String[] args) throws IOException {
		int n = in.nextInt();
		TreeSet <Vector> vectors = new TreeSet <> ();
		for(int i = 0; i < n; i ++) {
			Vector v = new Vector(in.nextLong(), in.nextLong(), i);
			vectors.add(v);
		}
		while(vectors.size() > 2) {
			//System.out.println(vectors);
			//stop(500);

			TreeSet <Vector> support = new TreeSet <> ();

			while(vectors.size() > 0) {
				Vector curr = vectors.pollFirst();
				Vector next1 = vectors.higher(curr);
				Vector next2 = vectors.lower(curr.negate());
				Vector next3 = vectors.higher(curr.negate());
				Vector next4 = vectors.lower(curr);

				//System.out.println("CURR: " + curr + "\n" + next1 + "\n" + next2);

				if(next1 != null) {
					if(ok(curr, next1)) {
						support.add(curr.add(next1));
						vectors.remove(next1);
						continue;
					}
				}
				if(next1 != null) {
					if(ok(curr, next1.negate())) {
						support.add(curr.subtract(next1));
						vectors.remove(next1);
						continue;
					}
				}
				if(next2 != null) {
					if(ok(curr, next2)) {
						support.add(curr.add(next2));
						vectors.remove(next2);
						continue;
					}
				}
				if(next2 != null) {
					if(ok(curr, next2.negate())) {
						support.add(curr.subtract(next2));
						vectors.remove(next2);
						continue;
					}
				}
				if(next3 != null) {
					if(ok(curr, next3)) {
						support.add(curr.add(next3));
						vectors.remove(next3);
						continue;
					}
				}
				if(next3 != null) {
					if(ok(curr, next3.negate())) {
						support.add(curr.subtract(next3));
						vectors.remove(next3);
						continue;
					}
				}
				if(next4 != null) {
					if(ok(curr, next4)) {
						support.add(curr.add(next4));
						vectors.remove(next4);
						continue;
					}
				}
				if(next4 != null) {
					if(ok(curr, next4.negate())) {
						support.add(curr.subtract(next4));
						vectors.remove(next4);
						continue;
					}
				}

				support.add(curr);
			}

			vectors = support;
		}

		if(vectors.size() == 2) {
			Vector curr = vectors.pollFirst();
			Vector next = vectors.pollFirst();
			Vector add = curr.add(next);
			Vector sub = curr.subtract(next);
			if(sub.euclideanNorm() <= add.euclideanNorm())
				vectors.add(sub);
			else
				vectors.add(add);
		}

		//System.out.println(vectors.first().euclideanNorm());

		StringBuilder buffer = new StringBuilder();
		int[] ans = new int[n];
		dfs(vectors.pollFirst(), ans);
		for(int i = 0; i < n; i ++)
			buffer.append(ans[i] + " ");
		System.out.println(buffer);
	}

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        BigInteger nextBigInteger() {
        	return new BigInteger(in.next());
        }

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

        char nextChar() {
            return in.next().charAt(0);
        }

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

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }

    static FastReader in = new FastReader();
    static OutputStream out = new BufferedOutputStream(System.out);

    public static byte[] toByte(Object o) {
        return String.valueOf(o).getBytes();
    }

    public static void sop(Object o) {
        System.out.print(o);
    }
}


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

public class A4 {

    public BufferedReader input;
    public PrintWriter output;
    public StringTokenizer stoken = new StringTokenizer("");

    public static void main(String[] args) throws IOException {
        new A4();
    }

    A4() throws IOException {
        input = new BufferedReader(new InputStreamReader(System.in));
        output = new PrintWriter(System.out);
        run();
        input.close();
        output.close();
    }

    private void run() throws IOException {
        int n = Math.toIntExact(nextLong());
        int m = Math.toIntExact(nextLong());
        int[] coor = new int[n + 1];
        int[] ss = new int[n + 1];
        for (int i = 0; i < n; i++) {
            coor[i] = Math.toIntExact(nextLong());
        }
        coor[n] = 1000000000;
        Arrays.sort(coor);
        for (int i = 0; i < m; i++) {
            long x1 = nextLong();
            long x2 = nextLong();
            nextLong();
            if (x1 == 1 && x2 >= coor[0]) {
                int l = 0;
                int r = n + 1;
                while (r - l > 1) {
                    int mi = (r + l) / 2;
                    if (coor[mi] > x2) {
                        r = mi;
                    } else {
                        l = mi;
                    }
                }
                ss[l]++;
            }
        }
        long[] ans = new long[n + 1];
        ans[n] = ss[n] + n;
        long min = ans[n];
        for (int i = n - 1; i > -1; i--) {
            ans[i] = ans[i + 1] - 1 + ss[i];
            if (ans[i] < min) {
                min = ans[i];
            }
        }
        System.out.println(min);
    }

    private Long nextLong() throws NumberFormatException, IOException {
        return Long.parseLong(nextString());
    }

    private String nextString() throws IOException {
        while (!stoken.hasMoreTokens()) {
            String st = input.readLine();
            stoken = new StringTokenizer(st);
        }
        return stoken.nextToken();
    }
}


import java.io.*;
import java.util.*;
public class Codechef{
	static int max=Integer.MIN_VALUE;
	static int res=0;
	static int[] checkMax(int arr[],int j){
             int sum=0;
             int x=arr[j];
        		while(x!=0){
        		        if(j+1==15){
        		        	j=0;
        		        }else{
        		        	arr[j+1]=arr[j+1]+1;
        		        }
        		        // if(arr[j+1]%2==0){
        		        // 	sum=sum+arr[j+1];
        		        // 	if(sum>=max){
        		        // 	max=sum;
        		        // }
        		        // }

        		        x--;
        		        j++;
        		}
        		     return arr;

	}

    public static void main(String []args){
       Scanner sc = new Scanner (System.in);
    long a [] = new long [14];
    long b [] = new long [14];
    long p,q,r,s,max = 0;
    for(int i = 0; i < 14; i++) a[i] = sc.nextInt();
    for(int i = 0; i < 14; i++){
      p = a[i]%14;
      q = a[i]/14;
      r = 0;
      s = 0;
      for(int j = 0; j < 14; j++) b[j] = a[j];
      b[i] = 0;
      int j = (i+1)%14;
      for(; r < p; r++) {
        b[j]++;
        j=(j+1)%14;
      }
      for( j = 0; j < 14; j++) {
        b[j] += q;
        if(b[j] % 2 == 0) s+= b[j];
      }
      max = Math.max(max,s);
    }
    System.out.println(max);

    }

}


import javafx.util.Pair;

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

public class Beacon8 {
    public static void main(String[] args) throws IOException {
//        int[] arr = {1, 3, 7, 18};
//        int bIndex = Arrays.binarySearch(arr, 4);
//        System.out.println(bIndex);

        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        Map<Integer, Integer> beacons = new TreeMap<>();

        for (int i = 0; i < n; i++) {
            int index = scan.nextInt();
            int power = scan.nextInt();
            beacons.put(index, power);
        }
        int[] indicesArr = new int[n];
        int arrInd = 0;
        for (int index : beacons.keySet()) {
            indicesArr[arrInd] = index;
            arrInd++;
        }
       // Integer[] indicesArr = ((Integer[])beacons.keySet().toArray());
        int[] nDestroys = new int[n];
        for (int i = 0; i < n; i++) {
            int bIndex = Arrays.binarySearch(indicesArr, indicesArr[i] - beacons.get(indicesArr[i]));
            if (bIndex < 0)
                bIndex = -(bIndex + 1);
            nDestroys[i] = i - bIndex;
        }
        int[] totalBeacons = new int[n];
        int maxBeacons = 1;
        totalBeacons[0] = 1;
        for (int i = 1; i < n; i++) {
            if (nDestroys[i] == 0)
                totalBeacons[i] = totalBeacons[i - 1] + 1;
            else {
                if ((i - nDestroys[i] - 1) >= 0)
                    totalBeacons[i] = totalBeacons[i - nDestroys[i] - 1] + 1;
                else
                    totalBeacons[i] = 1;
            }
               // totalBeacons[i] = totalBeacons[i - nDestroys[i]] + 1;
            //totalBeacons[i] = i - nDestroys[i] + totalBeacons[i - nDestroys[i]] + 1;
            if(totalBeacons[i] > maxBeacons)
                maxBeacons = totalBeacons[i];
        }
//        System.out.println("\ntotalBeacons array");
//        for (int i = 0; i < n; i++) {
//            System.out.print(totalBeacons[i] + " ");
//        }
//        if (maxBeacons == -1)
//            System.out.println(n);
        System.out.println(n - maxBeacons);
    }

}


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

public class CF1082D {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		StringTokenizer st = new StringTokenizer(br.readLine());
		int[] aa = new int[n];
		int[] i1 = new int[n];
		int[] i2 = new int[n];
		int n1 = 0, n2 = 0, m2 = 0;
		for (int i = 0; i < n; i++) {
			int a = Integer.parseInt(st.nextToken());
			aa[i] = a;
			if (a == 1)
				i1[n1++] = i;
			else {
				i2[n2++] = i;
				m2 += a;
			}
		}
		if (m2 < (n2 - 1) * 2 + n1) {
			System.out.println("NO");
			return;
		}
		int m = n2 - 1 + n1;
		int d = n2 - 1 + Math.min(n1, 2);
		PrintWriter pw = new PrintWriter(System.out);
		pw.println("YES " + d);
		pw.println(m);
		for (int i = 0; i + 1 < n2; i++) {
			pw.println((i2[i] + 1) + " " + (i2[i + 1] + 1));
			aa[i2[i]]--; aa[i2[i + 1]]--;
		}
		if (n1 > 0) {
			while (n2 > 0 && aa[i2[n2 - 1]] == 0)
				n2--;
			pw.println((i2[n2 - 1] + 1) + " " + (i1[n1 - 1] + 1));
			aa[i2[n2 - 1]]--;
			n1--;
		}
		for (int i = 0, j = 0; j < n1; j++) {
			while (aa[i2[i]] == 0)
				i++;
			pw.println((i2[i] + 1) + " " + (i1[j] + 1));
			aa[i2[i]]--;
		}
		pw.close();
	}
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

public class Div1_526C {

	static int nV;

	static ArrayList<Integer>[] chldn;

	static int root;

	static int[][] anc;
	static int[] depth;

	static int[] num;

	static int[] nLoc;

	static int[][] tree;

	public static void main(String[] args) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		nV = Integer.parseInt(reader.readLine());

		chldn = new ArrayList[nV];
		for (int i = 0; i < nV; i++) {
			chldn[i] = new ArrayList<>();
		}

		anc = new int[nV][21];
		depth = new int[nV];

		num = new int[nV];
		nLoc = new int[nV];
		tree = new int[nV * 4][2];
		for (int[] a : tree) {
			a[0] = a[1] = -1;
		}

		root = 0;

		StringTokenizer inputData = new StringTokenizer(reader.readLine());
		for (int i = 0; i < nV; i++) {
			num[i] = Integer.parseInt(inputData.nextToken());
			nLoc[num[i]] = i;
		}

		inputData = new StringTokenizer(reader.readLine());
		for (int i = 1; i < nV; i++) {
			anc[i][0] = Integer.parseInt(inputData.nextToken()) - 1;
			chldn[anc[i][0]].add(i);
		}

		preprocess();

		build(1, 0, nV - 1);

		int nQ = Integer.parseInt(reader.readLine());

		while (nQ-- > 0) {
			inputData = new StringTokenizer(reader.readLine());
			if (inputData.nextToken().equals("1")) {
				int a = Integer.parseInt(inputData.nextToken()) - 1;
				int b = Integer.parseInt(inputData.nextToken()) - 1;

				int temp = num[a];
				num[a] = num[b];
				num[b] = temp;

				nLoc[num[a]] = a;
				nLoc[num[b]] = b;

				update(1, 0, nV - 1, num[a]);
				update(1, 0, nV - 1, num[b]);
			} else {
				printer.println(query(1, 0, nV - 1, nLoc[0], nLoc[0]) + 1);
			}
		}
		printer.close();
	}

	static void build(int nI, int cL, int cR) {
		if (cL == cR) {
			tree[nI][0] = nLoc[cL];
			tree[nI][1] = nLoc[cL];
		} else {
			int mid = (cL + cR) >> 1;
			build(nI * 2, cL, mid);
			build(nI * 2 + 1, mid + 1, cR);
			if (tree[nI * 2][0] != -1 && tree[nI * 2 + 1][0] != -1) {
				merge(tree[nI * 2][0], tree[nI * 2][1], tree[nI * 2 + 1][0], tree[nI * 2 + 1][1]);
				tree[nI][0] = mResp[0];
				tree[nI][1] = mResp[1];
			}
		}
	}

	static int query(int nI, int cL, int cR, int e1, int e2) {
		if (cL == cR) {
			merge(e1, e2, nLoc[cL], nLoc[cL]);
			if (mResp[0] != -1) {
				return cL;
			} else {
				return cL - 1;
			}
		}
		int mid = (cL + cR) >> 1;

		merge(tree[nI * 2][0], tree[nI * 2][1], e1, e2);
		if (mResp[0] != -1) {
			return query(nI * 2 + 1, mid + 1, cR, mResp[0], mResp[1]);
		}
		return query(nI * 2, cL, mid, e1, e2);
	}

	static void update(int nI, int cL, int cR, int uI) {
		if (cL == cR) {
			tree[nI][0] = nLoc[cL];
			tree[nI][1] = nLoc[cL];
		} else {
			int mid = (cL + cR) >> 1;
			if (uI <= mid) {
				update(nI * 2, cL, mid, uI);
			} else {
				update(nI * 2 + 1, mid + 1, cR, uI);
			}
			merge(tree[nI * 2][0], tree[nI * 2][1], tree[nI * 2 + 1][0], tree[nI * 2 + 1][1]);
			tree[nI][0] = mResp[0];
			tree[nI][1] = mResp[1];
		}
	}

	static int[] mResp = new int[2];

	static void merge1(int... a) {
		for (int i = 0; i < 3; i++) {
			if (a[i] == -1) {
				mResp[0] = mResp[1] = -1;
				return;
			}
		}

		if (onPath(a[0], a[1], a[2])) {
			mResp[0] = a[0];
			mResp[1] = a[1];
			return;
		}
		if (onPath(a[0], a[2], a[1])) {
			mResp[0] = a[0];
			mResp[1] = a[2];
			return;
		}
		if (onPath(a[1], a[2], a[0])) {
			mResp[0] = a[1];
			mResp[1] = a[2];
			return;
		}
		mResp[0] = mResp[1] = -1;
	}

	static void merge(int... a) {
		merge1(a[0], a[1], a[2]);
		merge1(mResp[0], mResp[1], a[3]);
	}

	static boolean onPath(int a, int b, int c) {
		if (a == c || b == c) {
			return true;
		}

		if (depth[a] > depth[c]) {
			a = jump(a, depth[a] - depth[c] - 1);
		}
		if (depth[b] > depth[c]) {
			b = jump(b, depth[b] - depth[c] - 1);
		}
		if (a == b) {
			return false;
		}

		if (anc[a][0] == c || anc[b][0] == c) {
			return true;
		}

		return false;
	}

	// good for depth of up to 1_048_576 = 2^20

	static void preprocess() {
		anc[root][0] = root;
		fParent(root);

		for (int k = 1; k <= 20; k++) {
			for (int i = 0; i < nV; i++) {
				anc[i][k] = anc[anc[i][k - 1]][k - 1];
			}
		}
	}

	static void fParent(int cV) {
		for (int aV : chldn[cV]) {
			anc[aV][0] = cV;
			depth[aV] = depth[cV] + 1;
			fParent(aV);
		}
	}

	static int fLCA(int a, int b) {
		if (depth[a] > depth[b]) {
			int temp = b;
			b = a;
			a = temp;
		}
		b = jump(b, depth[b] - depth[a]);
		if (a == b) {
			return a;
		}
		for (int i = 20; i >= 0; i--) {
			if (anc[a][i] != anc[b][i]) {
				a = anc[a][i];
				b = anc[b][i];
			}
		}
		return anc[a][0];
	}

	static int jump(int cV, int d) {
		for (int i = 0; i <= 20; i++) {
			if ((d & (1 << i)) != 0) {
				cV = anc[cV][i];
			}
		}
		return cV;
	}

	static Comparator<Integer> BY_DEPTH = new Comparator<Integer>() {
		public int compare(Integer o1, Integer o2) {
			return -Integer.compare(depth[o1], depth[o2]); // greatest depth first
		}
	};
}


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

public class round569d2b {

	public static void main(String args[]) {
		FastScanner in = new FastScanner(System.in);

		int n = in.nextInt();
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = in.nextInt();
		}
		if (n % 2 == 0) {
			for (int i = 0; i < n; i++) {
				if (arr[i] >= 0) {
					arr[i] = -1*arr[i]-1;
				}
			}
		}
		else {
			int max = Integer.MIN_VALUE;
			int maxIndex = 0;
			for (int i = 0; i < n; i++) {
				int elem = arr[i];
				if (elem < 0) {
					elem = -1*elem-1;
				}
				if (elem > max) {
					max = elem;
					maxIndex = i;
				}
			}
			for (int i = 0; i < n; i++) {
				if (i == maxIndex) {
					if (arr[i] < 0) {
						arr[i] = -1*arr[i]-1;
					}
				}
				else {
					if (arr[i] >= 0) {
						arr[i] = -1*arr[i]-1;
					}
				}
			}
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < n ;i++) {
			sb.append(arr[i] + " ");

		}
		System.out.println(sb);

	}

	// ======================================================================================
	// =============================== Reference Code =======================================
	// ======================================================================================

	static int greatestDivisor(int n) {
		int limit = (int) Math.sqrt(n);
		int max = 1;
		for (int i = 2; i <= limit; i++) {
			if (n % i == 0) {
				max = Integer.max(max, i);
				max = Integer.max(max, n / i);
			}
		}
		return max;
	}

	// Method to return all primes smaller than or equal to
	// n using Sieve of Eratosthenes
	static boolean[] sieveOfEratosthenes(int n) {
		// Create a boolean array "prime[0..n]" and initialize
		// all entries it as true. A value in prime[i] will
		// finally be false if i is Not a prime, else true.
		boolean prime[] = new boolean[n + 1];
		for (int i = 0; i <= n; i++)
			prime[i] = true;

		prime[0] = false;
		prime[1] = false;

		for (int p = 2; p * p <= n; p++) {
			// If prime[p] is not changed, then it is a prime
			if (prime[p] == true) {
				// Update all multiples of p
				for (int i = p * p; i <= n; i += p)
					prime[i] = false;
			}
		}

		return prime;
	}

	// Binary search for number greater than or equal to target
	// returns -1 if number not found
	private static int bin_gteq(int[] a, int key) {
		int low = 0;
		int high = a.length;
		int max_limit = high;
		while (low < high) {
			int mid = low + (high - low) / 2;
			if (a[mid] < key) {
				low = mid + 1;
			} else
				high = mid;
		}

		return high == max_limit ? -1 : high;
	}

	public static int gcd(int a, int b) {
		if (a == 0)
			return b;

		return gcd(b % a, a);
	}

	static class Tuple<X, Y> {
		public final X x;
		public final Y y;

		public Tuple(X x, Y y) {
			this.x = x;
			this.y = y;
		}

		public String toString() {
			return "(" + x + "," + y + ")";
		}
	}

	static class Tuple3<X, Y, Z> {
		public final X x;
		public final Y y;
		public final Z z;

		public Tuple3(X x, Y y, Z z) {
			this.x = x;
			this.y = y;
			this.z = z;
		}

		public String toString() {
			return "(" + x + "," + y + "," + z + ")";
		}
	}

	static Tuple3<Integer, Integer, Integer> gcdExtended(int a, int b, int x, int y) {
		// Base Case
		if (a == 0) {
			x = 0;
			y = 1;
			return new Tuple3(0, 1, b);
		}

		int x1 = 1, y1 = 1; // To store results of recursive call
		Tuple3<Integer, Integer, Integer> tuple = gcdExtended(b % a, a, x1, y1);
		int gcd = tuple.z;
		x1 = tuple.x;
		y1 = tuple.y;

		// Update x and y using results of recursive
		// call
		x = y1 - (b / a) * x1;
		y = x1;

		return new Tuple3(x, y, gcd);
	}

	// Returns modulo inverse of a
	// with respect to m using extended
	// Euclid Algorithm. Refer below post for details:
	// https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/
	static int inv(int a, int m) {
		int m0 = m, t, q;
		int x0 = 0, x1 = 1;

		if (m == 1)
			return 0;

		// Apply extended Euclid Algorithm
		while (a > 1) {
			// q is quotient
			q = a / m;

			t = m;

			// m is remainder now, process
			// same as euclid's algo
			m = a % m;
			a = t;

			t = x0;

			x0 = x1 - q * x0;

			x1 = t;
		}

		// Make x1 positive
		if (x1 < 0)
			x1 += m0;

		return x1;
	}

	// k is size of num[] and rem[].
	// Returns the smallest number
	// x such that:
	// x % num[0] = rem[0],
	// x % num[1] = rem[1],
	// ..................
	// x % num[k-2] = rem[k-1]
	// Assumption: Numbers in num[] are pairwise
	// coprime (gcd for every pair is 1)
	static int findMinX(int num[], int rem[], int k) {
		// Compute product of all numbers
		int prod = 1;
		for (int i = 0; i < k; i++)
			prod *= num[i];

		// Initialize result
		int result = 0;

		// Apply above formula
		for (int i = 0; i < k; i++) {
			int pp = prod / num[i];
			result += rem[i] * inv(pp, num[i]) * pp;
		}

		return result % prod;
	}

	/**
	 * Source: Matt Fontaine
	 */
	static class FastScanner {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int chars;

		public FastScanner(InputStream stream) {
			this.stream = stream;
		}

		int read() {
			if (chars == -1)
				throw new InputMismatchException();
			if (curChar >= chars) {
				curChar = 0;
				try {
					chars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (chars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

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

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

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public String next() {
			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 String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}
	}

}


/**
 *   Author: Ridam Nagar
 *   Date: 27 February 2019
 *   Time: 01:17:36
**/
/*
package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
  static String reverse(String s){
    String reverse="";
    for(int i=s.length()-1;i>=0;i--){
        reverse=reverse + s.charAt(i);
    }
    return reverse;
  }

    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
         int n=sc.nextInt();
         int m=sc.nextInt();
         int x=m%(int)Math.pow(2,n);
         System.out.println(x);

}
}