Farmer John Actually Farms

시간 제한2초메모리 제한1024 MB

요약
i번째 식물의 최종 키가 정확히 t_i개의 다른 식물보다 작도록 만드는 최소 일수 t를 구하거나, 그러한 t가 없으면 -1을 출력한다.
난이도

어려움10점 중 8점

유형
정렬, 그리디, 수학, 이분 탐색
정답자
아직 제출이 없습니다

문제

Farmer John is growing NN (1≤N≤2⋅1051 \leq N \leq 2\cdot 10^5) plants of asparagus on his farm! However some of his plants have genetic differences, so some plants will grow faster than others. The initial height of the iith plant is h_ih\_i inches, and after each day, the iith plant grows by a_ia\_i inches.

FJ likes some of his plants more than others, and he wants some specific plants to be taller than others. He gives you an array of distinct values t_1,…,t_Nt\_1,\dots,t\_N containing all integers from 00 to N−1N-1 and he wants the iith plant to have exactly t_it\_i other plants that are taller than it. Find the minimum number of days so that FJ's request is satisfied, or determine that it is impossible.

입력

The first will consist of an integer TT, denoting the number of independent test cases (1≤T≤10)(1 \leq T \leq 10).

The first line of each test case consists of an integer NN.

The second line consists of NN integers h_ih\_i (1≤h_i≤109)(1 \leq h\_i \leq 10^9) denoting the initial height of the iith plant in inches.

The third line consists of NN integers a_ia\_i (1≤a_i≤109)(1 \leq a\_i \leq 10^9) denoting the number of inches the iith plant grows each day.

The fourth line consists of NN distinct integers t_it\_i denoting the array that FJ gives you.

It is guaranteed that the sum of NN over all test cases does not exceed 2⋅1052\cdot 10^5.

출력

Output TT lines, the answer to each test case on a different line. If it is not possible, output −1-1.

Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

예제2

  1. 예제 1

    입력
    6
    1
    10
    1
    0
    2
    7 3
    8 10
    1 0
    2
    3 6
    10 8
    0 1
    2
    7 3
    8 9
    1 0
    2
    7 7
    8 8
    0 1
    2
    7 3
    8 8
    1 0
    
    예상 출력
    0
    3
    2
    5
    -1
    -1
    
  2. 예제 2

    입력
    2
    5
    7 4 1 10 12
    3 4 5 2 1
    2 1 0 3 4
    5
    4 10 12 7 1
    3 1 1 4 5
    2 4 3 1 0
    
    예상 출력
    4
    7