CF1260(A,B,C,D,E)

A. Heating

Several days ago you bought a new house and now you are planning to start a renovation. Since winters in your region can be very cold you need to decide how to heat rooms in your house.

Your house has n rooms. In the i-th room you can install at most ci heating radiators. Each radiator can have several sections, but the cost of the radiator with k sections is equal to k2 burles.

Since rooms can have different sizes, you calculated that you need at least sumi sections in total in the i-th room.

For each room calculate the minimum cost to install at most ci radiators with total number of sections not less than sumi.

Input

The first line contains single integer n (1≤n≤1000) — the number of rooms.

Each of the next n lines contains the description of some room. The i-th line contains two integers ci and sumi (1≤ci,sumi≤104) — the maximum number of radiators and the minimum total number of sections in the i-th room, respectively.

Output

For each room print one integer — the minimum possible cost to install at most ci radiators with total number of sections not less than sumi.

Example

input

4
1 10000
10000 1
2 6
4 6

output

100000000
1
18
10

Note

In the first room, you can install only one radiator, so it’s optimal to use the radiator with sum1 sections. The cost of the radiator is equal to (104)2=108.

In the second room, you can install up to 104 radiators, but since you need only one section in total, it’s optimal to buy one radiator with one section.

In the third room, there 7 variants to install radiators: [6,0], [5,1], [4,2], [3,3], [2,4], [1,5], [0,6]. The optimal variant is [3,3] and it costs 32+32=18.

分析:

题意:给c个面积,组成的面积不能小于sum,价格为每块面积的平方相加,求最小花费。
贪心。

代码:

#include<bits/stdc++.h>
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n;
	cin>>n;
	while(n--)
	{
		int a,b,res;
		cin>>a>>b;
		int c,d;
		c=b/a;
		d=b%a;
		res=(a-d)*c*c+d*(c+1)*(c+1);
		cout<<res<<endl;
	}
	return 0;
}

B. Obtain Two Zeroes

You are given two integers a and b. You may perform any number of operations on them (possibly zero).

During each operation you should choose any positive integer x and set a:=a−x, b:=b−2x or a:=a−2x, b:=b−x. Note that you may choose different values of x in different operations.

Is it possible to make a and b equal to 0 simultaneously?

Your program should answer t independent test cases.

Input

The first line contains one integer t (1≤t≤100) — the number of test cases.

Then the test cases follow, each test case is represented by one line containing two integers a and b for this test case (0≤a,b≤109).

Output

For each test case print the answer to it — YES if it is possible to make a and b equal to 0 simultaneously, and NO otherwise.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

Example

input

3
6 9
1 1
1 2

output

YES
NO
YES

Note

In the first test case of the example two operations can be used to make both a and b equal to zero:

1、choose x=4 and set a:=a−x, b:=b−2x. Then a=6−4=2, b=9−8=1;
2、choose x=1 and set a:=a−2x, b:=b−x. Then a=2−2=0, b=1−1=0.

分析:

题意:问存不存在x,y∈Z+,使2x+y=a,x+2y=b。
简单的数学问题。

代码:

#include<bits/stdc++.h>
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n;
	cin>>n;
	while(n--)
	{
		int a,b;
		cin>>a>>b;
		long long k=2*b-a;
		if(k%3==0 && k/3>=0 && (a-k/3)>=0 && (b-k/3)>=0)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}

C. Infinite Fence

You are a rebel leader and you are planning to start a revolution in your country. But the evil Government found out about your plans and set your punishment in the form of correctional labor.

You must paint a fence which consists of 10100 planks in two colors in the following way (suppose planks are numbered from left to right from 0):

if the index of the plank is divisible by r (such planks have indices 0, r, 2r and so on) then you must paint it red;
if the index of the plank is divisible by b (such planks have indices 0, b, 2b and so on) then you must paint it blue;
if the index is divisible both by r and b you can choose the color to paint the plank;
otherwise, you don’t need to paint the plank at all (and it is forbidden to spent paint on it).
Furthermore, the Government added one additional restriction to make your punishment worse. Let’s list all painted planks of the fence in ascending order: if there are k consecutive planks with the same color in this list, then the Government will state that you failed the labor and execute you immediately. If you don’t paint the fence according to the four aforementioned conditions, you will also be executed.

The question is: will you be able to accomplish the labor (the time is not important) or the execution is unavoidable and you need to escape at all costs.

Input

The first line contains single integer T (1≤T≤1000) — the number of test cases.

The next T lines contain descriptions of test cases — one per line. Each test case contains three integers r, b, k (1≤r,b≤109, 2≤k≤109) — the corresponding coefficients.

Output

Print T words — one per line. For each test case print REBEL (case insensitive) if the execution is unavoidable or OBEY (case insensitive) otherwise.

