import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int ketasu = sc.nextInt();
        int m = sc.nextInt();
        HashMap<Integer, Integer> map = new HashMap<>();
        boolean judge = true;
        int maxKetasu = 0;
        for(int i = 0; i < m; i++){
          int a = sc.nextInt();
          int b = sc.nextInt();
          if(map.containsKey(a) && b != map.get(a)){
            judge = false;
            break;
          }else{
            map.put(a, b);
          }
          if(maxKetasu < a)
            maxKetasu = a;
        }
        if(judge == false)
          System.out.println(-1);
        else if(!map.containsKey(1))
          System.out.println(-1);
        else if(map.get(1) == 0)
          System.out.print(-1);
        else{
          for(int i = 1; i <= ketasu; i++){
            if(!map.containsKey(i))
              System.out.print(0);
            else
              System.out.print(map.get(i));
          }
        }
        System.out.println();
    }
}


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

public class Main {

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

    int[] s = new int[m];
    int[] c = new int[m];

    for (int i = 0; i < m; i++) {
      s[i] = sc.nextInt();
      c[i] = sc.nextInt();
    }

    TreeMap<Integer,Integer> mp = new TreeMap<Integer,Integer>();

    for (int i = 0; i < m; i++) {
      if (mp.containsKey(s[i])) {
        int k = mp.get(s[i]);
        if (k != c[i]) {
          System.out.println(-1);
          return;
        }
      }
      mp.put(s[i],c[i]);
    }

    String h = "";

    for (int i = 1; i <= n; i++) {
      Integer x = mp.get(i);
      if (x == null) {
        h += "0";
      } else {
        h += String.valueOf(x);
      }
    }

    if (h.charAt(n-1) == '0') {
      System.out.println(-1);
    } else {
      System.out.println(h);
    }

  }
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 300 points

Problem Statement

If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.

The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.)

The s_i-th digit from the left is c_i. \left(i = 1, 2, \cdots, M\right)

Constraints

All values in input are integers.

1 \leq N \leq 3

0 \leq M \leq 5

1 \leq s_i \leq N

0 \leq c_i \leq 9

Input

Input is given from Standard Input in the following format:

N M
s_1 c_1
\vdots
s_M c_M

Output

Print the answer.

Sample Input 1

3 3
1 7
3 2
1 7

Sample Output 1

702

702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them.

Sample Input 2

3 2
2 1
2 3

Sample Output 2

-1

Sample Input 3

3 1
1 0

Sample Output 3

-1

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
	    int digit = in.nextInt();
      	int inputs = in.nextInt();

      	if (inputs == 0) {
         	System.out.println(-1);
         	return;
        }

        int[] array = new int[digit];
		for (int i = 0; i < inputs; i++) {
          	int pos = in.nextInt();
          	int val = in.nextInt();
          	if (val < 0) {
              System.out.println(-1);
              return;
            }
          	array[pos - 1] = val;
        }

      	if (array.length > 1 && array[0] == 0) {
         	System.out.println(-1);
         	return;
        }

      	StringBuilder sb = new StringBuilder();
		for (int i = 0; i < array.length; i++) {
         	sb.append(array[i]);
        }

      	System.out.println(sb.toString());
    }

}


import java.util.Scanner;

public class Main {

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

		int n = Integer.parseInt(sc.next());
		int m = Integer.parseInt(sc.next());

		int[] s = new int[n];
		for(int i = 0 ; i < n ; i++) {
			s[i] = -1;

		}

		int ans = 0;
		for(int i = 0 ; i < m ; i++) {
			int temp1 = Integer.parseInt(sc.next()) -1;
			int temp2 = Integer.parseInt(sc.next());

			if(s[temp1] == -1) {
				s[temp1] = temp2;
			}else if(s[temp1] != temp2) {
				ans = -1;
				break;
			}

		}

		for(int i = 0 ; i < n ; i++) {
			if(s[i] == -1) {
				if(i == 0 || n == 1) {
					s[i] = 1;
				}else {
					s[i] = 0;
				}
			}
		}

		if(n != 1 && s[0] == 0) {
			ans = -1;
		}

		if(ans != -1) {
			String tmp = "";
			for(int i = 0 ; i < n ; i++ ) {
				tmp += "" + s[i];
			}

			ans = Integer.parseInt(tmp);
		}

		System.out.println(ans);
	}
}


import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class Main {
	int N, M;
	int[] s, c;
	int[] buf;
	boolean enable = true;

	public static void main(String[] args) throws IOException {
		InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
		BufferedReader in = new BufferedReader(reader);
		Main ins = new Main(in);
		ins.calc();
		ins.showResult();
	}

	Main(BufferedReader in) throws IOException {
		String[] tokens = in.readLine().split(" ");
		N = Integer.parseInt(tokens[0]);
		M = Integer.parseInt(tokens[1]);
		s = new int[M];
		c = new int[M];
		for (int i = 0; i < M; ++i) {
			tokens = in.readLine().split(" ");
			s[i] = Integer.parseInt(tokens[0]) - 1;
			c[i] = Integer.parseInt(tokens[1]);
		}
		this.buf = new int[N];
		Arrays.fill(buf, -1);
	}

	void calc() {
		for (int i = 0; i < M; ++i) {
			if (buf[s[i]] < 0 || buf[s[i]] == c[i]) {
				// 未初期化，埋められる
				buf[s[i]] = c[i];
			} else {
				// 既存と違う: NG
				enable = false;
				break;
			}
		}
		if (!enable) {
			return;
		}
		// 残りを埋める
		if (N == 1) {
			if (buf[0] < 0) {
				buf[0] = 0;
			}
			return;
		}
		if (buf[0] == 0) {
			if (N != 1) {
				enable = false;
			}
			return;
		}
		buf[0] = buf[0] < 0 ? 1 : buf[0];
		for (int i = 1; i < N; ++i) {
			if (buf[i] < 0) {
				buf[i] = 0;
			}
		}
	}

	void showBuf() {
		for (int i = 0; i < buf.length; ++i) {
			System.out.print(buf[i] + " ");
		}
		System.out.println();
	}

	void showResult() {
		// showBuf();
		if (enable) {
			StringBuilder strBuilder = new StringBuilder();
			for (int i = 0; i < N; ++i) {
				strBuilder.append(Integer.toString(buf[i]));
			}
			System.out.println(strBuilder.toString());
		} else {
			System.out.println(-1);
		}
	}

}


import java.util.*;

public class Main{
  static int p = 1000000007;
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String[] S = sc.nextLine().split(" ");
    int N = Integer.parseInt(S[0]);
    int M = Integer.parseInt(S[1]);
    int[] ans = new int[N];
    Arrays.fill(ans, -1);
    for(int i = 0; i < M; i++){
      S = sc.nextLine().split(" ");
      int a = Integer.parseInt(S[0])-1;
      int b = Integer.parseInt(S[1]);

      if((a+1 > N) || (ans[a] != -1 && ans[a] != b)){
        System.out.println(-1);
        return;
      }
      ans[a] = b;
    }

    boolean f = false;
    for(int n : ans){
      if(n == -1){
        n = 1;
      }
      if(N != 1 && n == 0 && !f){
        System.out.println(-1);
        return;
      }
      f = true;
      System.out.print(n);
    }
    System.out.println();
  }
}


import java.util.*;
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		if(M == 0) {
			System.out.print(1);
			for(int i = 1; i < N; i++)
				System.out.print(0);
			return;
		}
		int[] ans = new int[N];
		boolean[] used = new boolean[N];
		for(int i = 0; i < M; i++) {
			int s = sc.nextInt();
			int c = sc.nextInt();
			if(used[s-1] && ans[s-1] != c) {
				System.out.println(-1);
				return;
			}
			ans[s-1] = c;
			used[s-1] = true;
		}
		if(used[0] && ans[0] == 0) {
			if(N == 1)
				System.out.println(0);
			else
				System.out.println(-1);
			return;
		}
		else if(ans[0] == 0)
			ans[0] = 1;
		for(int i = 0; i < N; i++)
			System.out.print(ans[i]);

	}

}


import java.util.*;

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

		int N = sc.nextInt();
		int M = sc.nextInt();
		int[] v = new int[N];
		Arrays.fill(v, -1);

		for(int i=0; i<M; i++) {
			int s = sc.nextInt()-1;
			int c = sc.nextInt();

			if(v[s]<0) {
				v[s] = c;
			} else if(v[s]!=c) {
				System.out.println(-1);
				sc.close();
				return;
			}
		}
		if(N>1 && v[0]==0) {
			System.out.println(-1);
			sc.close();
			return;
		}
		for(int i=0; i<N; i++) {
			System.out.print(v[i]>=0 ? v[i] : (i==0 ? (N==1 ? 0 : 1) : 0));
		}
		System.out.println();
		sc.close();
	}
}


import java.io.InputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.math.*;
import java.util.*;

public class Main {

  public static void main(String[] args) {
    solve(System.in, System.out);
  }

  static void solve(InputStream is, PrintStream os) {
    Scanner sc = new Scanner(is);

    /* read */
    int n = sc.nextInt();
    int m = sc.nextInt();

    Integer[] values = new Integer[n];

    for (int i = 0; i < n; i++) {
      values[i] = null;
    }

    for (int i = 0; i < m; i++) {
      int s = sc.nextInt() - 1;
      int c = sc.nextInt();
      if (values[s] != null && c != values[s]) {
        os.println(-1);
        return;
      }
      values[s] = c;
    }
    String ans = "";
    if (values[0] != null) {
      if (values[0] == 0) {
        os.println(-1);
        return;
      } else {
        ans += values[0];
      }
    } else {
      ans += "1";
    }

    for (int i = 1; i < n; i++) {
      if (values[i] != null) {
        ans += String.valueOf(values[i]);
      } else {
        ans += "0";
      }
    }
    os.println(ans);
  }

