1964. Chinese Dialects
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
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
a
1 people speak the first dialect, at least
a
2 people speak the second dialect, …, at least
a
k 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 ≤ 10
9). The second line contains space-separated integers
a
1, …,
a
k (1 ≤
a
k ≤
n).
Output
Print the minimum number of people in China that speak all
k dialects of the Chinese language.
Samples
input | output |
---|---|
1000000000 2 800000000 800000000 | 600000000 |
1000000000 2 500000000 500000000 | 0 |
题意:一共n个中国人,有k种方言,有a1个人会说第一种,a2个人会说第二种,ak个人会说第k种。问K种方言全会的人有多少。
解决:算是集合取交集了。将两个方言计算一次,然后将结果看做一种语言往下求结果。
细节:max函数, 每次两个方言都必须有交集,而且和都必须大于n才行。因为结果是取最小。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define fin freopen("in.txt", "r", stdin)
#define line printf("-----------------\n")
using namespace std;
const int maxn = 1000000;
int n, k, a, ans;
int main(){
cin >> n >> k;
ans = n;
for(int i = 1; i <= k; i++){
cin >> a;
ans = max(ans + a - n,0);
}
cout << ans << endl;
}