Example

input

4
1 1 2
2 10 4
5 2 3
3 2 2

output

OBEY
REBEL
OBEY
OBEY

分析:

题意:给10100个色块涂色。给出r和b的值,r的倍数涂红色,b的倍数涂蓝色,r和b的倍数自己选择颜色,讲涂色的色块按编号从小到大排号,要求不能有连续k个色块是同一个颜色。
为了同色连续的色块尽可能的少,对于r和d的倍数的色块应该选较大值的那一个颜色。然后推导一下:
ma=max(r,b);
mi=min(r,b);
我们可以看作题目是求在0~ma中有几个mi的倍数,即time=ma/mi;如果ma%mi=0的话就是mi*time=ma,按照上面的贪心策略,time=time-1;
g=__gcd(r,b);m=ma%mi;
如果mi/g*m>mi,即m>g时,就表示在0~mi/g*ma的区间中存在一个区间k*ma~(k+1)*ma中time=time+1;

代码:

#include<bits/stdc++.h>
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		int a,b,k;
		cin>>a>>b>>k;
		int g=__gcd(a,b),ma=max(a,b),mi=min(a,b);
		int time=ma/mi,m=ma%mi;
		if(m>g)
			time++;
		if(ma%mi==0)
			time--;
		if(time<k)
			cout<<"OBEY"<<endl;
		else
			cout<<"REBEL"<<endl;
	}
	return 0;
}

D. A Game with Traps

You are playing a computer game, where you lead a party of m soldiers. Each soldier is characterised by his agility ai.

The level you are trying to get through can be represented as a straight line segment from point 0 (where you and your squad is initially located) to point n+1 (where the boss is located).

The level is filled with k traps. Each trap is represented by three numbers li, ri and di. li is the location of the trap, and di is the danger level of the trap: whenever a soldier with agility lower than di steps on a trap (that is, moves to the point li), he gets instantly killed. Fortunately, you can disarm traps: if you move to the point ri, you disarm this trap, and it no longer poses any danger to your soldiers. Traps don’t affect you, only your soldiers.

You have t seconds to complete the level — that is, to bring some soldiers from your squad to the boss. Before the level starts, you choose which soldiers will be coming with you, and which soldiers won’t be. After that, you have to bring all of the chosen soldiers to the boss. To do so, you may perform the following actions:

if your location is x, you may move to x+1 or x−1. This action consumes one second;
if your location is x and the location of your squad is x, you may move to x+1 or to x−1 with your squad in one second. You may not perform this action if it puts some soldier in danger (i. e. the point your squad is moving into contains a non-disarmed trap with di greater than agility of some soldier from the squad). This action consumes one second;
if your location is x and there is a trap i with ri=x, you may disarm this trap. This action is done instantly (it consumes no time).
Note that after each action both your coordinate and the coordinate of your squad should be integers.

You have to choose the maximum number of soldiers such that they all can be brought from the point 0 to the point n+1 (where the boss waits) in no more than t seconds.

Input

The first line contains four integers m, n, k and t (1≤m,n,k,t≤2⋅105, n<t) — the number of soldiers, the number of integer points between the squad and the boss, the number of traps and the maximum number of seconds you may spend to bring the squad to the boss, respectively.

The second line contains m integers a1, a2, …, am (1≤ai≤2⋅105), where ai is the agility of the i-th soldier.

Then k lines follow, containing the descriptions of traps. Each line contains three numbers li, riand di (1≤li≤ri≤n, 1≤di≤2⋅105) — the location of the trap, the location where the trap can be disarmed, and its danger level, respectively.

Output

Print one integer — the maximum number of soldiers you may choose so that you may bring them all to the boss in no more than t seconds.

Example

input

5 6 4 14
1 2 3 4 5
1 5 2
1 2 5
2 3 5
3 5 3

output

3

Note

In the first example you may take soldiers with agility 3, 4 and 5 with you. The course of action is as follows:

go to 2 without your squad;
disarm the trap 2;
go to 3 without your squad;
disartm the trap 3;
go to 0 without your squad;
go to 7 with your squad.
The whole plan can be executed in 13 seconds.

分析:

题意:给你m个小兵的敏捷值ai,要求你们从0走到n+1。存在k个陷阱,从li开始,你走到ri的时候可以撤销它,如果小兵的敏捷值低于di,他就会被杀死。你可以选择自己一个人或者和小兵一起向前或向后走一个单位,每次耗时1秒。给你t秒,问你最多可以带多少小兵到达n+1处。
我们由题可以得到,我和小兵必须走的路径长度是n+1。所以求能拆掉最大的陷阱,以便带最多的小兵数量。我们可以把小兵排序,二分人数。把大于小兵的位置标记,需要走的路程即为标记的长度len*2+n+1。