  private static long gcd(long m, long n) {
    if (m < n) {
      return gcd(n, m);
    }
    if (n == 0) {
      return m;
    }
    return gcd(n, m % n);
  }

  private static long lcm(long m, long n) {
    return m / gcd(m, n) * n;
  }

  private static class Modular {

    private final long mod;

    private Modular(long mod) {
      this.mod = mod;
    }

    private long inverse(long n) {
      return pow(n, mod - 2) % mod;
    }

    private long pow(long x, long n) {
      /*
      long ans = 1;
      while (n > 0) {
        if ((n & 1) == 1) {
          ans = ans * x % mod;
        }
        x = x * x % mod;
        n >>= 1;
      }
      return ans;
      */
      if (n == 0) {
        return 1;
      }
      return n % 2 == 0 ?
          pow(x * x % mod, n / 2) % mod :
          x % mod * pow(x, n - 1) % mod;
    }
  }

  private static class ModularNFixedCombination {

    private final Modular modular;
    private final long[] comb;
    private final long N;

    public ModularNFixedCombination(long N, int size, long mod) {
      this.modular = new Modular(mod);
      this.comb = new long[size + 1];
      this.N = N;

      comb[0] = 1;

      for (int i = 1; i <= size; i++) {
        comb[i] = ((comb[i - 1] * (N - (i - 1))) % mod * modular.inverse(i)) % mod;
      }
    }

    long combination(int n, int k) {
      if (n != N) {
        throw new IllegalStateException();
      }
      return comb[k];
    }

    long repeatedCombination(int n, int k) {
      return combination(n + k - 1, k);
    }
  }

  private static class ModularCombination {

    private final long fact[];
    private final long mod;
    private final Modular modular;

    public ModularCombination(int size, long mod) {
      this.fact = new long[size + 1];
      this.mod = mod;
      this.modular = new Modular(mod);

      this.fact[0] = 1;
      for (int i = 1; i <= size; i++) {
        fact[i] = (fact[i - 1] * i) % mod;
      }
    }

    private long factorial(int n) {
      return fact[n];
    }

    long combination(int n, int k) {
      return
          (
              (
                  (
                      factorial(n) * modular.inverse(factorial(n - k))
                  ) % mod
              ) * modular.inverse(factorial(k))
          ) % mod;
    }

    long repeatedCombination(int n, int k) {
      return combination(n + k - 1, k);
    }
  }

  private static class Scanner {

    private final InputStream is;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    Scanner(InputStream is) {
      this.is = is;
    }

    private boolean hasNextByte() {
      if (ptr < buflen) {
        return true;
      } else {
        ptr = 0;
        try {
          buflen = is.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (buflen <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) {
        return buffer[ptr++];
      } else {
        return -1;
      }
    }

    private static boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    public boolean hasNext() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
        ptr++;
      }
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    public long nextLong() {
      if (!hasNext()) {
        throw new NoSuchElementException();
      }
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    public int nextInt() {
      long nl = nextLong();
      if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) {
        throw new NumberFormatException();
      }
      return (int) nl;
    }

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


import java.util.*;
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		int[] ans = new int[N];
		boolean[] used = new boolean[N];
		for(int i = 0; i < M; i++) {
			int s = sc.nextInt();
			int c = sc.nextInt();
			if(used[s-1] && ans[s-1] != c) {
				System.out.println(-1);
				return;
			}
			ans[s-1] = c;
			used[s-1] = true;
		}
		if(used[0] && ans[0] == 0) {
			System.out.println(-1);
			return;
		}
		ans[0] = 1;
		for(int i = 0; i < N; i++)
			System.out.print(ans[i]);

	}

}


import java.util.Arrays;
import java.util.Scanner;

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

    int N = scanner.nextInt();
    int M = scanner.nextInt();
    int[] digits = new int[N];
    Arrays.fill(digits, -1);
    for (int i = 0; i < M; i++) {
      int s = scanner.nextInt() - 1;
      int c = scanner.nextInt();
      if (digits[s] >= 0 && digits[s] != c) {
        System.out.println(-1);
        return;
      }
      digits[s] = c;
    }
    if (digits[0] == 0 && N != 1) {
      System.out.println(-1);
      return;
    }

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < N; i++) {
      if (digits[i] >= 0) sb.append(digits[i]);
      else {
        if (i == 0) sb.append(N == 1 ? "0" : "1");
        else sb.append("0");
      }
    }
    System.out.println(sb.toString());
  }
}


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

public class Main {
	static int n;
	static long k;
	static long a[];
	static void solve() {
		n = ni(); k = nl();
		a = new long[n];
		for(int i=0;i<n;i++)a[i] = ni();
		sort(a);
		long inf = (long)1e18;
		long l = -inf-1, r = inf+1;
		while(r - l > 1) {
			long c = (l + r)/2;
			if(isMoreK(c))r = c;
			else l = c;
		}

		out.println(r);
	}

	//c以下の値が、k個以上の時、true
	private static boolean isMoreK(long c) {
		long num = 0;//c以下の値の数を数える
		for(int i=0;i<n;i++) {
			int l = -1, r = n;
			while(r - l > 1) {
				int m = (l+r)/2;
				if(a[i]>=0){
					if(a[i]*a[m] <= c)l=m;
					else r = m;
				}
				else {
					if(a[i]*a[m] <= c)r=m;
					else l = m;
				}
			}
			if(a[i]>=0) {
				num +=  r;
				if(i<=l)num--;
			}
			else {
				num += n - r;
				if(i>l)num--;
			}
		}
		return num/2 >= k;
	}

	//constant
	static final long mod = (long) 1e9 + 7;
	static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
	static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 };
	static final int inf = Integer.MAX_VALUE / 3;
	static final long linf = Long.MAX_VALUE / 3;
	static final double dinf = Double.MAX_VALUE / 3;
	static final double eps = 1e-10;
	static final double pi = Math.PI;
	static StringBuilder sb = new StringBuilder();
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";

	//libraries
	static void reverse(int ar[]) {
		int len = ar.length;
		for (int i = 0; i < len / 2; i++) {
			int t = ar[i];
			ar[i] = ar[len - 1 - i];
			ar[len - 1 - i] = t;
		}
	}

	static void reverse(long ar[]) {
		int len = ar.length;
		for (int i = 0; i < len / 2; i++) {
			long t = ar[i];
			ar[i] = ar[len - 1 - i];
			ar[len - 1 - i] = t;
		}
	}

	static void reverse(double ar[]) {
		int len = ar.length;
		for (int i = 0; i < len / 2; i++) {
			double t = ar[i];
			ar[i] = ar[len - 1 - i];
			ar[len - 1 - i] = t;
		}
	}

	static void reverse(char ar[]) {
		int len = ar.length;
		for (int i = 0; i < len / 2; i++) {
			char t = ar[i];
			ar[i] = ar[len - 1 - i];
			ar[len - 1 - i] = t;
		}
	}

	static String getReverse(String s) {
		char c[] = s.toCharArray();
		reverse(c);
		s = String.valueOf(c);
		return s;
	}

	static <T> void reverse(List<T> ls) {
		int sz = ls.size();
		for (int i = 0; i < sz / 2; i++) {
			T t = ls.get(i);
			ls.set(i, ls.get(sz - 1 - i));
			ls.set(sz - 1 - i, t);
		}
	}

	static <T> void reverse(T[] ar) {
		int len = ar.length;
		for (int i = 0; i < len / 2; i++) {
			T t = ar[i];
			ar[i] = ar[len - 1 - i];
			ar[len - 1 - i] = t;
		}
	}

	static void sbnl() {//StringBuilderに改行文字をappendする
		sb.append("\n");
	}

	static int lowerBound(int[] a, int x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] < x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int upperBound(int[] a, int x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] <= x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int rlowerBound(int[] a, int x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] > x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int rupperBound(int[] a, int x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] >= x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int lowerBound(long[] a, long x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] < x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int upperBound(long[] a, long x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] <= x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int rlowerBound(long[] a, long x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] > x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int rupperBound(long[] a, long x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] >= x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int lowerBound(double[] a, double x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] < x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int upperBound(double[] a, double x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] <= x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int rlowerBound(double[] a, double x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] > x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int rupperBound(double[] a, double x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] >= x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int lowerBound(char[] a, char x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] < x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int upperBound(char[] a, char x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] <= x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int rlowerBound(char[] a, char x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] > x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static int rupperBound(char[] a, char x) {
		int l = -1, r = a.length;
		while (r - l > 1) {
			int c = (l + r) / 2;
			if (a[c] >= x) {
				l = c;
			} else {
				r = c;
			}
		}
		return r;
	}

	static <T> int lowerBound(List<T> ls, T x) throws RuntimeException {
		if (ls.size() == 0)
			return -1;
		if (ls.get(0) instanceof Integer) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) >= 0 ? 1 : -1);
		} else if (ls.get(0) instanceof Long) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) >= 0 ? 1 : -1);
		} else if (ls.get(0) instanceof Double) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) >= 0 ? 1 : -1);
		} else {
			System.err.println(
					String.format("%s:数値でないリストを二分探索しています。", Thread.currentThread().getStackTrace()[1].getMethodName()));
			throw new RuntimeException();
		}
	}

	static <T> int upperBound(List<T> ls, T x) throws RuntimeException {
		if (ls.size() == 0)
			return -1;
		if (ls.get(0) instanceof Integer) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) > 0 ? 1 : -1);
		} else if (ls.get(0) instanceof Long) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) > 0 ? 1 : -1);
		} else if (ls.get(0) instanceof Double) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) > 0 ? 1 : -1);
		} else {
			System.err.println(
					String.format("%s:数値でないリストを二分探索しています。", Thread.currentThread().getStackTrace()[1].getMethodName()));
			throw new RuntimeException();
		}
	}

	static <T> int rupperBound(List<T> ls, T x) throws RuntimeException {
		if (ls.size() == 0)
			return -1;
		if (ls.get(0) instanceof Integer) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) < 0 ? 1 : -1);
		} else if (ls.get(0) instanceof Long) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) < 0 ? 1 : -1);
		} else if (ls.get(0) instanceof Double) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) < 0 ? 1 : -1);
		} else {
			System.err.println(
					String.format("%s:数値でないリストを二分探索しています。", Thread.currentThread().getStackTrace()[1].getMethodName()));
			throw new RuntimeException();
		}
	}

	static <T> int rlowerBound(List<T> ls, T x) {
		if (ls.size() == 0)
			return -1;
		if (ls.get(0) instanceof Integer) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) <= 0 ? 1 : -1);
		} else if (ls.get(0) instanceof Long) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) <= 0 ? 1 : -1);
		} else if (ls.get(0) instanceof Double) {
			return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) <= 0 ? 1 : -1);
		} else {
			System.err.println(
					String.format("%s:数値でないリストを二分探索しています。", Thread.currentThread().getStackTrace()[1].getMethodName()));
			throw new RuntimeException();
		}
	}

	static int[] concat(int x, int arr[]) {
		int ret[] = new int[arr.length + 1];
		System.arraycopy(arr, 0, ret, 1, ret.length - 1);
		ret[0] = x;
		return ret;
	}

	static int[] concat(int arr[], int x) {
		int ret[] = new int[arr.length + 1];
		System.arraycopy(arr, 0, ret, 0, ret.length - 1);
		ret[ret.length - 1] = x;
		return ret;
	}

	static long[] concat(long x, long arr[]) {
		long ret[] = new long[arr.length + 1];
		System.arraycopy(arr, 0, ret, 1, ret.length - 1);
		ret[0] = x;
		return ret;
	}

	static long[] concat(long arr[], long x) {
		long ret[] = new long[arr.length + 1];
		System.arraycopy(arr, 0, ret, 0, ret.length - 1);
		ret[ret.length - 1] = x;
		return ret;
	}

	static int max(int x, int y) {
		return Math.max(x, y);
	}

	static int min(int x, int y) {
		return Math.min(x, y);
	}

	static int max(int x, int y, int z) {
		x = Math.max(x, y);
		x = Math.max(x, z);
		return x;
	}

	static int min(int x, int y, int z) {
		x = Math.min(x, y);
		x = Math.min(x, z);
		return x;
	}

	static long max(long x, long y) {
		return Math.max(x, y);
	}

	static long min(long x, long y) {
		return Math.min(x, y);
	}

	static long max(long x, long y, long z) {
		x = Math.max(x, y);
		x = Math.max(x, z);
		return x;
	}

	static long min(long x, long y, long z) {
		x = Math.min(x, y);
		x = Math.min(x, z);
		return x;
	}

	static double max(double x, double y) {
		return Math.max(x, y);
	}

	static double min(double x, double y) {
		return Math.min(x, y);
	}

	static double max(double x, double y, double z) {
		x = Math.max(x, y);
		x = Math.max(x, z);
		return x;
	}

	static double min(double x, double y, double z) {
		x = Math.min(x, y);
		x = Math.min(x, z);
		return x;
	}

	static void sort(int[] ar) {
		Arrays.sort(ar);
	}

	static void sort(long[] ar) {
		Arrays.sort(ar);
	}

	static void sort(double[] ar) {
		Arrays.sort(ar);
	}

	static void sort(char[] ar) {
		Arrays.sort(ar);
	}

	static void rsort(int[] ar) {
		Arrays.sort(ar);
		int len = ar.length;
		for (int i = 0; i < len / 2; i++) {
			int tmp = ar[i];
			ar[i] = ar[len - 1 - i];
			ar[len - 1 - i] = tmp;
		}
	}

	static void rsort(long[] ar) {
		Arrays.sort(ar);
		int len = ar.length;
		for (int i = 0; i < len / 2; i++) {
			long tmp = ar[i];
			ar[i] = ar[len - 1 - i];
			ar[len - 1 - i] = tmp;
		}
	}

	static void rsort(double[] ar) {
		Arrays.sort(ar);
		int len = ar.length;
		for (int i = 0; i < len / 2; i++) {
			double tmp = ar[i];
			ar[i] = ar[len - 1 - i];
			ar[len - 1 - i] = tmp;
		}
	}

	static void rsort(char[] ar) {
		Arrays.sort(ar);
		int len = ar.length;
		for (int i = 0; i < len / 2; i++) {
			char tmp = ar[i];
			ar[i] = ar[len - 1 - i];
			ar[len - 1 - i] = tmp;
		}
	}

	static void fill(int arr[], int x) {
		Arrays.fill(arr, x);
	}

	static void fill(long arr[], long x) {
		Arrays.fill(arr, x);
	}

	static void fill(boolean arr[], boolean x) {
		Arrays.fill(arr, x);
	}

	static void fill(double arr[], double x) {
		Arrays.fill(arr, x);
	}

	static void fill(int arr[][], int x) {
		for (int i = 0; i < arr.length; i++)
			Arrays.fill(arr[i], x);
	}

	static void fill(long arr[][], long x) {
		for (int i = 0; i < arr.length; i++)
			Arrays.fill(arr[i], x);
	}

	static void fill(double arr[][], double x) {
		for (int i = 0; i < arr.length; i++)
			Arrays.fill(arr[i], x);
	}

	static void fill(boolean arr[][], boolean x) {
		for (int i = 0; i < arr.length; i++)
			Arrays.fill(arr[i], x);
	}

	//MOD culc
	static long plus(long x, long y) {
		long res = (x + y) % mod;
		return res < 0 ? res + mod : res;
	}

	static long sub(long x, long y) {
		long res = (x - y) % mod;
		return res < 0 ? res + mod : res;
	}

	static long mul(long x, long y) {
		long res = (x * y) % mod;
		return res < 0 ? res + mod : res;
	}

	static long div(long x, long y) {
		long res = x * pow(y, mod - 2) % mod;
		return res < 0 ? res + mod : res;
	}

	static long pow(long x, long y) {
		if (y < 0)
			return 0;
		if (y == 0)
			return 1;
		if (y % 2 == 1)
			return (x * pow(x, y - 1)) % mod;
		long root = pow(x, y / 2);
		return root * root % mod;
	}

	public static void main(String[] args) throws Exception {
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		solve();
		out.flush();
	}

	//input
	private static byte[] inbuf = new byte[1024];
	static int lenbuf = 0, ptrbuf = 0;

	private static int readByte() {
		if (lenbuf == -1)
			throw new InputMismatchException();
		if (ptrbuf >= lenbuf) {
			ptrbuf = 0;
			try {
				lenbuf = is.read(inbuf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (lenbuf <= 0)
				return -1;
		}
		return inbuf[ptrbuf++];
	}

	private static boolean isSpaceChar(int c) {
		return !(c >= 33 && c <= 126);
	}

	private static int skip() {
		int b;
		while ((b = readByte()) != -1 && isSpaceChar(b))
			;
		return b;
	}

	@SuppressWarnings("unused")
	private static double nd() {
		return Double.parseDouble(ns());
	}

	@SuppressWarnings("unused")
	private static char nc() {
		return (char) skip();
	}

	private static String ns() {
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!(isSpaceChar(b))) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private static char[] ns(int n) {
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while (p < n && !(isSpaceChar(b))) {
			buf[p++] = (char) b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	@SuppressWarnings("unused")
	private static char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for (int i = 0; i < n; i++)
			map[i] = ns(m);
		return map;
	}

	@SuppressWarnings("unused")
	private static int[] na(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = ni();
		return a;
	}

	@SuppressWarnings("unused")
	private static long[] nla(int n) {
		long[] a = new long[n];
		for (int i = 0; i < n; i++)
			a[i] = nl();
		return a;
	}

	private static int ni() {
		int num = 0, b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	@SuppressWarnings("unused")
	private static long nl() {
		long num = 0;
		int b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

}

Score: 400 points

Problem Statement

We have N integers A_1, A_2, ..., A_N.

There are \frac{N(N-1)}{2} ways to choose two of them and form a pair. If we compute the product of each of those pairs and sort the results in ascending order, what will be the K-th number in that list?

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq K \leq \frac{N(N-1)}{2}

-10^9 \leq A_i \leq 10^9\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the answer.

Sample Input 1

4 3
3 3 -4 -2

Sample Output 1

-6

There are six ways to form a pair. The products of those pairs are 9, -12, -6, -12, -6, 8.

Sorting those numbers in ascending order, we have -12, -12, -6, -6, 8, 9. The third number in this list is -6.

Sample Input 2

10 40
5 4 3 2 -1 0 0 0 0 0

Sample Output 2

6

Sample Input 3

30 413
-170202098 -268409015 537203564 983211703 21608710 -443999067 -937727165 -97596546 -372334013 398994917 -972141167 798607104 -949068442 -959948616 37909651 0 886627544 -20098238 0 -948955241 0 -214720580 277222296 -18897162 834475626 0 -425610555 110117526 663621752 0

Sample Output 3

448283280358331064


import java.util.*;
import java.util.Collections;
/**
 * Main
 */
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// 整数の入力
		int N = sc.nextInt();
		int K = sc.nextInt();
		Long X;
		ArrayList<Long> list = new ArrayList<>();
		ArrayList<Long> List = new ArrayList<>();
		for(int i = 0;i<N;i++){
			X = sc.nextLong();
			if(list.size()>0){
				for(int j = 0; j < list.size(); j++){
					List.add(X*list.get(j));
				}
			}
			list.add(X);
		}
		Collections.sort(List);
		System.out.println(List.get(K-1));
		sc.close();
	}
};


import java.util.Arrays;
import java.util.Scanner;

// ref. https://atcoder.jp/contests/abc155/submissions/10152895
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    long K = sc.nextLong();
    long[] A = new long[N];
    for (int i = 0; i < N; i++) {
      A[i] = sc.nextInt();
    }
    Arrays.sort(A);

    long l = -1000_000_000_000_000_000L;
    long r = 1000_000_000_000_000_000L;
    //    long l = -20;
    //    long r = 20;
    while (l + 1 < r) {
      long kThProd = (l + r) / 2;
      long cnt = countPairLessThan(kThProd, A);
      if (cnt < K) { // Find 'cnt == K -1', then 'kThProd' is K-th smallest product.
        l = kThProd; // l:ok.
      } else {
        r = kThProd; // r:ng.
      }
    }
    System.out.println(l);
  }

  private static long countPairLessThan(long m, long[] A) {
    long tot = 0;
    for (long a : A) {
      int l = -1, r = A.length;
      if (a > 0) {
        while (l + 1 < r) {
          int c = (l + r) / 2;
          if (a * A[c] < m) l = c; // l:ok, r:ng
          else r = c;
        }
        tot += l + 1; // Because index starts with 0. OK count is (l+1).
      } else {
        while (l + 1 < r) {
          int c = (l + r) / 2;
          if (a * A[c] < m) r = c; // l:ng, r:ok
          else l = c;
        }
        tot +=
            A.length - (l + 1); // Remove NG count. Because index starts with 0. NG count is (l+1).
      }
      if (a * a < m) tot--;
    }

    return tot / 2;
  }
}

// i < j
// Search m, where count(a*x <= m) equals K
// -----------------
// condition of a | count where | count
// -----------------
// a > 0: x <= m/a | |
// a== 0:          | | num-zero * x
// a < 0: x >= m/a | | N- count(x < m/a) <=> N-count(x <= m/a -1)
//   ref: 3/(-2) => -1


import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

//System.out.println();
public class Main { //クラス名はMain
    public static void main(String[] args) {
        //コード
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int k = Integer.parseInt(sc.next());
        ArrayList<Long> ori = new ArrayList<Long>();
        ArrayList<Long> re = new ArrayList<Long>();

        for(int i=0; i<n; i++){
        	ori.add(Long.parseLong(sc.next()));
        }
        for(int l=0; l<ori.size(); l++){
        	if(l!=n-1) {
	        for(int j=l+1; j<ori.size(); j++){
	        	long r = ori.get(j)*ori.get(l);
	        	re.add(r);
	        }
        	}
        }
        Collections.sort(re);
        System.out.println(re.get(k-1));
    }
}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Main {
	static int bsearch(List<Long> ps , long x){
		if(ps.isEmpty()){
			return 0;
		}
		if(ps.get(0) >= x){
			return 0;
		}
		if(ps.get(ps.size() - 1) < x){
			return ps.size();
		}
		int left = 0;
		int right = ps.size() - 1;
		while(right - left > 1){
			int mid = (left + right) / 2;
			long v = ps.get(mid);
			if(v < x){
				left = mid;
			}else{
				right = mid;
			}
		}
		return right;
	}
	// x 以下の数値が何個あるか計算する (xはマイナスとする)
	static long f(List<Long> negs , List<Long> poss , long x){
		long ret = 0;
		for(long n : negs){ //n -3  x -81
			long m = (- x + (- n - 1)) /(- n);
			// x以下となるのは m が 26以上のとき
			int cnt = bsearch(poss, m);
			ret += poss.size() - cnt;
		}
		return ret;
	}
	static long g(List<Long> poss , long x){
		long ret = 0;
		int N = poss.size();
		for(int i = 0 ; i < poss.size() - 1; ++i){
			long pi = poss.get(i);
			long m = x / pi;
			int left = i + 1;
			int right = N - 1;
			if(poss.get(left) > m){
				continue;
			}
			if(poss.get(right) <= m){
				ret += N - i - 1;
				continue;
			}
			while(right - left > 1){
				int mid = (left + right) / 2;
				if(poss.get(mid) <= m){
					left = mid;
				}else{
					right = mid;
				}
			}
			ret += (left) - i;
		}
		return ret;
	}

	static long solve(long A[] , long K){
		List<Long> neg = new ArrayList<Long>();
		List<Long> neg2 = new ArrayList<Long>();
		List<Long> pos = new ArrayList<Long>();
		List<Long> zero = new ArrayList<Long>();
		for(long a : A){
			if(a < 0){
				neg.add(a);
				neg2.add(- a);
			}else if(a == 0){
				zero.add(a);
			}else{
				pos.add(a);
			}
		}
		Collections.sort(neg);
		Collections.sort(neg2);
		Collections.sort(pos);
		Collections.sort(zero);
		long neg_num = neg.size() * pos.size();
		long zero_num = zero.size() * (pos.size() + neg.size()) + (zero.size() * (zero.size() - 1)) / 2;
		if(K > neg_num && K <= neg_num + zero_num){
			return 0;
		}else{
			if(K <= neg_num){
				long left = - (1000000000L * 1000000000L) - 1;
				long right = 0;
				while(right - left > 1){
					long mid = (left + right) / 2;
					long f = f(neg, pos , mid);
					if(K <= f){
						right = mid;
					}else{
						left = mid;
					}
				}
				return right;
			}else{
				long left = 0;
				long right = (1000000000L * 1000000000L) + 1;
				while(right - left > 1){
					long mid = (left + right) / 2;
					long f = g(neg2, mid) + g(pos , mid) + neg_num + zero_num;
					if(K <= f){
						right = mid;
					}else{
						left = mid;
					}
				}
				return right;
			}
		}
	}
	static long solveNaive(long A[] , int K){
		List<Long> lst = new ArrayList<Long>();
		int N = A.length;
		for(int i = 0 ; i < N ; ++i){
			for(int j = i +1 ; j < N ; ++j){
				lst.add(A[i] * A[j]);
			}
		}
		Collections.sort(lst);
		return lst.get(K - 1);
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long K = sc.nextLong();
		long A[] = new long[N];
		for(int i = 0 ; i < N ; ++i){
			long a = sc.nextLong();
			A[i] = a;
		}
		System.out.println(solve(A, K));
	}
}


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

public class Main implements Runnable {
	boolean judge = true;
	FastReader scn;
	PrintWriter out;
	String INPUT = "";

	void solve() {
		int n = scn.nextInt();
		long k = scn.nextLong();
		long[] arr = scn.nextLongArray(n);
		Arrays.parallelSort(arr);

		long[] pos = new long[n], neg = new long[n];
		int p = 0, q = 0, z = 0;
		for (int i = 0; i < n; i++) {
			if (arr[i] > 0) {
				pos[p++] = arr[i];
			} else if (arr[i] < 0) {
				neg[q++] = -arr[i];
			} else {
				z++;
			}
		}

		pos = Arrays.copyOf(pos, p);
		neg = Arrays.copyOf(neg, q);

		Arrays.parallelSort(pos);
		Arrays.parallelSort(neg);

		long l = -(long) 1e18, r = (long) 1e18, ans = (long) 2e18;
		while (l <= r) {
			long m = (l + r) >> 1;
			long small = smaller(pos, neg, z, m);
			if (small >= k) {
				r = m - 1;
				ans = m;
			} else {
				l = m + 1;
			}
		}

		out.println(ans);
	}

	long smaller(long[] pos, long[] neg, long z, long x) {
		long rv = 0;
		if (x >= 0) {
			for (int i = 0; i < pos.length; i++) {
				long y = x / pos[i];
				if (x % pos[i] == 0) {
					y++;
				}
				int l = i + 1, r = pos.length - 1, ind = pos.length;
				while (l <= r) {
					int m = (l + r) >> 1;
					if (pos[m] > y) {
						ind = m;
						r = m - 1;
					} else {
						l = m + 1;
					}
				}
				rv += ind - (i + 1);
			}
			for (int i = 0; i < neg.length; i++) {
				long y = x / neg[i];
				if (x % neg[i] == 0) {
					y++;
				}
				int l = i + 1, r = neg.length - 1, ind = neg.length;
				while (l <= r) {
					int m = (l + r) >> 1;
					if (neg[m] > y) {
						ind = m;
						r = m - 1;
					} else {
						l = m + 1;
					}
				}
				rv += ind - (i + 1);
			}
			rv += pos.length * 1L * neg.length;
			rv += z * (z - 1) / 2;
			rv += pos.length * z;
			rv += neg.length * z;
		} else {
			x = -x;
			for (int i = 0; i < pos.length; i++) {
				long y = x / pos[i];
				if (x % pos[i] != 0) {
					y++;
				}
				int l = 0, r = neg.length - 1, ind = neg.length;
				while (l <= r) {
					int m = (l + r) >> 1;
					if (neg[m] >= y) {
						ind = m;
						r = m - 1;
					} else {
						l = m + 1;
					}
				}

				rv += neg.length - ind;
			}
		}

		return rv;
	}

	public void run() {
		long time = System.currentTimeMillis();
		boolean oj = System.getProperty("ONLINE_JUDGE") != null || judge;
		out = new PrintWriter(System.out);
		scn = new FastReader(oj);
		solve();
		out.flush();
		if (!oj) {
			System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
		}
	}

	public static void main(String[] args) {
		new Thread(null, new Main(), "Main", 1 << 26).start();
	}

	class FastReader {
		InputStream is;

		public FastReader(boolean onlineJudge) {
			is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		}

