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 nnn monsters. The i-th of them appears ki​ seconds after the start of the level and has hi health points. As an additional constraint, hi​≤ki​ for all 1≤i≤n. All ki 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,… The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is 1. Otherwise, let the damage at the previous second be x. Then he can choose the damage to be either x+1 or 1. A spell uses mana: casting a spell with damage x uses x mana. Mana doesn't regenerate.

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

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 ttt (1≤t≤10^4) — the number of testcases.

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

The second line of the testcase contains nnn integers k1​<k2​<⋯<kn​ (1≤ki≤10^9) — the number of second from the start the i-th monster appears at. All ki​ are different, ki​ are provided in the increasing order.

The third line of the testcase contains nnn integers h1,h2,…,hn (1≤hi≤ki≤10^9) — the health of the i-th monster.

The sum of n over all testcases doesn't exceed 10^4.

Output

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

Sample 1

InputOutput
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,5 and 6 seconds from the start with damages 1,2,3 and 4, respectively. The damage dealt at 6 seconds is 4, 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,4 and 5 seconds from the start with damages 1,2 and 3, respectively.

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

思路:

题目大概意思就是在第k秒使h健康值的怪兽一击毙命。

就是在[k-h+1,k]区间内等差数列求和。

问题在于区间可能会重合。

处理方式在分情况讨论,如果重合则向前调,前重合之后必定是随前面一个区间走(从何往前看:这个处理方法比较独特,思维不要定式)

特别注意,不要为了偷懒使输入跟处理走。

#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
const ll N=1e4;
ll t,ans,m,n;
ll k[N+5],h[N+5];
ll max(ll a,ll b)
{
	return a>b?a:b;
}
ll sum(ll a,ll b)
{
	return (ll)(a+b)*(b-a+1)/2;
}
int main()
{
	scanf("%lld",&t);
	while(t--)
	{
		ans=0;
		scanf("%lld",&n);
		for(int i=1;i<=n;i++)
			cin>>k[i];	
		for(int i=1;i<=n;i++)
			cin>>h[i];
		k[0]=0,m=k[n];
		for(int i=n;i!=0;i--)
		{
			if(k[i]-k[i-1]>=h[i])
			{
				ans+=sum(1,m-k[i]+h[i]);
				m=k[i-1];
			}
			else
			{
				h[i-1]=max(h[i-1],h[i]-(k[i]-k[i-1]));
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TherAndI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值