由于我太菜,不会做,所以附上队长的代码:

#include<bits/stdc++.h>
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
const int N=2e5+5;
 
int cmp(int a,int b){
	return a>b; 
}
int m,n,k,t;
int a[N],vis[N],s[N];
 
struct node{
	int u,v,p;
}b[N];
 
int check(int x){
	int cnt=0;
	for(int i=0;i<k;i++){
		if(b[i].p>x){
			vis[b[i].u]++;
			vis[b[i].v+1]--;
		}
	}
	for(int i=1;i<=n+1;i++){
		vis[i]+=vis[i-1];	
	}
	for(int i=0;i<=n+1;i++){
		if(vis[i]){
			cnt++;
			vis[i]=0;
		}
	}
	if(cnt*2+n+1<=t)
		return 1;
	else
		return 0;
}
 
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>m>>n>>k>>t;
	for(int i=1;i<=m;i++)
		cin>>a[i];
	sort(a+1,a+1+m,cmp);
	for(int i=0;i<k;i++){
		cin>>b[i].u>>b[i].v>>b[i].p;
	}
	int l=1,r=m;
	while(r>=l){
		int mid=(l+r)/2;
		if(check(a[mid])){
			l=mid+1;
		}else{
			r=mid-1;
		}
	}
	for(int i=r;i>=l-1;i--){
		if(check(a[i])){
			cout<<i<<endl;
			break;
		}
		if(i==0){
			cout<<0<<endl;
			break;
		}
	}
	return 0;
}

E. Tournament

You are organizing a boxing tournament, where n boxers will participate (n is a power of 2), and your friend is one of them. All boxers have different strength from 1 to n, and boxer i wins in the match against boxer j if and only if i is stronger than j.

The tournament will be organized as follows: n boxers will be divided into pairs; the loser in each pair leaves the tournament, and n/2 winners advance to the next stage, where they are divided into pairs again, and the winners in all pairs advance to the next stage, and so on, until only one boxer remains (who is declared the winner).

Your friend really wants to win the tournament, but he may be not the strongest boxer. To help your friend win the tournament, you may bribe his opponents: if your friend is fighting with a boxer you have bribed, your friend wins even if his strength is lower.

Furthermore, during each stage you distribute the boxers into pairs as you wish.

The boxer with strength i can be bribed if you pay him aidollars. What is the minimum number of dollars you have to spend to make your friend win the tournament, provided that you arrange the boxers into pairs during each stage as you wish?

Input

The first line contains one integer n (2≤n≤218) — the number of boxers. n is a power of 2.

The second line contains n integers a1, a2, …, an, where ai is the number of dollars you have to pay if you want to bribe the boxer with strength i. Exactly one of ai is equal to −1 — it means that the boxer with strength i is your friend. All other values are in the range [1,109].

Output

Print one integer — the minimum number of dollars you have to pay so your friend wins.

Examples

input

4
3 9 1 -1

output

0

input

8
11 -1 13 19 24 7 17 5

output

12

Note

In the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament.

In the second test case you can distribute boxers as follows (your friend is number 2):

1:2,8:5,7:3,6:4 (boxers 2,8,7 and 6 advance to the next stage);

2:6,8:7 (boxers 2 and 8 advance to the next stage, you have to bribe the boxer with strength 6);

2:8 (you have to bribe the boxer with strength 8);

分析:

题意:给你n个人(n=2k)进行拳击比赛,每次将m人分成m/2对比赛,对于下标i>j,就是i获胜,但是你可以贿赂ai块让i输。你想让你的朋友获得最终胜利,你的朋友的ai值为-1,问你最少要花多少钱。
记朋友位置为id,我们从后面推一下,如果id!=n,那要贿赂第n个选手;如果id<n/2,那要贿赂[n/2~n)中最小的一个……可以用优先队列存贿赂的钱,按从小到大排,每次取出队头,加起来即为最小值。

代码:

#include<bits/stdc++.h>
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
#define ll long long
using namespace std;
int a[1000000];
priority_queue<ll,vector<ll>,greater<ll> > que;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	ll n,i,id;
	ll res=0;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]==-1)
			id=i;
	}
	ll k=n;
	while(k>id)
	{
		for(i=k;i<2*k&&i<=n;i++)
			que.push(a[i]);
		res+=que.top();
		que.pop();
		k/=2;
	}
	cout<<res<<endl;
	return 0;
}

记录分数:1332+132=1464。比赛只做出了abc题,d题想到二分但是不会写,e题最后时间才开始写,刚开始想错了,直接把所有排在朋友前面的数放进优先队列。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值