PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000190. Tổng cặp phần tử nhỏ thứ K (K-th Sum)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng cặp phần tử nhỏ thứ K (K-th Sum)

K-th Sum

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given two integer arrays a1,a2,…,ana_1,a_2,\ldots,a_n and b1,b2,…,bnb_1,b_2,\ldots,b_n.

For each ordered index pair (i,j)(i,j) satisfying 1≤i,j≤n1\le i,j\le n, form the sum ai+bja_i+b_j. There are exactly n2n^2 sums. Different index pairs count separately, even when their sums are equal.

Sort all these sums in nondecreasing order and find the value at position kk, with positions starting at 1.

Input

  • The first line contains integers nn and kk.
  • The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.
  • The third line contains nn integers b1,b2,…,bnb_1,b_2,\ldots,b_n.

Output

  • Print one integer: the kk-th smallest pair sum, counting duplicates.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; −109≤ai,bi≤109-10^9\le a_i,b_i\le 10^9 for every 1≤i≤n1\le i\le n; 1≤k≤n21\le k\le n^2.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; −109≤ai,bi≤109-10^9\le a_i,b_i\le 10^9 for every 1≤i≤n1\le i\le n; 1≤k≤n21\le k\le n^2.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; −109≤ai,bi≤109-10^9\le a_i,b_i\le 10^9 for every 1≤i≤n1\le i\le n; 1≤k≤n21\le k\le n^2.

Examples

Input

3 5
1 4 6
2 3 5

Output

7

Explanation

For a=[1,4,6]a=[1,4,6] and b=[2,3,5]b=[2,3,5], the sums by row are:

  • From a1=1a_1=1: 3,4,63,4,6.
  • From a2=4a_2=4: 6,7,96,7,9.
  • From a3=6a_3=6: 8,9,118,9,11.

After sorting all nine sums, we get [3,4,6,6,7,8,9,9,11][3,4,6,6,7,8,9,9,11]. Its fifth element is 77.