Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <vector>
  2. #include <set>
  3. #include <map>
  4. #include <list>
  5. #include <string>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <stdio.h>
  9. #include <iostream>
  10. #include <algorithm>
  11.  
  12. using namespace std;
  13.  
  14.  
  15. #define dd long double
  16. #define ll long long
  17. #define PI acos(-1)
  18. #define mp make_pair
  19. #define pb push_back
  20.  
  21. #define INF int(1e9+7)
  22. #define EPS 1e-12
  23. #define sqr(a) (a)*(a)
  24.  
  25.  
  26. vector <ll> a;
  27. ll n, d, ii = 0, jj = 1, m;
  28.  
  29. bool cmp(ll x, ll y){
  30.     return (x < y);
  31. }
  32.  
  33.  
  34. bool bc(ll checking){
  35.    
  36.    
  37.    
  38.     if (1 >= checking){
  39.         return false;
  40.     }
  41.     int l = 0, r = n-3;
  42.  
  43.     while(l<=r){
  44.         m = (l+r)/2;
  45.         if (a[m] < checking && a[m+1] > checking) return true;
  46.         if (a[m] < checking) l = m+1;
  47.         else
  48.             if (a[m] > checking) r = m;
  49.         else
  50.             return false;
  51.        
  52.         if (a[m] == checking) return false;
  53.  
  54.         if (l == 0 && r == 1 && a[0] != checking && a[1] != checking)
  55.             return true;
  56.     }
  57.     return true;
  58. }
  59.  
  60.  
  61. int main(){
  62.  
  63.     freopen("input.txt", "r", stdin);
  64.     freopen("output.txt", "w", stdout);
  65.  
  66.  
  67.     cin >> d >> n;
  68.  
  69.     a.resize(n-2);
  70.  
  71.     for (int z = 0; z < n-2; ++z){
  72.         cin >> a[z];
  73.         d -= a[z];
  74.     }
  75.  
  76.     if (d <= 2){
  77.         cout << -1;
  78.         return 0;
  79.     }
  80.  
  81.     sort(a.begin(), a.end(), cmp);
  82.    
  83.  
  84.  
  85.     while(ii < n-2){
  86.         while(jj < a[ii]){
  87.             if (bc(d-jj) && (jj < d-jj)){
  88.                 cout << jj;
  89.                 return 0;
  90.             }
  91.             ++jj;
  92.         }
  93.         ++jj;
  94.         ++ii;
  95.     }
  96.  
  97.     if (bc(d-jj)){
  98.         cout << jj;
  99.         return 0;
  100.     }
  101.    
  102.     cout << -1;
  103.    
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement