D - Monsters And Spells

Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters.

The level he's currently on contains nn monsters. The ii-th of them appears k_iki​ seconds after the start of the level and has h_ihi​ health points. As an additional constraint, h_i \le k_ihi​≤ki​ for all 1 \le i \le n1≤i≤n. All k_iki​ are different.

Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: 1, 2, 3, \dots1,2,3,… The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is 11. Otherwise, let the damage at the previous second be xx. Then he can choose the damage to be either x + 1x+1 or 11. A spell uses mana: casting a spell with damage xx uses xx mana. Mana doesn't regenerate.

To kill the ii-th monster, Monocarp has to cast a spell with damage at least h_ihi​ at the exact moment the monster appears, which is k_iki​.

Note that Monocarp can cast the spell even when there is no monster at the current second.

The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters.

It can be shown that it's always possible to kill all monsters under the constraints of the problem.

Input

The first line contains a single integer tt (1 \le t \le 10^41≤t≤104) — the number of testcases.

The first line of the testcase contains a single integer nn (1 \le n \le 1001≤n≤100) — the number of monsters in the level.

The second line of the testcase contains nn integers k_1 < k_2 < \dots < k_nk1​<k2​<⋯<kn​ (1 \le k_i \le 10^91≤ki​≤109) — the number of second from the start the ii-th monster appears at. All k_iki​ are different, k_iki​ are provided in the increasing order.

The third line of the testcase contains nn integers h_1, h_2, \dots, h_nh1​,h2​,…,hn​ (1 \le h_i \le k_i \le 10^91≤hi​≤ki​≤109) — the health of the ii-th monster.

The sum of nn over all testcases doesn't exceed 10^4104.

Output

For each testcase, print a single integer — the least amount of mana required for Monocarp to kill all monsters.

Sample 1

InputcopyOutputcopy
3
1
6
4
2
4 5
2 2
3
5 7 9
2 1 2
10
6
7

Note

In the first testcase of the example, Monocarp can cast spells 3, 4, 53,4,5 and 66 seconds from the start with damages 1, 2, 31,2,3 and 44, respectively. The damage dealt at 66 seconds is 44, which is indeed greater than or equal to the health of the monster that appears.

In the second testcase of the example, Monocarp can cast spells 3, 43,4 and 55 seconds from the start with damages 1, 21,2 and 33, respectively.

In the third testcase of the example, Monocarp can cast spells 4, 5, 7, 84,5,7,8 and 99 seconds from the start with damages 1, 2, 1, 11,2,1,1 and 22, respectively.

#include <bits/stdc++.h>
#define int long long 
using namespace std;
const int N = 1e2 + 10;
int s[N],h[N];
int find(int a1,int a2)
{
	return (a1 + a2) * (a2 - a1 + 1)/2;
}
void solve()
{
	int n; cin >> n;
	for(int i = 1;i <= n;i++) 
	    cin >> s[i];
	for(int i = 1;i <= n;i++) 
	    cin >> h[i];
	for(int i = n-1;i >= 1;i--) 
	    h[i] = max(h[i],h[i+1] - (s[i+1] - s[i]));
	int ans = 0,pre = 0;
	for(int i = 1;i <= n;i++)
	{
		if(s[i] - s[i-1] >= h[i])
		{
			ans += find(1,h[i]);
			pre = h[i];
		}
		else if(pre + s[i] -s[i-1] >= h[i])
		{
			ans += find(pre+1,pre+s[i] - s[i-1]);
			pre += s[i] - s[i-1];
		}
	}
	cout << ans << endl;
}
signed main()
{
	int t; 
	cin >> t;
	while(t--)
	{
		solve();
	}
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值