3103: Journey

时间限制:2000 ms 内存限制:256 MB
上传者:
提交:18 通过:5

题目描述

There are $n$ city labels $1\sim n$ on a tourist route, and each city has different scenery, so each city has a happiness value W. When a tourist finishes visiting the city, he will get the happiness of W. Obviously, the initial happiness value of tourists in each city is equal to the happiness value of the city where they are located.

In order to ensure the order of the tour, there are now such travel regulations:
  • Tourists can only visit cities with a label greater than their own city. If the cities are arranged from left to right by label, then a tourist can only go to the city on his right.
  • Each city cannot accommodate too many people due to its limited capacity. Therefore, in order to limit the number of tourists, only tourists who have been to a city whose label is not greater than the travel limit distance L can travel to the city.
The travel planning staff would like to know what is the greatest happiness among the tourists who have visited the last city marked. Please help calculate it.

输入格式

The first line contains a single integer T$(1\leq T\leq 10)$ — the number of samples.

The first line of each test case contains a single integer n $(1 \le n\le 10^5)$ — the number of cities.

Next line contains n integer $ w_{1},w_{2},...,w_{n} (w_{i} \le 10^{9}) $   — Happiness-value of each city.

Next line contains n integer $ l_{1},l_{2},...,l_{n} (l_{i} \le n)$ — the travel limit distance of each city.

输出格式

For each test case, print a single number  — the greatest happiness among the tourists who have visited the last city marked.

输入样例 复制

1
5
1 2 2 -1 1
2 1 2 2 3

输出样例 复制

6