		byte[] inbuf = new byte[1024];
		public int lenbuf = 0, ptrbuf = 0;

		int readByte() {
			if (lenbuf == -1)
				throw new InputMismatchException();
			if (ptrbuf >= lenbuf) {
				ptrbuf = 0;
				try {
					lenbuf = is.read(inbuf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (lenbuf <= 0)
					return -1;
			}
			return inbuf[ptrbuf++];
		}

		boolean isSpaceChar(int c) {
			return !(c >= 33 && c <= 126);
		}

		int skip() {
			int b;
			while ((b = readByte()) != -1 && isSpaceChar(b))
				;
			return b;
		}

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

		char nextChar() {
			return (char) skip();
		}

		String next() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		String nextLine() {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ')
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		char[] next(int n) {
			char[] buf = new char[n];
			int b = skip(), p = 0;
			while (p < n && !(isSpaceChar(b))) {
				buf[p++] = (char) b;
				b = readByte();
			}
			return n == p ? buf : Arrays.copyOf(buf, p);
		}

		int nextInt() {
			int num = 0, b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		long nextLong() {
			long num = 0;
			int b;
			boolean minus = false;
			while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
				;
			if (b == '-') {
				minus = true;
				b = readByte();
			}

			while (true) {
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
				b = readByte();
			}
		}

		char[][] nextMatrix(int n, int m) {
			char[][] map = new char[n][];
			for (int i = 0; i < n; i++)
				map[i] = next(m);
			return map;
		}

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

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

		int[][] next2DInt(int n, int m) {
			int[][] arr = new int[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextIntArray(m);
			}
			return arr;
		}

		long[][] next2DLong(int n, int m) {
			long[][] arr = new long[n][];
			for (int i = 0; i < n; i++) {
				arr[i] = nextLongArray(m);
			}
			return arr;
		}

		int[] shuffle(int[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				int c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		long[] shuffle(long[] arr) {
			Random r = new Random();
			for (int i = 1, j; i < arr.length; i++) {
				j = r.nextInt(i);
				long c = arr[i];
				arr[i] = arr[j];
				arr[j] = c;
			}
			return arr;
		}

		int[] uniq(int[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			int[] rv = new int[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		long[] uniq(long[] arr) {
			arr = scn.shuffle(arr);
			Arrays.sort(arr);
			long[] rv = new long[arr.length];
			int pos = 0;
			rv[pos++] = arr[0];
			for (int i = 1; i < arr.length; i++) {
				if (arr[i] != arr[i - 1]) {
					rv[pos++] = arr[i];
				}
			}
			return Arrays.copyOf(rv, pos);
		}

		int[] reverse(int[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		long[] reverse(long[] arr) {
			int l = 0, r = arr.length - 1;
			while (l < r) {
				arr[l] = arr[l] ^ arr[r];
				arr[r] = arr[l] ^ arr[r];
				arr[l] = arr[l] ^ arr[r];
				l++;
				r--;
			}
			return arr;
		}

		int[] compress(int[] arr) {
			int n = arr.length;
			int[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}

		long[] compress(long[] arr) {
			int n = arr.length;
			long[] rv = Arrays.copyOf(arr, n);
			rv = uniq(rv);
			for (int i = 0; i < n; i++) {
				arr[i] = Arrays.binarySearch(rv, arr[i]);
			}
			return arr;
		}
	}
}


import java.util.Scanner;

public class Main {

	static void quick_sort(long[] d, int left, int right) {
		if (left >= right) {
			return;
		}
		long p = d[(left + right) / 2];
		int l = left, r = right;
		long tmp;
		while (l <= r) {
			while (d[l] < p) {
				l++;
			}
			while (d[r] > p) {
				r--;
			}
			if (l <= r) {
				tmp = d[l];
				d[l] = d[r];
				d[r] = tmp;
				l++;
				r--;
			}
		}
		quick_sort(d, left, r); // ピボットより左側をクイックソート
		quick_sort(d, l, right); // ピボットより右側をクイックソート
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int K = sc.nextInt();
		int c = N * (N - 1) / 2;
		long set[] = new long[c];
		long number[] = new long[N];
		for (int i = 0; i < N; i++) {
			number[i] = sc.nextLong();
		}
		int k = 0;
		for (int i = 0; i < N - 1; i++) {
			for (int j = i + 1; j < N; j++) {
				if (i != j) {
					set[k] = number[i] * number[j];
					k++;
				}
			}
		}

		quick_sort(set, 0, c - 1);

		System.out.println(set[K - 1]);

	}
}


import java.util.*;

public class Main {
  public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
    	int a = sc.nextInt();
    	int b = sc.nextInt();
    	if(a < 10 && b < 10) {
          System.out.println(a * b);
        } else {
          System.out.println(-1);
        }
  }
}

Score : 100 points

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. He cannot do any other calculation.

Given are two integers A and B.

If Takahashi can calculate A \times B, print the result; if he cannot, print -1 instead.

Constraints

1 \leq A \leq 20

1 \leq B \leq 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If Takahashi can calculate A \times B, print the result; if he cannot, print -1.

Sample Input 1

2 5

Sample Output 1

10

2 \times 5 = 10.

Sample Input 2

5 10

Sample Output 2

-1

5\times 10 = 50, but Takahashi cannot do this calculation, so print -1 instead.

Sample Input 3

9 9

Sample Output 3

81


import java.util.*;

public class Main {
  public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    int B = sc.nextInt();
    sc.close();

    if (1 <= A && A <= 9 && 1 <= B && B <= 9) {
      System.out.println(A * B);
    } else {
      System.out.println(-1);
    }
  }
}


import java.util.*;

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

        int a = sc.nextInt();
        int b = sc.nextInt();

        if (a <= 9 && b <= 9) {
            System.out.println(a * b);
        } else {
            System.out.println(-1);
        }
	}
}


import java.util.Scanner;

/**
 *
 * @author C0117177
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();
        int b = sc.nextInt();

        if(a <= 9 && b<= 9){
            System.out.println(a * b);
        }else{
            System.out.println(-1);
        }
        sc.close();
    }
}


import java.util.*;

public class Main {
    public static void main(String[] args) {
      Scanner scan = new Scanner(System.in);
      int a = scan.nextInt();
      int b = scan.nextInt();
      int seki = a*b;
      System.out.println(seki);
    }
}


import java.util.*;

class Main{
public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);
	  int a = scan.nextInt();
	  int b = scan.nextInt();
	if(1<=a && a<=20 && 1<=b && b<=20) {
		System.out.println(a * b);
	}else {
		System.out.println(-1);
	}
 }
}


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

public class Main {
    public static void main(String[] args) {
        FastReader sc = new FastReader();
        int A = sc.nextInt(), B = sc.nextInt();
        if (A>9 || B>9) {
            System.out.println(-1);
        } else {
            System.out.println(A*B);
        }
    }
    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());
        }

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


import java.io.*;
import java.util.*;
 class Main
{
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
int a=s.nextInt();
int b=s.nextInt();
if(a>=1 && a<=9 && b>=1 && b<=9)
System.out.println(a*b);
else
System.out.println("-1");}}


import java.util.*;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int A = sc.nextInt();
		int B = sc.nextInt();

		if (A >= 1 && A <= 9 && B >= 1 && B <= 9) {
			System.out.println(A * B);
		} else {
			System.out.println(-1);
		}

	}

}


import java.util.*;
public class Main {
    public static void main(String args[]) {
      Scanner sc=new Scanner(System.in);
      int a=sc.nextInt();
      int b=sc.nextInt();
      if(a>=1&&b<=9)
      System.out.println((a*b));
      else
      System.out.println("-1");
    }
}


import java.util.*;

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

      int a = Integer.parseInt(sc.next());
      int b = Integer.parseInt(sc.next());

      if(a<10 && b<10){
        System.out.println(a*b);
      }
      else{
        System.out.println("-1");
      }

    }
}


import java.util.Scanner;

public class Main {

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

		char[] str = sc.next().toCharArray();
		int k = sc.nextInt();;

		if(str.length == 1){
			System.out.println(k/2);
			return;
		}
		int changeCount = 0;
		for(int i = 1; i < str.length; i++){
			if(str[i] == str[i - 1]){
				changeCount++;
				str[i] = 'A';
			}
		}
//		System.out.println(changeCount);
		long result = (long)k*changeCount;

		if(str[0] == str[str.length - 1]){
			result += k - 1;
		}
		System.out.println(result);
	}

}

Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997


import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;

class Main{

  	static int check = -1;
	public static void main(String[] args){
      	Scanner sc = new Scanner(System.in);
      	String line = sc.nextLine();
      	long n = sc.nextLong();

     	long count;
      	long c = 0l;
      	if(line.length() > 1){
          count = count(line);
          c = count2(line);
      	  System.out.println(count*n + c*(n-1));
        }else{
          if(n == 1){
            System.out.println(0);
          }else{
             String l = line;
             for(int i = 0; i < n-1; i++) l += line;
             count = count(l);
             c = count2(l);
             System.out.println(count+c);
          }
        }
    }

  	static long count(String line){
    	long count = 0;
      	int i = 1;
    	while(i <= line.length()-1){
          System.out.println(i);
    		if(line.charAt(i) == line.charAt(i-1)){
              count++;
              i += 2;
              check = i;
            }else{
              i++;
            }
    	}
    	return count;
    }

  	static long count2(String line){
    	return (line.length() > 2 &&
                (check == line.length()) &&
                line.charAt(0) == line.charAt(line.length()-1)) ? 1: 0;
    }
}


import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String S = sc.next();
		long K = sc.nextLong();
		long count = 0;
		long check = 1;
		int l = S.length();
		int first = 1;
		int last = 1;
		int oddCheck = 1;
		if(l > 2 && S.substring(0,1).equals(S.substring(l-1,l))) {
			for(int i = 0; i < l-1; i++) {
				if(S.substring(i,i+1).equals(S.substring(i+1,i+2))) {
					first++;
				}else {
					break;
				}
			}
			for(int i = l; i > 1; i--) {
				if(S.substring(i-1,i).equals(S.substring(i-2,i-1))) {
					last++;
				}else {
					break;
				}
			}
			for(int i = first; i <= l-last-1; i++) {
				if(S.substring(i, i+1).equals(S.substring(i+1, i+2))) {
					check++;
				}else {
					count += (int)(check / 2) * K;
					check = 1;
				}
			}
			count += (first + last)/2 * K;
			for(int i = 0; i < l-1; i++) {
				if(S.substring(i,i+1).equals(S.substring(i+1,i+2))) {
					oddCheck++;
				}else
				break;
			}
			if(oddCheck == l) {
				count = l*K / 2;
			}
		}else if(l > 2 && !(S.substring(0,1).equals(S.substring(l-1,l)))) {
			for(int i = 0; i < l-1; i++) {
				if(S.substring(i,i+1).equals(S.substring(i+1,i+2))) {
					first++;
				}else {
					break;
				}
			}
			for(int i = l; i > 1; i--) {
				if(S.substring(i-1,i).equals(S.substring(i-2,i-1))) {
					last++;
				}else {
					break;
				}
			}
			for(int i = first; i <= l-last-1; i++) {
				if(S.substring(i, i+1).equals(S.substring(i+1, i+2))) {
					check++;
				}else {
					count += (int)(check / 2) * K;
					check = 1;
				}
			}
			count += (first + last)/2 * K;
		}else if(l == 2 && S.substring(0,1).equals(S.substring(1,2))) {
			count += K;
		}else if(l == 2 && !(S.substring(0,1).equals(S.substring(1,2)))) {
			count = 0;
		}else{	//|S|=1
			count += K /2 ;
		}
		System.out.println(count);
		sc.close();
	}

}


// @uthor -: puneet
// TimeStamp -: 5:29 PM - 5/10/19

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

public class Main implements Runnable {

    public void solve(){
        int n=in.ni();
        ArrayList<Integer>[] lists=new ArrayList[n];
        for(int i=0;i<n;++i){
            lists[i]=new ArrayList<>();
        }

        for(int i=0;i<n;++i){
            char[] chars=in.ns().toCharArray();
            for(int j=0;j<n;++j){
                if(chars[j]=='1'){
                    lists[i].add(j);
                }
            }
        }

        Arrays.fill(dist,-1);
        isOddCycle(lists,0,-1,1);

        if(isOdd){
            out.println(-1);
            return;
        }

        int ans=0;
        for(int i=0;i<n;++i)
            ans=Math.max(ans,bfs(lists,i));
        out.println(ans);

    }

    private int bfs(ArrayList<Integer>[] adj,int node) {

        Deque<Integer> queue=new ArrayDeque<>();
        queue.add(node);
        boolean [] vis=new boolean[201];
        int step=0;
        vis[node]=true;
        while (!queue.isEmpty()){

            int temp=-1;
            while(!queue.isEmpty()) {
                int i=queue.pollFirst();
                if(temp==i){
                    queue.addLast(temp);
                    break;
                }
                vis[i]=true;
                for (int j : adj[i]) {
                    if (!vis[j]) {
                        queue.addLast(j);
                        if(temp==-1)
                            temp=j;
                    }
                }
            }

            step++;
        }

        return step;
    }

    int[] dist=new int[201];
    boolean isOdd=false;

    private void isOddCycle(ArrayList<Integer>[] lists, int node,int par,int dis) {

        dist[node]=dis;

        for(int j:lists[node]){
            if(par==j)
                continue;
            if(dist[j]!=-1 && Math.abs(dist[j]-dist[node])%2==0){
                isOdd=true;
                break;
            }else if(dist[j]==-1)
                isOddCycle(lists,j,node,dis+1);

        }

    }

    /* -------------------- Templates and Input Classes -------------------------------*/

    @Override
    public void run() {
        try {
            init();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        solve();
        out.flush();
    }

    private FastInput in;
    private PrintWriter out;

    public static void main(String[] args) throws Exception {
        new Thread(null, new Main(), "Main", 1 << 27).start();
    }

    private void init() throws FileNotFoundException {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        out = new PrintWriter(outputStream);
        in = new FastInput(inputStream);
    }

    private void sort(int[] arr) {
        List<Integer> list = new ArrayList<>();
        for (int object : arr) list.add(object);
        Collections.sort(list);
        for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
    }

    private void sort(long[] arr) {
        List<Long> list = new ArrayList<>();
        for (long object : arr) list.add(object);
        Collections.sort(list);
        for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
    }

    public long ModPow(long x, long y, long MOD) {
        long res = 1L;
        x = x % MOD;
        while (y >= 1) {
            if ((y & 1) > 0) res = (res * x) % MOD;
            x = (x * x) % MOD;
            y >>= 1;
        }
        return res;
    }

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

    public long gcd(long a, long b) {
        if (a == 0) return b;
        return gcd(b % a, a);
    }

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

        public FastInput(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 peek() {
            if (numChars == -1) {
                return -1;
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    return -1;
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar];
        }

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

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

        public String ns() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                if (Character.isValidCodePoint(c)) {
                    res.appendCodePoint(c);
                }
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

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

        private String readLine0() {
            StringBuilder buf = new StringBuilder();
            int c = read();
            while (c != '\n' && c != -1) {
                if (c != '\r') {
                    buf.appendCodePoint(c);
                }
                c = read();
            }
            return buf.toString();
        }

        public String readLine() {
            String s = readLine0();
            while (s.trim().length() == 0) {
                s = readLine0();
            }
            return s;
        }

        public String readLine(boolean ignoreEmptyLines) {
            if (ignoreEmptyLines) {
                return readLine();
            } else {
                return readLine0();
            }
        }

        public BigInteger readBigInteger() {
            try {
                return new BigInteger(ns());
            } catch (NumberFormatException e) {
                throw new InputMismatchException();
            }
        }

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

        public double nd() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E') {
                    return res * Math.pow(10, ni());
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E') {
                        return res * Math.pow(10, ni());
                    }
                    if (c < '0' || c > '9') {
                        throw new InputMismatchException();
                    }
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public String next() {
            return ns();
        }

        public SpaceCharFilter getFilter() {
            return filter;
        }

        public void setFilter(SpaceCharFilter filter) {
            this.filter = filter;
        }

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

        public int[] intarr(int n) {
            int arr[] = new int[n];
            for (int i = 0; i < n; i++) {
                arr[i] = ni();
            }
            return arr;
        }

        public double[] doublearr(int n) {
            double arr[] = new double[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nd();
            }
            return arr;
        }

        public double[][] doublearr(int n, int m) {
            double[][] arr = new double[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    arr[i][j] = nd();
                }
            }
            return arr;
        }

        public int[][] intarr(int n, int m) {
            int[][] arr = new int[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    arr[i][j] = ni();
                }
            }
            return arr;
        }

        public long[][] longarr(int n, int m) {
            long[][] arr = new long[n][m];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    arr[i][j] = nl();
                }
            }
            return arr;
        }

        public long[] longarr(int n) {
            long arr[] = new long[n];
            for (int i = 0; i < n; i++) {
                arr[i] = nl();
            }
            return arr;
        }

    }

}


import java.util.Scanner;

public class Main {

	/*
	 * 余りの最大値を求める。
	 */
	public static void main(String[] args) {

		/*
		 * Scannerで値取得。
		 */
		Scanner sint = new Scanner(System.in);
		Scanner sStr = new Scanner(System.in);

		//値取得
		String str = sStr.next();
		int number = sint.nextInt();

		sint.close();
		sStr.close();

		StringBuilder strSum = new StringBuilder();

		for(int i = 0; i < number; i++) {
			strSum.append(str);
		}

		String stringSum = strSum.toString();

		String[] strs = stringSum.split("");

		int var = 0;
		for(int i =0; i < strs.length -1 ; i++) {
			if(strs[i].equals(strs[i+1])) {
				strs[i+1]= "AA";
				var++;
			}
		}
		System.out.println(var);

	}

}


import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String S = sc.next();
        int K = sc.nextInt();

        //先頭の文字格納
        char fstStr = S.charAt(0);
        //末尾の文字格納
        char endStr = S.charAt(S.length()-1);

        //最後の文字が変わらない時true
        Boolean endFlag = true;

        //全て同じ文字の時True
        Boolean sameFlag = true;

        int count = 0;
        int result = 0;

        //比較
        for(int i=1;i<S.length();i++) {
            //もし直前の文字と同じなら
            if(S.charAt(i) == S.charAt(i-1)){
                count++;
                //もし最後の文字が変更されたら
                if(i == S.length()-1){
                    endFlag = false;
                }
                //一つ飛ばし
                i++;
            }
        }

        //先頭から同じ文字が何文字続くか
        int strNum = 1;
        //文字列の中身の検査用
        for(int i=1; i<S.length();i++) {
            if(S.charAt(i) == S.charAt(i-1)){
                strNum++;
            } else {
                sameFlag = false; //全ておなじ文字列でない場合
                //同じでなくなった場合ループを抜ける
                break;
            }
        }

        //もし末尾が変更されず、かつ先頭と末尾文字が等しい場合
        if(endFlag == true && fstStr == endStr){
            //(1)Sが奇数かつ、全て同じ文字の場合
            if(S.length() % 2 != 0 && sameFlag == true){
                result = (count * K) + (K / 2);
            }
            //(2)先頭からの文字が同じ文字で偶数個の場合
            if(strNum % 2 == 0){
                result = count * K;
            } else {
                result = (count+1) * K -1;
            }//(3)先頭からの文字が同じ文字で奇数個の場合

        } else result = count * K;

        System.out.println(result);
    }
}


import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String S = sc.next();
		String S_2 = S + S;
		String S_3 = S + S + S;
		int N = Integer.parseInt(sc.next());
		char first = S.charAt(0);
		char before = first;
		long cnt_1 = 0L;
		for (int i=1;i<S.length();i++) {
			if (S.charAt(i)==before) {
				cnt_1++;
				i++;
			}
			if (i<=S.length()-1) {
				before=S.charAt(i);
			}
		}

