F - Invisible Hand Editorial by evima
Supplement to Official EditorialIf you further organize the alternative solution in the official editorial, you will find that the answer is the \(M\)-th value when the \(N + M\) values \(A_1, \dots, A_N, B_1 + 1, \dots, B_M + 1\) are sorted in ascending order. (Below, we use the symbols in the official commentary.) This is because \(f(x) - g(x) = -M\) for \(x = 0\), and as \(x\) increases, \(f(x) - g(x)\) increases by \(1\) at \(x = A_1, \dots, A_N, B_1 + 1, \dots, B_M + 1\).
Sample Implementation (Python)
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
print(sorted(A + list(map(lambda x: x + 1, B)))[M - 1])
posted:
last update: