第二次个人排位赛题解(大二)

A - Bear and Elections

CodeForces - 574A

Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland.

There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate would get ai votes. Limak is candidate number 1. To win in elections, he must get strictly more votes than any other candidate.

Victory is more important than everything else so Limak decided to cheat. He will steal votes from his opponents by bribing some citizens. To bribe a citizen, Limak must give him or her one candy - citizens are bears and bears like candies. Limak doesn’t have many candies and wonders - how many citizens does he have to bribe?

Input

The first line contains single integer n (2 ≤ n ≤ 100) - number of candidates.

The second line contains n space-separated integers a1, a2, …, an (1 ≤ ai ≤ 1000) - number of votes for each candidate. Limak is candidate number 1.

Note that after bribing number of votes for some candidate might be zero or might be greater than 1000.

Output

Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate.

Examples

Input

5
5 1 11 2 8

Output

4

Input

4
1 8 8 8

Output

6

Input

2
7 6

Output

0

Note

In the first sample Limak has 5 votes. One of the ways to achieve victory is to bribe 4 citizens who want to vote for the third candidate. Then numbers of votes would be 9, 1, 7, 2, 8 (Limak would have 9 votes). Alternatively, Limak could steal only 3 votes from the third candidate and 1 vote from the second candidate to get situation 9, 0, 8, 2, 8.

In the second sample Limak will steal 2 votes from each candidate. Situation will be 7, 6, 6, 6.

In the third sample Limak is a winner without bribing any citizen.

AC代码:

#include <iostream>
using namespace std;

int arr[105];
int main()
{
    int m;
    while(cin>>m){
        for(int i=0; i<m; i++)
            cin>>arr[i];
        bool flag = 1;
        int ans = 0;
        while(flag){
            flag = 0;
            int Max = -9999999;
            int k = 1;
            for(int i=1; i<m; i++){
                if(arr[i] > Max){
                    Max = arr[i];
                    k = i;
                }
            }
            if(Max >= arr[0]) {
                arr[k]--;
                arr[0]++;
                ans++;
                flag = 1;
            }
            else
                flag = 0;
        }
        cout<<ans<<endl;
    }
    return 0;
}

B - Bear and Three Musketeers

CodeForces - 574B
Do you know a story about the three musketeers? Anyway, you will learn about its origins now.

Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to fight against bad guys.

There are n warriors. Richelimakieu wants to choose three of them to become musketeers but it’s not that easy. The most important condition is that musketeers must know each other to cooperate efficiently. And they shouldn’t be too well known because they could be betrayed by old friends. For each musketeer his recognition is the number of warriors he knows, excluding other two musketeers.

Help Richelimakieu! Find if it is possible to choose three musketeers knowing each other, and what is minimum possible sum of their recognitions.

Input

The first line contains two space-separated integers, n and m (3 ≤ n ≤ 4000, 0 ≤ m ≤ 4000) — respectively number of warriors and number of pairs of warriors knowing each other.

i-th of the following m lines contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi). Warriors ai and bi know each other. Each pair of warriors will be listed at most once.

Output

If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print “-1” (without the quotes).

Examples

Input

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

Output

2

Input

7 4
2 1
3 6
5 1
1 7

Output

1

Note

In the first sample Richelimakieu should choose a triple 1, 2, 3. The first musketeer doesn’t know anyone except other two musketeers so his recognition is 0. The second musketeer has recognition 1 because he knows warrior number 4. The third musketeer also has recognition 1 because he knows warrior 4. Sum of recognitions is 0 + 1 + 1 = 2.

The other possible triple is 2, 3, 4 but it has greater sum of recognitions, equal to 1 + 1 + 1 = 3.

In the second sample there is no triple of warriors knowing each other.

AC代码:

#include<iostream>
#include<string.h>
#include<stdio.h>
#define MIN 2147483568
using namespace std;

int n,m,mi=MIN,x,y;
int cnt[4005],g[4005][4005];

int main(){
    memset(cnt,0,sizeof(cnt));
    memset(g,0,sizeof(g));
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++){
        scanf("%d%d",&x,&y);
        cnt[x]++;
        cnt[y]++;
        g[x][y]=1;
        g[y][x]=1;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n&&j!=i;j++){
            if(g[i][j]==0){
                continue;
            }
            for(int k=1;k!=i&&k!=j&&k<=n;k++){
                if(g[k][i]==1&&g[k][j]==1){
                    int tmp=cnt[j]+cnt[i]+cnt[k]-6;
                    if(mi>tmp){
                        mi=tmp;
                    }
                }
            }
        }
    }
    printf("%d\n",mi==MIN?-1:mi);
    return 0;
}

C - Bear and Poker

CodeForces - 574C
Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars.

Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot?

Input

First line of input contains an integer n (2 ≤ n ≤ 105), the number of players.

The second line contains n integer numbers a1, a2, …, an (1 ≤ ai ≤ 109) — the bids of players.

Output

Print “Yes” (without the quotes) if players can make their bids become equal, or “No” otherwise.

Examples

Input

4
75 150 75 50

Output

Yes

Input

3
100 150 250

Output

No

Note

In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid.

It can be shown that in the second sample test there is no way to make all bids equal.

AC代码:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<set>
using namespace std;

set<long long>s;

int main(){
    int n;
    long long x;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%lld",&x);
        while(x%2==0){
            x/=2;
        }
        while(x%3==0){
            x/=3;
        }
        s.insert(x);
    }
    if(s.size()==1){
        printf("Yes\n");
    }
    else{
        printf("No\n");
    }
    return 0;
} 

D - Bear and Blocks

CodeForces - 574D

Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample.

Limak will repeat the following operation till everything is destroyed.

Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time.

Limak is ready to start. You task is to count how many operations will it take him to destroy all towers.

Input

The first line contains single integer n (1 ≤ n ≤ 105).

The second line contains n space-separated integers h1, h2, …, hn (1 ≤ hi ≤ 109) — sizes of towers.

Output

Print the number of operations needed to destroy all towers.

Examples

Input

6
2 1 4 6 2 2

Output

3

Input

7
3 3 3 1 3 3 3

Output

2

Note

The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color.
这里写图片描述

After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.

AC代码:

#include<iostream> 
#include <stdio.h>
#include <cstring>
#include<algorithm>
using namespace std;
int dp[200000];
int t[200000];
int l[200000];
int r[200000];
int n;
int main()
{
    while(scanf("%d",&n)!=EOF){
        for(int i = 1; i <= n; i++){
            scanf("%d",&t[i]);
        }
        l[1] = 1;
        for(int i = 2; i <= n; i++){
            l[i] = min(l[i-1] + 1, t[i]);//从左到右
        }
        r[n] = 1;
        for(int i = n-1; i >= 1; i--){
            r[i] = min(r[i+1] + 1, t[i]);//从右到左
        }
        int ans = 0;
        for(int i = 1; i <= n; i++){
            ans = max(ans,min(l[i],r[i]));
        }
        printf("%d\n",ans);
    }
    return 0;
}

从这次起,我会认真写好每次比赛的题解报告,真的是不经受打击,不知道自己有多弱。
之前打的比赛,题目的题解报告我会补上。

本次比赛链接:https://vjudge.net/contest/244445#overview

猜你喜欢

转载自blog.csdn.net/wuyileiju__/article/details/81475550
今日推荐