Codeforces Round 926 (Div. 2) A B C

A. Sasha and the Beautiful Array

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Sasha decided to give his girlfriend an array a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an. He found out that his girlfriend evaluates the beauty of the array as the sum of the values ( a i − a i − 1 ) (a_i - a_{i - 1}) (aiai1) for all integers i i i from 2 2 2 to n n n.

Help Sasha and tell him the maximum beauty of the array a a a that he can obtain, if he can rearrange its elements in any way.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 500 1 \le t \le 500 1t500) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 100 2 \leq n \leq 100 2n100) — the length of the array a a a.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1ai109) — the elements of the array a a a.

Output

For each test case, output a single integer — the maximum beauty of the array a a a that can be obtained.

Example

input

5
3
2 1 3
3
69 69 69
5
100 54 80 43 90
4
3 4 3 3
2
2 1

output

2
0
57
1
1

Note

In the first test case, the elements of the array a a a can be rearranged to make a = [ 1 , 2 , 3 ] a = [1, 2, 3] a=[1,2,3]. Then its beauty will be equal to ( a 2 − a 1 ) + ( a 3 − a 2 ) = ( 2 − 1 ) + ( 3 − 2 ) = 2 (a_2 - a_1) + (a_3 - a_2) = (2 - 1) + (3 - 2) = 2 (a2a1)+(a3a2)=(21)+(32)=2.

In the second test case, there is no need to rearrange the elements of the array a a a. Then its beauty will be equal to 0 0 0.

Tutorial

a 2 − a 1 + a 3 − a 2 + … + a n − a n − 1 = a n − a 1 a_2 - a_1 + a_3 - a_2 + \ldots + a_n - a_{n - 1} = a_n - a_1 a2a1+a3a2++anan1=ana1,所以答案就是最大值减去最小值

Solution

for _ in range(int(input())):
    input()
    a = list(map(int, input().split()))
    print(max(a) - min(a))

B. Sasha and the Drawing

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Even in kindergarten, Sasha liked a girl. Therefore, he wanted to give her a drawing and attract her attention.

As a drawing, he decided to draw a square grid of size n × n n \times n n×n, in which some cells are colored. But coloring the cells is difficult, so he wants to color as few cells as possible. But at the same time, he wants at least k k k diagonals to have at least one colored cell. Note that the square grid of size n × n n \times n n×n has a total of 4 n − 2 4n - 2 4n2 diagonals.

Help little Sasha to make the girl fall in love with him and tell him the minimum number of cells he needs to color.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of test cases. The description of the test cases follows.

The only line of each test case contains two integers n n n and k k k ( 2 ≤ n ≤ 1 0 8 2 \leq n \leq 10^8 2n108, 1 ≤ k ≤ 4 n − 2 1 \leq k \leq 4n - 2 1k4n2) — the size of the square grid and the minimum number of diagonals in which there should be at least one colored cell.

Output

For each test case, output a single integer — the minimum number of cells that need to be colored.

Example

input

7
3 4
3 3
3 10
3 9
4 7
7 11
2 3

output

2
2
6
5
4
6
2

Note

In the pictures below, the colored cells are marked in black, and all diagonals are marked in purple.

In the first test case, you can color 2 2 2 cells so that 4 4 4 diagonals contain at least one colored cell:

In the third test case, you can color 6 6 6 cells so that all 10 10 10 diagonals contain at least one colored cell:

Tutorial

每一个格子最多可以满足两条对角线,所以如果 k ≤ ( 2 n − 2 ) × 2 k \le (2n - 2) \times 2 k(2n2)×2,答案最小为 k + 1 2 {k + 1} \over 2 2k+1,否则必须为另一面的格子涂色,且此时一个格子只能满足一条对角线,此时答案为 2 n 2n 2n

Solution

for _ in range(int(input())):
    n, k = map(int, input().split())
    print(2 * n if n * 4 < k + 3 else (k + 1) // 2)

C. Sasha and the Casino

time limit per test: 2 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

Sasha decided to give his girlfriend the best handbag, but unfortunately for Sasha, it is very expensive. Therefore, Sasha wants to earn it. After looking at earning tips on the internet, he decided to go to the casino.

Sasha knows that the casino operates under the following rules. If Sasha places a bet of y y y coins (where y y y is a positive integer), then in case of winning, he will receive y ⋅ k y \cdot k yk coins (i.e., his number of coins will increase by y ⋅ ( k − 1 ) y \cdot (k - 1) y(k1)). And in case of losing, he will lose the entire bet amount (i.e., his number of coins will decrease by y y y).

Note that the bet amount must always be a positive ( > 0 > 0 >0) integer and cannot exceed Sasha’s current number of coins.

Sasha also knows that there is a promotion at the casino: he cannot lose more than x x x times in a row.

Initially, Sasha has a a a coins. He wonders whether he can place bets such that he is guaranteed to win any number of coins. In other words, is it true that for any integer n n n, Sasha can make bets so that for any outcome that does not contradict the rules described above, at some moment of time he will have at least n n n coins.

Input

Each test consists of multiple test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of test cases. The description of the test cases follows.

The single line of each test case contains three integers k , x k, x k,x and a a a ( 2 ≤ k ≤ 30 2 \leq k \leq 30 2k30, 1 ≤ x ≤ 100 1 \leq x \leq 100 1x100, 1 ≤ a ≤ 1 0 9 1 \leq a \leq 10^9 1a109) — the number of times the bet is increased in case of a win, the maximum number of consecutive losses, and the initial number of coins Sasha has.

Output

For each test case, output “YES” (without quotes) if Sasha can achieve it and “NO” (without quotes) otherwise.

You can output “YES” and “NO” in any case (for example, the strings “yEs”, “yes” and “Yes” will be recognized as a positive answer).

Example

input

9
2 1 7
2 1 1
2 3 15
3 3 6
4 4 5
5 4 7
4 88 1000000000
25 69 231
13 97 18806

output

YES
NO
YES
NO
NO
YES
NO
NO
NO

Note

In the first test case, Sasha can proceed as follows:

  • If Sasha places a bet for the first time or if he won the previous bet, then he places 1 1 1 coin.
  • If Sasha lost the previous bet, then he places 2 2 2 coins.

Note that Sasha cannot lose more than once in a row.

It can be proven that with this strategy, Sasha can obtain as many coins as he wants.

In the second test case, Sasha can only place 1 1 1 coin for the first time. But in case of a loss, he will not be able to place any more bets, so he will not be able to guarantee having as many coins as he wants.

Tutorial

由题意得每次手上的硬币至少得有 1 1 1 个,对于每次下注,如果之前输了 c n t cnt cnt 元钱,那么在这一轮我们需要下注 c n t k − 1 + 1 {cnt \over k - 1} + 1 k1cnt+1 元,才能保证这次可以一次性把之前所有输掉的钱赢回来,所以最开始下注 1 1 1 元,后面每次根据输掉的钱下注即可,需要自己的钱可以撑到赌场给的“保底”,即自己手上的钱可以输 x x x 轮,时间复杂度 O ( x ) O(x) O(x)

Solution

# 能否必定赢钱
for _ in range(int(input())):
    k, x, a = map(int, input().split())
    cnt = 0
    for i in range(x + 1):
        cnt += cnt // (k - 1) + 1
    print("YES" if cnt <= a else "NO")

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值