		long cnt_2 = 0L;
		int flag=0;
		before = first;
		for (int i=1;i<S_2.length();i++) {
			if (S_2.charAt(i)==before) {
				cnt_2++;
				i++;
			}
			if (i<=S_2.length()-1) {
				before=S_2.charAt(i);
			}
			if (i==S_2.length()-1) {
				if (S_2.charAt(i)==first) {
					flag=1;
					if (S.charAt(0)==S.charAt(1) && S_2.charAt(i)==first) {
						flag=0;
					}
				}
			}
		}

		long cnt_3 = 0L;
		before = first;
		for (int i=1;i<S_3.length();i++) {
			if (S_3.charAt(i)==before) {
				cnt_3++;
				i++;
			}
			if (i<=S_3.length()-1) {
				before=S_3.charAt(i);
			}
		}

		long ans = 0L;
		if (N%2==0) {
			// ans = cnt_2*(N/2)+Math.max(flag*(N/2)-1, 0);
			// ans = (cnt_2-cnt_1) * (N-1) + cnt_1;
			ans = (cnt_3-cnt_1) * (N/2-1) + cnt_2;
		} else {
			// ans = cnt_2*(N/2)+cnt_1;
			// if (flag==1) {
				// ans+=flag*(N/2);
			// }
			// ans = (cnt_2-cnt_1) * (N-1) + cnt_1;
			ans = (cnt_3-cnt_1) * (N/2) + cnt_1;
		}
		System.out.println(ans);
		// System.out.println(cnt_1);
		// System.out.println(cnt_2);
		// System.out.println(flag);
	}
}


