CF1716C.Robot in a Hallway
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
There is a grid, consisting of 2 rows and m columns. The rows are numbered from 1 to 2 from top to bottom. The columns are numbered from 1 to m from left to right.
The robot starts in a cell (1,1) . In one second, it can perform either of two actions:
- move into a cell adjacent by a side: up, right, down or left;
- remain in the same cell.
The robot is not allowed to move outside the grid.
Initially, all cells, except for the cell (1,1) , are locked. Each cell (i,j) contains a value ai,j — the moment that this cell gets unlocked. The robot can only move into a cell (i,j) if at least ai,j seconds have passed before the move.
The robot should visit all cells without entering any cell twice or more (cell (1,1) is considered entered at the start). It can finish in any cell.
What is the fastest the robot can achieve that?
输入格式
The first line contains a single integer t ( 1≤t≤104 ) — the number of testcases.
The first line of each testcase contains a single integer m ( 2≤m≤2⋅105 ) — the number of columns of the grid.
The i -th of the next 2 lines contains m integers ai,1,ai,2,…,ai,m ( 0≤ai,j≤109 ) — the moment of time each cell gets unlocked. a1,1=0 . If ai,j=0 , then cell (i,j) is unlocked from the start.
The sum of m over all testcases doesn't exceed 2⋅105 .
输出格式
For each testcase, print a single integer — the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.
输入输出样例
输入#1
4 3 0 0 1 4 3 2 5 0 4 8 12 16 2 6 10 14 18 4 0 10 10 10 10 10 10 10 2 0 0 0 0
输出#1
5 19 17 3