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 kiki seconds after the start of the level and has hihi health points. As an additional constraint, hi≤kihi≤ki for all 1≤i≤n1≤i≤n. All kiki 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,…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 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 hihi at the exact moment the monster appears, which is kiki.

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≤t≤1041≤t≤104) — the number of testcases.

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

The second line of the testcase contains nn integers k1<k2<⋯<knk1<k2<⋯<kn (1≤ki≤1091≤ki≤109) — the number of second from the start the ii-th monster appears at. All kiki are different, kiki are provided in the increasing order.

The third line of the testcase contains nn integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤ki≤1091≤hi≤ki≤109) — the health of the ii-th monster.

The sum of nn over all testcases doesn't exceed 104104.

Output

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

Input

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

Out:

10
6
7

题意:有n歌怪物,他们同时出现,但是每个人出现持续的时间是t,我们要在他消失之前打败他。怎么打败呢?怪物有血量h,我们可以的伤害是逐秒递增加1,消耗法力和伤害一样,如果停止进攻,下次进攻从伤害1开始。我们要求把所有怪物打败的最小的法力消耗。

题解:首先可以肯定的是,为了达到最小的目的。我们最好就是在怪物消失的时候,能够刚好将怪物消灭。但有时间隔会小于上一个怪物的血量(即上次打败怪物时的伤害),这时就不能从1开始,要继续累加伤害直至把怪物消灭。

这样的的思路解法是,若当前和上一个怪物消失的间隔大于当前怪物生命值,直接加上1到h[i]的前n项和,不然则要从上次的伤害pre开始,一直加到能够打败怪物的伤害h[i],为止,也就是加上pre + 1到pre + s[i] - s[i+1]的前n项和。

上面的思路是理想状态,因为事实上可能当前的怪物的血量大于上一个怪物血量加上二者之间的时间间隔。因此,我们需要从后到前遍历更新h[i]。

这时h[i]就不再是血量了,是到达这个怪物消失的时间s[i],此时要达到的伤害值。我们让h[i]取原本血量(h[i])和下一个怪物应该达到伤害减去二者时间间隔 ( h[i+1] - s[k+1) - s[k] 的较大值。

之后在顺序和之前思路一样解即可。

代码:

#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、付费专栏及课程。

余额充值