URAL1964:Chinese Dialects

来源:互联网 发布:淘宝 发卡 编辑:程序博客网 时间:2024/06/12 20:13

Open Ural FU Personal Contest 2013


Vova moved from Guangzhou to Shenzhen. He immediately found out that the local people don't understand his Cantonese phrases as well. Vova tried chatting with them in Mandarin, but to no success.
Then Vova decided to learn more about Chinese dialects. It turned out that people in China speak kdifferent dialects, at that at least a1 people speak the first dialect, at least a2 people speak the second dialect, …, at least ak people speak the k-th dialect. How many people speak all k dialects if the population of China is n people?

Input

The first line contains integers n and k (2 ≤ k ≤ 20; 1 ≤ n ≤ 109). The second line contains space-separated integers a1, …, ak (1 ≤ ak ≤ n).

Output

Print the minimum number of people in China that speak all k dialects of the Chinese language.

Samples

inputoutput

1000000000 2800000000 800000000
600000000
1000000000 2500000000 500000000
0

题意:有n个人,有m种语言,然后m个输入代表每种语言有几个人会讲,要求会讲所有语言的人有几个


思路:对于两种语言的话,我们不难发现必然是a1+a2-n,那么我们以此类推,先求出会讲两种语言的人数,在把会讲两种语言的人看做一种,循环计算下去求得会讲所有语言的人有几个


#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <algorithm>using namespace std;#define ls 2*i#define rs 2*i+1#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,x) memset(a,x,sizeof(a))#define w(a) while(a)#define LL long longconst double pi = acos(-1.0);#define Len 200005#define mod 19999997const int INF = 0x3f3f3f3f;LL n,m,N,M;int main(){    w(~scanf("%I64d%I64d",&n,&m))    {        LL t = n,x;        w(m--)        {            scanf("%I64d",&x);            t = max(0LL,t+x-n);        }        printf("%I64d\n",t);    }    return 0;}



0 0