Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <set>
- #include <map>
- #include <list>
- #include <string>
- #include <cmath>
- #include <ctime>
- #include <stdio.h>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define dd long double
- #define ll long long
- #define PI acos(-1)
- #define mp make_pair
- #define pb push_back
- #define INF int(1e9+7)
- #define EPS 1e-12
- #define sqr(a) (a)*(a)
- vector <ll> a;
- ll n, d, ii = 0, jj = 1, m;
- bool cmp(ll x, ll y){
- return (x < y);
- }
- bool bc(ll checking){
- if (1 >= checking){
- return false;
- }
- int l = 0, r = n-3;
- while(l<=r){
- m = (l+r)/2;
- if (a[m] < checking && a[m+1] > checking) return true;
- if (a[m] < checking) l = m+1;
- else
- if (a[m] > checking) r = m;
- else
- return false;
- if (a[m] == checking) return false;
- if (l == 0 && r == 1 && a[0] != checking && a[1] != checking)
- return true;
- }
- return true;
- }
- int main(){
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- cin >> d >> n;
- a.resize(n-2);
- for (int z = 0; z < n-2; ++z){
- cin >> a[z];
- d -= a[z];
- }
- if (d <= 2){
- cout << -1;
- return 0;
- }
- sort(a.begin(), a.end(), cmp);
- while(ii < n-2){
- while(jj < a[ii]){
- if (bc(d-jj) && (jj < d-jj)){
- cout << jj;
- return 0;
- }
- ++jj;
- }
- ++jj;
- ++ii;
- }
- if (bc(d-jj)){
- cout << jj;
- return 0;
- }
- cout << -1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement