首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Codeforces Round #202 (Div. 一) A. Mafia

2013-09-29 
Codeforces Round #202 (Div. 1)A. MafiaA. Mafiatime limit per test2 secondsmemory limit per test256

Codeforces Round #202 (Div. 1) A. Mafia
A. Mafiatime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

One day n friends gathered together to play "Mafia". During each round of the game some player must be the supervisor and other n?-?1 people take part in the game. For each person we know in how many rounds he wants to be a player, not the supervisor: the i-th person wants to play ai rounds. What is the minimum number of rounds of the "Mafia" game they need to play to let each person play at least as many rounds as they want?

Input

The first line contains integer n (3?≤?n?≤?105). The second line contains n space-separated integers a1,?a2,?...,?an (1?≤?ai?≤?109) — the i-th number in the list is the number of rounds the i-th person wants to play.

Output

In a single line print a single integer — the minimum number of game rounds the friends need to let the i-th person play at leastai rounds.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the%I64d specifier.

Sample test(s)input
33 2 2
output
4
input
42 2 2 2
output
3
Note

You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times:http://en.wikipedia.org/wiki/Mafia_(party_game).

二分枚举就可以了,比赛是竟没想到用二分,二啊

#include <iostream>#include <stdio.h>#include <math.h>using namespace std;#define M 100050#define inf (((__int64)1<<62)-1)__int64 pri[M];int n;int fun(__int64 x){    int i;__int64 sum=0;    for(i=0;i<n;i++){        __int64 temp=x-pri[i];        if(temp<0)return 0;        sum+=temp;        if(sum>inf)return 1;    }    if(sum>=x)return 1;    return 0;}int main(){    int i;    while(scanf("%d",&n)!=EOF){        for( i=0;i<n;i++){             scanf("%I64d",&pri[i]);        }        __int64 l=1,r=inf-1,mid,last;        while(l<=r){            mid=(l+r)>>1;            if(fun(mid))            r=mid-1,last=mid;            else            l=mid+1;        }        printf("%I64d\n",last);    }    return 0;}


热点排行