CF1501A.Alexey and Train

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Alexey is travelling on a train. Unfortunately, due to the bad weather, the train moves slower that it should!

Alexey took the train at the railroad terminal. Let's say that the train starts from the terminal at the moment 00 . Also, let's say that the train will visit nn stations numbered from 11 to nn along its way, and that Alexey destination is the station nn .

Alexey learned from the train schedule nn integer pairs (ai,bi)(a_i, b_i) where aia_i is the expected time of train's arrival at the ii -th station and bib_i is the expected time of departure.

Also, using all information he has, Alexey was able to calculate nn integers tm1,tm2,,tmntm_1, tm_2, \dots, tm_n where tmitm_i is the extra time the train need to travel from the station i1i - 1 to the station ii . Formally, the train needs exactly aibi1+tmia_i - b_{i-1} + tm_i time to travel from station i1i - 1 to station ii (if i=1i = 1 then b0b_0 is the moment the train leave the terminal, and it's equal to 00 ).

The train leaves the station ii , if both conditions are met:

  1. it's on the station for at least biai2\left\lceil \frac{b_i - a_i}{2} \right\rceil units of time (division with ceiling);
  2. current time bi\ge b_i .

Since Alexey spent all his energy on prediction of time delays, help him to calculate the time of arrival at the station nn .

输入格式

The first line contains one integer tt ( 1t1001 \le t \le 100 ) — the number of test cases.

The first line of each test case contains the single integer nn ( 1n1001 \le n \le 100 ) — the number of stations.

Next nn lines contain two integers each: aia_i and bib_i ( 1ai<bi1061 \le a_i < b_i \le 10^6 ). It's guaranteed that bi<ai+1b_i < a_{i+1} .

Next line contains nn integers tm1,tm2,,tmntm_1, tm_2, \dots, tm_n ( 0tmi1060 \le tm_i \le 10^6 ).

输出格式

For each test case, print one integer — the time of Alexey's arrival at the last station.

输入输出样例

  • 输入#1

    2
    2
    2 4
    10 12
    0 2
    5
    1 4
    7 8
    9 10
    13 15
    19 20
    1 2 3 4 5

    输出#1

    12
    32

说明/提示

In the first test case, Alexey arrives at station 11 without any delay at the moment a1=2a_1 = 2 (since tm1=0tm_1 = 0 ). After that, he departs at moment b1=4b_1 = 4 . Finally, he arrives at station 22 with tm2=2tm_2 = 2 extra time, or at the moment 1212 .

In the second test case, Alexey arrives at the first station with tm1=1tm_1 = 1 extra time, or at moment 22 . The train, from one side, should stay at the station at least b1a12=2\left\lceil \frac{b_1 - a_1}{2} \right\rceil = 2 units of time and from the other side should depart not earlier than at moment b1=4b_1 = 4 . As a result, the trains departs right at the moment 44 .

Using the same logic, we can figure out that the train arrives at the second station at the moment 99 and departs at the moment 1010 ; at the third station: arrives at 1414 and departs at 1515 ; at the fourth: arrives at 2222 and departs at 2323 . And, finally, arrives at the fifth station at 3232 .

首页