Loading

2020 China Collegiate Programming Contest, Weihai Site A. Golden Spirit(思维)

It's a sunny day with good scenery, and you come to the park for a walk. You feel curious that there are many old guys gathering by a bridge, and want to take a look at what happened.

There are exactly 𝑛n old guys on each side of the bridge, and they all want to go across the bridge, take some time for relaxing on the other side, and finally go across the bridge back to the original side. However, they are too old to cross the bridge by themselves.

Driven by the golden spirit in your heart, you want to help these 2𝑛2n old guys. Initially, you are on one side of the bridge. It takes 𝑡t minutes for you to go across the bridge and 𝑥x minutes for an old guy relaxing. You may help an old guy when you cross the bridge, which doesn't take extra time.

As a master of time management, you want to know the minimum time needed to help all these 2𝑛2n old guys. Please write a program to calculate this minimum time.

Input

The first line contains one integer 𝑇T (1≤𝑡≤104)(1≤t≤104), indicating the total number of test cases.

For each of the next 𝑇T lines, there are three integers 𝑛,𝑥,𝑡n,x,t (1≤𝑛,𝑥,𝑡≤109)(1≤n,x,t≤109), as explained in problem statement.

Output

You should output exactly 𝑇T lines. Each line should contain exactly one integer, indicating the minimum time you needed in each test case.

Example

input

Copy

3
2 2 2
3 1 10
11 45 14

output

Copy

16
120
616
#include <bits/stdc++.h>
using namespace std;
int main() {
	int T;
	cin >> T;
	while(T--) {
		long long n, x, t;
		cin >> n >> x >> t;
		long long ans = 2e18;
		if(x <= 2 * (n - 1) * t) ans = 4 * n * t;
		else {
			ans = min(ans, 4 * n * t + (x - 2 * (n - 1) * t > 0 ? x - 2 * (n - 1) * t : 0));//先帮助和自己在一边的休息时间最长的老人回去
			ans = min(ans, 4 * n * t + (x - 2 * n * t > 0 ? x + t - 2 * n * t : t));//过河帮助另一边的休息时间最长的老人回去
			//注意如果到另一边再等一会的话是x + t - 2 * n * t而非x - 2 * n * t;因为另一边的这个老人的休息时间是从t时刻开始算的,等到帮他再回去的时候一共经过了x + t
		}
		cout << ans << endl;
	}
	return 0;
}

posted @ 2021-05-11 22:47  脂环  阅读(128)  评论(0编辑  收藏  举报