import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String S = sc.next();
		String S_2 = S + S;
		int N = Integer.parseInt(sc.next());
		char first = S.charAt(0);
		char before = first;
		long cnt_1 = 0L;
		for (int i=1;i<S.length();i++) {
			if (S.charAt(i)==before) {
				cnt_1++;
				i++;
			}
			if (i<=S.length()-1) {
				before=S.charAt(i);
			}
		}

		long cnt_2 = 0L;
		int flag=0;
		before = first;
		for (int i=1;i<S_2.length();i++) {
			if (S_2.charAt(i)==before) {
				cnt_2++;
				i++;
			}
			if (i<=S_2.length()-1) {
				before=S_2.charAt(i);
			}
			if (i==S_2.length()-1) {
				if (S_2.charAt(i)==first) {
					flag=1;
					if (S.charAt(0)==S.charAt(1) && S_2.charAt(i)==first) {
						flag=0;
					}
				}
			}
		}

		long ans = 0L;
		if (N%2==0) {
			// ans = cnt_2*(N/2)+Math.max(flag*(N/2)-1, 0);
			ans = (cnt_2-cnt_1) * (N-1) + cnt_1;
		} else {
			// ans = cnt_2*(N/2)+cnt_1;
			// if (flag==1) {
				// ans+=flag*(N/2);
			// }
			ans = (cnt_2-cnt_1) * (N-1) + cnt_1;
		}
		System.out.println(ans);
		// System.out.println(cnt_1);
		// System.out.println(cnt_2);
		// System.out.println(flag);
	}
}


import java.util.Scanner;
public class Main {
	public static void main(String[] args)throws Exception{
		Scanner stdIn=new Scanner(System.in);
		String S=stdIn.next();
		char A[]=new char[S.length()];
		long K=stdIn.nextInt();
		long ans=0;
		boolean fes=true;
		boolean key=false;
		for(int i=0;i<S.length();i++){
			A[i]=S.charAt(i);
			if(A[0]!=A[i])
				fes=false;
		}
		for(int i=0;i<S.length();i++){
			char a=A[i];
			char b;
			if(i!=S.length()-1)
				b=A[i+1];
			else{
				b=A[0];
				if(a==b)
					key=true;
			}
			if(a==b){
				if(i!=S.length()-1){
					A[i+1]=1;
				}
				ans++;
			}
		}
		for(int i=0;i<S.length();i++){
			A[i]=S.charAt(i);
		}
		if(key){
			if(S.length()>2&&A[1]!=A[2]&&A[0]==A[1]){
				ans--;
				ans*=K;

			}
			else{
				ans*=K;
				ans--;
			}
		}
		else
			ans*=K;
		if(fes)
			ans=(K*S.length())/2;
		System.out.println(ans);
	}
}


import java.util.*;

public class Main{
  public static void main(String[]args){
    Scanner sc = new Scanner(System.in);
    String S = sc.next();
    char[] C = S.toCharArray();
    long N = Long.parseLong(sc.next());
    int s = S.length();
    int count = 0;
    for(int i = 1; i < s; i++){
      if(C[i-1] == C[i]){
        C[i] = '-';
        count++;
      }
    }
    boolean f = false;
    if(C[0] == C[s-1]){
      count++;
      f = true;
    }
    long ans = 0;
    if(count == s-1 && s > 2){
      if(N%2==0){
        ans = count*(N/2) + (count-1)*(N/2);
      }else{
        ans = count*(N/2) + (count-1)*((N/2)+1);
      }
    }else{
      ans = count * N;
      if(f){
        ans--;
      }
    }

    if(s == 1){
      ans = N/2;
    }
    System.out.println(ans);
  }
}


import java.io.*;
import java.util.*;
public class Main{

    static class BI{
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream dis;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        //********Constructors*********\\

        public BI() {
            dis = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }
        public BI(String file_name) throws IOException {
            dis = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        //*******Reads a line until new line character************\\

        public String readLine() throws IOException {
            byte[] buf = new byte[1<<25];
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n') break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        //*******Reads a string Until First Space************\\

        public String nextString() throws IOException {
            // Skip all whitespace characters from the stream
            byte c = read();
            while(Character.isWhitespace(c)){
                c = read();
            }
            StringBuilder builder = new StringBuilder();
            builder.append((char)c);
            c = read();
            while(!Character.isWhitespace(c)){
                builder.append((char)c);
                c = read();
            }
            return builder.toString();
        }

        //*******Reads a Integer************\\

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        //*******Reads a Integer************\\

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        //*******Reads a Char************\\

        public char nextChar() throws IOException{
            byte c = read();
            while(Character.isWhitespace(c)){
                c = read();
            }
            return (char) c;
        }

        //*******Reads a Double************\\

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }
            if (neg)
			return -ret;
			return ret;
        }

        //*******Reads an array of double values************\\

        public double[] nextDoubleArray(int n) throws IOException {
            double arr[] = new double[n];
            for(int i = 0; i < n; i++){
                arr[i] = nextDouble();
            }
            return arr;
        }

        //*******Reads an array of long integers************\\

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

        //*******Reads an array of Integers************\\

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

        //*******Fills the buffer from input stream************\\

        private void fillBuffer() throws IOException {
            bytesRead = dis.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
            buffer[0] = -1;
        }
        private byte read() throws IOException {
            if (bufferPointer == bytesRead)
            fillBuffer();
            return buffer[bufferPointer++];
        }
        public void close() throws IOException {
            if (dis == null) return;
            dis.close();
        }

    }

    //**********************CODE IS HERE*****************************\\

    static long gcd(long a, long b){ return (b==0) ? a : gcd(b, a%b); }
    static int gcd(int a, int b){ return (b==0) ? a : gcd(b, a%b); }
    public static int rev(int a)
    {
        int tmp=0;
        while(a>0)
        {
            tmp = (tmp*10)+(a%10);
            a=a/10;
        }
        return tmp;
    }
    public static void main(String args[])throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));//bw.write("Shubham Pandey\n");

        BI in=new BI();
        // int a=in.nextInt();
        // long b=in.nextLong();
        // char c=in.nextChar();
        // double d=in.nextDouble();
        // int arr[]=in.nextIntArray(3);
        // double drr[]=in.nextDoubleArray(6);
        // long lrr[]=in.nextLongArray(4);
        // String str=in.readLine();//line
        // String ptr=in.nextString();//word
        int big = (int)1e7;
        String str = in.readLine();
        int k = in.nextInt();
        int n = str.length();
        if(n==1){ int tp = k/2; bw.write(tp+"");}
        else
        {
            char ch[] = str.toCharArray();
            long count=0l;
            if(k>1)
            {
                for(int i=0;i<n;i++)
                {
                    if(ch[i]==ch[(i+1)%n]) {ch[(i+1)%n] = '^';count++;}
                }
            }
            else
            {
                for(int i=0;i<n-1;i++)
                {
                    if(ch[i]==ch[i+1]) {ch[i+1] = '^';count++;}
                }
            }
            bw.write(count*k*1L+"");
        }

        bw.flush();
    }
}


import java.util.*;

public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		String S = sc.next();
		long K = sc.nextLong();
		int count = 0;

		char c = ' ';
		//if (S.charAt(0) != S.charAt(S.length()-1)) {
			for (int i = 0; i < S.length(); i++) {
				if (S.charAt(i) == c) {
					count++;
					i++;
				}
				if (i >= S.length())
					break;
				c = S.charAt(i);
			}
			long ans = count * K;
			System.out.println(K*count);
			//}
	}
}


import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    List<Long> a = new ArrayList<>();
    for (int i = 0; i < n; i++) {
      a.add(sc.nextLong());
    }
    if (n == 1) {
      long p = a.get(0);
      for (int i = 0; i < m; i++) {
        p = p / 2;
      }
      System.out.println(p);
      return;
    }
    a.sort(Comparator.reverseOrder());
    for (int i = 0; i < m; i++) {
      Long aLong = a.get(0);
      long newLong = aLong / 2;
      a.set(0, newLong);
      if (newLong < a.get(1)) {
        a.sort(Comparator.reverseOrder());
      }
    }
    System.out.println(a.stream().reduce(0L, Long::sum));
  }
}

Score : 400 points

Problem Statement

Takahashi is going to buy N items one by one.

The price of the i-th item he buys is A_i yen (the currency of Japan).

He has M discount tickets, and he can use any number of them when buying an item.

If Y tickets are used when buying an item priced X yen, he can get the item for \frac{X}{2^Y} (rounded down to the nearest integer) yen.

What is the minimum amount of money required to buy all the items?

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the minimum amount of money required to buy all the items.

Sample Input 1

3 3
2 13 8

Sample Output 1

9

We can buy all the items for 9 yen, as follows:

Buy the 1-st item for 2 yen without tickets.

Buy the 2-nd item for 3 yen with 2 tickets.

Buy the 3-rd item for 4 yen with 1 ticket.

Sample Input 2

4 4
1 9 3 5

Sample Output 2

6

Sample Input 3

1 100000
1000000000

Sample Output 3

0

We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.

Sample Input 4

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 4

9500000000


//package com.company;
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main{
    public static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private 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 String nextLine() {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            String str = "";
            try {
                str = br.readLine();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
        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 long nextLong() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            while (!isSpaceChar(c));
            return res * sgn;
        }
        public double nextDouble() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E')
                    return res * Math.pow(10, nextInt());
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c))
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, nextInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }
        public char nextChar() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            return (char)c;
        }
        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 boolean isSpaceChar(int c) {
            if (filter != null)
                return filter.isSpaceChar(c);
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);
        }
    }
    static int gcd(int a, int b){
        if (a == 0)
            return b;
        return gcd(b % a, a);
    }
    static int lcm(int a, int b)  {
        return (a*b)/gcd(a, b);
    }
    public static void main(String args[]){
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter w = new PrintWriter(outputStream);
        int n,m,q,i,j;
      long sum=0;
      n=in.nextInt();
      m=in.nextInt();
      long arr[]=new long[n];
      PriorityQueue<Long> pq=new PriorityQueue<>(Collections.reverseOrder());
      for(i=0;i<n;i++){
        arr[i]=in.nextLong();
        pq.add(arr[i]);
      }
      if(n==1){
        arr[0]=arr[0]/(int)Math.pow(2,m);
        w.println(arr[0]);
        w.close();
        return;
      }
      else{

        while(m>0&&arr[n-2]>0){

          long x=pq.poll();
          long y=pq.poll();
          if(x==y){
            m--;
            x=x/2;
            //continue;
          }else{
            int temp=(int)(Math.log((int)Math.ceil((double)x/y))/(double)(Math.log(2)));
            m-=temp;
            x=x/(long)Math.pow(2,temp);
            //w.println(arr[n-1]);
          }
          pq.add(x);
          pq.add(y);
        }
      }
      while(!pq.isEmpty())
      {
        sum+=pq.poll();
      }
      w.println(sum);
        w.close();
    }

}


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

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}

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

    static FastScanner scan = new FastScanner();
    static final int MOD = 1_000_000_007;
    static final int INF = 2147483647;
    static final long LINF = 9223372036854775807L;

    // Scanner
    int ni(){return scan.nextInt();}
    int[] ni(int n){int[] a = new int[n]; for(int i = 0; i < n; i++){a[i] = ni();} return a;}
    int[][] ni(int y, int x){int[][] a = new int[y][x];
        for(int i = 0; i < y; i++){for(int j = 0; j < x; j++){a[i][j] = ni();}} return a;}
    long nl(){return scan.nextLong();}
    long[] nl(int n){long[] a = new long[n]; for(int i = 0; i < n; i++){a[i] = nl();} return a;}
    long[][] nl(int y, int x){long[][] a = new long[y][x];
        for(int i = 0; i < y; i++){for(int j = 0; j < x; j++){a[i][j] = nl();}} return a;}
    String ns(){return scan.next();}
    String[] ns(int n){String[] a = new String[n]; for(int i = 0; i < n; i++){a[i] = ns();} return a;}
    String[][] ns(int y, int x){String[][] a = new String[y][x];
        for(int i = 0; i < y; i++){for(int j = 0; j < x; j++){a[i][j] = ns();}} return a;}

    // Math
    int max(int a, int b){return Math.max(a, b);}
    long max(long a, long b){return Math.max(a, b);}
    double max(double a, double b){return Math.max(a, b);}
    int max(int[] a){int max = a[0]; for(int value:a){max = max(max,value);} return max;}
    long max(long[] a){long max = a[0]; for(long value:a){max = max(max,value);} return max;}
    double max(double[] a){double max = a[0]; for(double value:a){max = max(max,value);} return max;}
    int min(int a, int b){return Math.min(a, b);}
    long min(long a, long b){return Math.min(a, b);}
    double min(double a, double b){return Math.min(a, b);}
    int min(int[] a){int min = a[0]; for(int value:a){min = min(min,value);} return min;}
    long min(long[] a){long min = a[0]; for(long value:a){min = min(min,value);} return min;}
    double min(double[] a){double min = a[0]; for(double value:a){min = min(min,value);} return min;}
    long sum(int[] a){long sum = 0; for(int value:a){sum += value;} return sum;}
    long sum(long[] a){long sum = 0; for(long value:a){sum += value;} return sum;}
    double sum(double[] a){double sum = 0; for(double value:a){sum += value;} return sum;}
    int gcd(int a, int b){return b == 0 ? a : gcd(b, a % b);}
    long gcd(long a, long b){return b == 0 ? a : gcd(b, a % b);}
    int lcm(int a, int b){return a / gcd(a, b) * b;}
    long lcm(long a, long b){return a / gcd(a, b) * b;}

    // Array
    void sort(int[] a){ Arrays.sort(a);}
    void sort(long[] a){ Arrays.sort(a);}
    void sort(double[] a){ Arrays.sort(a);}
    void sort(String[] a){ Arrays.sort(a);}
    int[] reverse(int[] a){
        int[] reversed = new int[a.length];
        for(int i = 0; i < a.length; i++){
            reversed[a.length - i - 1] = a[i];
        }
        return reversed;
    }
    long[] reverse(long[] a){
        long[] reversed = new long[a.length];
        for(int i = 0; i < a.length; i++){
            reversed[a.length - i - 1] = a[i];
        }
        return reversed;
    }
    double[] reverse(double[] a){
        double[] reversed = new double[a.length];
        for(int i = 0; i < a.length; i++){
            reversed[a.length - i - 1] = a[i];
        }
        return reversed;
    }
    String[] reverse(String[] a){
        String[] reversed = new String[a.length];
        for(int i = 0; i < a.length; i++){
            reversed[a.length - i - 1] = a[i];
        }
        return reversed;
    }
    boolean[] reverse(boolean[] a){
        boolean[] reversed = new boolean[a.length];
        for(int i = 0; i < a.length; i++){
            reversed[a.length - i - 1] = a[i];
        }
        return reversed;
    }
    void fill(int[] array, int x) { Arrays.fill(array, x); }
    void fill(long[] array, long x) { Arrays.fill(array, x); }
    void fill(double[] array, double x) { Arrays.fill(array, x); }
    void fill(boolean[] array, boolean x) { Arrays.fill(array, x); }
    void fill(int[][] array, int x) { for(int a[] : array) { fill(a, x); } }
    void fill(long[][] array, long x) { for(long a[] : array) { fill(a, x); } }
    void fill(double[][] array, double x) { for(double a[] : array) { fill(a, x); } }
    void fill(boolean[][] array, boolean x) { for(boolean a[] : array) { fill(a, x); } }

    public void solve(){
        int n = ni();
        int m = ni();
        long[] a = nl(n);

        while(m > 0){
            sort(a);
            a[n-1] /= 2;
            m--;
        }

        System.out.println(sum(a));

    }
}


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

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] stdR1 = br.readLine().split(" ");
        int N = Integer.parseInt(stdR1[0]);
        int M = Integer.parseInt(stdR1[1]);

        String[] stdR2 = br.readLine().split(" ");
        PowerfulDiscountTickets pdt = new PowerfulDiscountTickets(N, M);

        for(int i = 0; i < N; i++){
            pdt.goods[i] = Integer.parseInt(stdR2[i]);
        }

        System.out.println(pdt.binarySearch());
    }
}

class PowerfulDiscountTickets{
    int N;
    int M;
    int[] goods;

    public PowerfulDiscountTickets(int N, int M){
        this.N = N;
        this.M = M;
        goods = new int[N];
    }

    public long binarySearch(){
        long left = -1;
        long right = 100_000_000_000_000L;

        while(right - left > 1){
            long mid = (left + right) / 2;
            if(isOK(mid)){
                right = mid;
            }else{
                left = mid;
            }
        }

        return right;
    }

    private boolean isOK(long price){

        int[] goods2 = goods.clone();
        Arrays.sort(goods2);

        long sum = 0;
        for(int i = 0; i < N; i++){
            sum += goods2[i];
        }

        for(int i = 0; i < M; i++){
            int before = goods2[N-1];
            goods2[N-1] /= 2;
            sum = sum - before + goods2[N-1];
            if( sum <= price ) break;
            Arrays.sort(goods2);
        }

        return sum <= price;
    }

}