Problem:
舞踏会 (Ball)
IOI 王国では，王女である JOI 姫の誕生日を祝って舞踏会が開かれることになった．
舞踏会には
N
人の貴族が参加する予定である．
N
は奇数である．貴族には 1 から
N
までの番号が付けられている．それぞれの貴族には踊りのうまさという整数が定められており，貴族
i
(1 ≤
i
≤
N
) の踊りのうまさは
D
i
である．
舞踏会では JOI 姫を含む
N
+ 1 人で 2 人ずつ組を作って踊る．IOI 王国では，上級者が初級者を補助できるように，伝統的に以下の方法で踊りの組を決定している．
最初に，
N
人の貴族が 1 列に並ぶ．
列に並んでいる貴族が 1 人になるまで，以下の操作を繰り返す．
- 列の先頭から 3 人の貴族の踊りのうまさを調べる．
- その 3 人の貴族の中で，最も踊りのうまさが大きい貴族を A とおく．ただし，複数いる場合は，最も踊りのうまさが大きい貴族の中で，最も番号の小さい貴族 を A とおく．
- その 3 人の貴族の中で，最も踊りのうまさが小さい貴族を B とおく．ただし，複数いる場合は，最も踊りのうまさが小さい貴族の中で，最も番号の大きい貴族 を B とおく．
- A と B が列から抜けて組になる．
- 残った 1 人は列の最後尾に移動する．
最終的に残った 1 人が JOI 姫と組になる．
貴族 1 から貴族
M
(1 ≤
M
≤
N
− 2) の
M
人の貴族については，すでに初期状態で列の何番目に並ぶのかが決まっている．残りの
N
−
M
人の貴族の並び方は国王が自由に決めることができる．
JOI 姫は踊りを学んだばかりなので，国王は JOI 姫と組になる貴族の踊りのうまさをできるだけ大きくしたいと考えている．JOI 姫と組になる貴族の踊りのうまさとして考えられる最大値を求めよ．
課題
それぞれの貴族の踊りのうまさと，
M
人の貴族の初期状態で並ぶ場所が与えられたとき，JOI 姫と組になる貴族の踊りのうまさとして考えられる最大値を求めるプログラムを作成せよ．
入力
標準入力から以下のデータを読み込め．
1 行目には，2 個の整数
N
,
M
が空白を区切りとして書かれている．これは舞踏会に貴族が
N
人参加し，列に並ぶ場所がすでに決まっている貴族が
M
人いることを表す．
続く
M
行のうちの
i
行目 (1 ≤
i
≤
M
) には，2 個の整数
D
i
,
P
i
が空白を区切りとして書かれている．これは貴族
i
の踊りのうまさが
D
i
で，貴族
i
が初期状態で列の先頭から
P
i
番目に並ぶことを表す．
続く
N
−
M
行のうちの
i
行目 (1 ≤
i
≤
N
−
M
) には，整数
D
i+M
が書かれている．これは貴族 (
i
+
M
)の踊りのうまさが
D
i+M
であることを表す．
出力
標準出力に，JOI 姫と組になる貴族の踊りのうまさとして考えられる最大値を表す整数を 1 行で出力せよ．
制限
すべての入力データは以下の条件を満たす．
3 ≤
N
≤ 99 999．
N
は奇数である．
1 ≤
M
≤
N
− 2．
1 ≤
D
i
≤ 1 000 000 000 (1 ≤
i
≤
N
)．
1 ≤
P
i
≤
N
(1 ≤
i
≤
M
)．
P
i
≠
P
j
(1 ≤
i
<
j
≤
M
)．
入出力例
入力例 1
7 3
5 2
5 5
8 6
6
2
8
9
出力例 1
8
初期状態では 3 人の貴族の並ぶ場所がすでに決まっている．
括弧内の数字は踊りのうまさを表す．左端が列の先頭である．
例えば，先頭から順に貴族 5，貴族 1，貴族 4，貴族 6，貴族 2，貴族 3，貴族 7 という順番に並んだ場
合を考える．
すべての貴族が並んだあとの配置
この場合，以下のように列が変化していく．
列の先頭の 3 人の貴族 (貴族 5，貴族 1，貴族 4) 中で，最も踊りのうまさが大きい貴族 4 と最も踊りのうまさが小さい貴族 5 が組になり，残った貴族 1 が最後尾に移動する．
次に，列の先頭の 3 人の貴族 (貴族 6，貴族 2，貴族 3) の中で，最も踊りのうまさが大きい貴族は貴族 6 と貴族 3 の 2 人であり，このうち番号の小さい貴族は貴族 3 である．また，列の先頭の 3 人の貴族のうち最も踊りのうまさが小さい貴族は貴族 2 である．貴族 3 と貴族 2 が組になり，残った貴族 6 が最後尾に移動する．
次に，列の先頭の 3 人の貴族 (貴族 7，貴族 1，貴族 6) の中で，最も踊りのうまさが大きい貴族 7 と最も踊りのうまさが小さい貴族 1 が組になり，残った貴族 6 が最後尾に移動する．
最終的に貴族 6 が残り，JOI 姫と組になる．貴族 6 の踊りのうまさは 8 である．この値が JOI 姫と組になる貴族の踊りのうまさとして考えられる最大値である．
列の変化の様子
入力例 2
3 1
5 3
5
5
出力例 2
5
どのような順番で並んでも，貴族 2 と JOI 姫が組になる．
入力例 3
7 2
32 4
27 6
37
41
41
30
27
出力例 3
37
問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std;
int n, m, a, b, c[100000], D[100000], E[100000];
map<vector<int>, tuple<vector<int>, vector<int>, vector<int>>>M;
void make(int n) {
	queue<vector<int>>Q; for (int i = 1; i <= n; i++) { vector<int>ichi; ichi.push_back(i); Q.push(ichi); }
	while (Q.size() > 1) {
		vector<int>b1 = Q.front(); Q.pop();
		vector<int>b2 = Q.front(); Q.pop();
		vector<int>b3 = Q.front(); Q.pop();
		vector<int>sum;
		for (int i = 0; i < b1.size(); i++)sum.push_back(b1[i]);
		for (int i = 0; i < b2.size(); i++)sum.push_back(b2[i]);
		for (int i = 0; i < b3.size(); i++)sum.push_back(b3[i]);
		sort(sum.begin(), sum.end());
		M[sum] = make_tuple(b1, b2, b3); Q.push(sum);
	}
	Q.pop();
}
int solve(vector<int>vec, int border) {
	if (vec.size() == 1) {
		if (c[vec[0]] < border && c[vec[0]] >= 1)return 10000000;
		if (c[vec[0]] >= border && c[vec[0]] >= 1)return 0;
		return 1;
	}
	int s[3] = { solve(get<0>(M[vec]), border),solve(get<1>(M[vec]), border),solve(get<2>(M[vec]), border) };
	sort(s, s + 3);
	return s[0] + s[1];
}
int main() {
	cin >> n >> m; for (int i = 0; i < m; i++) { cin >> a >> b; c[b] = a; D[i] = a; }
	for (int i = m; i < n; i++) { cin >> a; D[i] = a; E[i - m] = a; }
	sort(D, D + n); sort(E, E + n - m);
	make(n);
	int maxn = 0;
	vector<int>Ball; for (int i = 1; i <= n; i++)Ball.push_back(i);
	
	//--------------------------------------------Binary Search------------------------------------
	int L = 0, R = n, M;
	while (true) {
		bool Y1 = false, Y2 = false;
		M = (L + R) / 2;
		if (M < n) {
			int F = solve(Ball, D[M]);
			int pos1 = lower_bound(E, E + n - m, D[M] - 1) - E;
			pos1 = (n - m) - pos1;
			if (F <= pos1)Y1 = true;
		}
		if (M < n - 1) {
			int F = solve(Ball, D[M + 1]);
			int pos2 = lower_bound(E, E + n - m, D[M + 1] - 1) - E;
			pos2 = (n - m) - pos2;
			if (F <= pos2)Y2 = true;
		}
		if (Y1 == true && Y2 == false) { maxn = D[M]; break; }
		if (Y1 == false) { R = M; }
		if (Y2 == true) { L = M; }
	}
	cout << maxn << endl;
	return 0;
}
accuracy: 1
time_ms: 590
memory_kb: 26196
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, ll> Q;
#define mp make_pair
#define pb push_back
#define INF 10000000

int n, m;
int s[100005], p[100005];
int q[200005];

int main() {
    // 入力を受け取ります
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        cin >> s[i] >> p[i];
    }
    for (int i = m + 1; i <= n; i++) {
        cin >> s[i];
    }

    // 二分探索の範囲を設定します
    int lb = 0;
    int ub = 1e9 + 50;

    // 二分探索を実行します
    while (ub - lb > 1) {
        int mid = (lb + ub) / 2;
        int x = 1, y = n;

        // 配列qを初期化します
        fill(q + 1, q + n + 1, 1);
        for (int i = 1; i <= m; i++) {
            if (s[i] >= mid) {
                q[p[i]] = 0;
            } else {
                q[p[i]] = n + 5;
            }
        }

        // 条件に基づいて配列qを更新します
        while (y - x > 1) {
            int sum = q[x] + q[x + 1] + q[x + 2];
            sum -= max({q[x], q[x + 1], q[x + 2]});
            q[y + 1] = sum;
            x += 3;
            y++;
        }

        // 条件を満たす要素のカウント
        int cnt = 0;
        for (int i = m + 1; i <= n; i++) {
            cnt += (s[i] >= mid);
        }

        // 二分探索の範囲を更新します
        if (q[y] <= cnt) {
            lb = mid;
        } else {
            ub = mid;
        }
    }

    // 結果を出力します
    cout << lb << endl;

    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 4680
final_rank: 5

code_snippet: #include <cassert>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

const long long INF = 1e9;


vector<vector<long long>> generate_graph(long long n){
  vector<vector<long long>> G(n);
  deque<long long> dq;
  for(long long i = 0; i < n; ++i)
    dq.emplace_back(i);
  long long sz = n;
  while(dq.size() > 1){
    vector<long long> ch;
    for(long long i = 0; i < 3; ++i){
      ch.emplace_back(dq.front());
      dq.pop_front();
    }
    G.emplace_back(ch);
    dq.emplace_back(sz);
    ++sz;
  }
  return G;
}

long long solve(const vector<vector<long long>>& G, long long v, const vector<long long>& E){
  if(G[v].empty()){
    if(E[v] == 1) return 0;
    if(E[v] < 0) return 1;
    return INF;
  }
  assert(G[v].size() == 3);
  long long ma = 0, s = 0;
  for(auto u : G[v]){
    long long x = solve(G,u,E);
    s += x;
    ma = max(ma,x);
  }
  s -= ma;
  s = min(s,INF);
  return s;
}

int main(){
  long long N, M;
  cin >> N >> M;
  vector<long long> D(N,-1);
  vector<long long> R;
  for(long long i = 0; i < M; ++i){
    long long d, p;
    cin >> d >> p;
    --p;
    D[p] = d;
  }
  for(long long i = M; i < N; ++i){
    long long d;
    cin >> d;
    R.emplace_back(d);
  }
  long long ok = 0, ng = INF;
  auto G = generate_graph(N);
  // for(size_t i = 0; i < G.size(); ++i){
  //   cerr << i << " : ";
  //   for(auto e : G[i])
  //     cerr << e << " ";
  //   cerr << endl;
  // }
  const long long v = G.size()-1;
  // cerr << v << endl;
  while(ng-ok > 1){
    long long m = (ok+ng)/2;
    vector<long long> E(N);
    // 0 : D[i] < m
    // 1 : D[i] >= m
    // -1 : 未確定
    for(long long i = 0; i < N; ++i){
      if(D[i] == -1) E[i] = -1;
      else if(D[i] < m) E[i] = 0;
      else E[i] = 1;
    }
    long long c = 0;
    for(auto e : R){
      c += (e >= m);
    }
    long long x = solve(G,v,E);
    // cerr << m << " " << c << " " << x << endl;
    if(x <= c) ok = m;
    else ng = m;
  }
  cout << ok << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 12064
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(12), 0);
const int INF = 1e9;
struct Tree {
    Tree(int i) {
        idx = i;
        children[0] = children[1] = children[2] = nullptr;
    }
    Tree(Tree *a, Tree *b, Tree *c) {
        idx = -1;
        children[0] = a, children[1] = b, children[2] = c;
    }
    int idx;
    Tree *children[3];
    int ans;
};
Tree* build(int n) {
    assert(n % 2 == 1);
    deque<Tree*> items;
    for (int i = 0; i < n; i++) {
        items.push_back(new Tree(i));
    }
    while (items.size() >= 2) {
        auto t = new Tree(items[0], items[1], items[2]);
        items.pop_front(), items.pop_front(), items.pop_front();
        items.push_back(t);
    }
    return items[0];
}
int main() {
    int N, M; cin >> N >> M;
    vector<int> D(N, -1);
    vector<int> rest(N - M);
    for (int i = 0; i < M; i++) {
        int d, p; cin >> d >> p; p--;
        D[p] = d;
    }
    for (int i = 0; i < N - M; i++) {
        int d; cin >> d;
        rest.push_back(d);
    }
    Tree *root = build(N);
    int ok = 1, ng = 1000000001;
    while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;
        int cnt = 0;
        for (auto r : rest) {
            if (r >= mid) cnt++;
        }
        auto recur = [&](auto&& recur, Tree *node) -> void {
            if (node->idx >= 0) {
                if (D[node->idx] >= mid) {
                    node->ans = 0;
                } else if (D[node->idx] == -1) {
                    node->ans = 1;
                } else {
                    node->ans = INF;
                }
            } else {
                for (int i = 0; i < 3; i++) {
                    recur(recur, node->children[i]);
                }
                int sum = 0, maxval = 0;
                for (int i = 0; i < 3; i++) {
                    int a = node->children[i]->ans;
                    sum += a;
                    maxval = max(maxval, a);
                }
                node->ans = min(INF, sum - maxval);
            }
        };
        recur(recur, root);
        if (cnt >= root->ans) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    cout << ok << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 11268
final_rank: 6

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
const int inf = 123456789;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N, M;
	cin >> N >> M;
	vector<pair<int, int> > X(M);
	vector<int> Y(N - M);
	for (int i = 0; i < M; ++i) {
		cin >> X[i].first >> X[i].second;
		--X[i].second;
	}
	for (int i = 0; i < N - M; ++i) {
		cin >> Y[i];
	}
	sort(X.begin(), X.end(), greater<pair<int, int> >());
	sort(Y.begin(), Y.end(), greater<int>());
	vector<int> dp(N + N / 2, 1);
	for (int i = 0; i < M; ++i) {
		dp[X[i].second] = inf;
	}
	for (int i = 0; (i + 1) * 2 < N; ++i) {
		dp[i + N] = min(dp[i * 3] + dp[i * 3 + 1] + dp[i * 3 + 2] - max({ dp[i * 3], dp[i * 3 + 1], dp[i * 3 + 2] }), inf);
	}
	int ans = (dp.back() != inf ? Y[dp.back() - 1] : -inf);
	for (int i = 0; i < M; ++i) {
		int pos = X[i].second;
		dp[pos] = 0;
		while (pos + 1 != N + N / 2) {
			pos /= 3;
			dp[pos + N] = min(dp[pos * 3] + dp[pos * 3 + 1] + dp[pos * 3 + 2] - max({ dp[pos * 3], dp[pos * 3 + 1], dp[pos * 3 + 2] }), inf);
			pos += N;
		}
		ans = max(ans, min(X[i].first, dp.back() != inf ? Y[dp.back() - 1] : -inf));
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4116
final_rank: 1

code_snippet: #pragma GCC optimize ("Ofast")
#pragma GCC target ("avx")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<unordered_map>
#include<climits>
#include<fstream>
#include<complex>
#include<time.h>
#include<cassert>
#include<functional>
#include<numeric>
#include<tuple>

using namespace std;
using ll = long long;
using ld = long double;
#define int long long
#define all(a) (a).begin(),(a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define H pair<int, int>
#define P pair<int, pair<int, int>>
#define Q(i,j,k) mkp(i,mkp(j,k))
#define R pair<pair<int, int>, pair<int, int>>
#define S(i,j,k,l) mkp(mkp(i,j),mkp(k,l))
#define rep(i,n) for(int (i) = 0 ; (i) < (n) ; (i)++)
#define rng(i,s,n) for(int (i) = (s) ; (i) < (n) ; (i)++)
#define req(n,i) for(int (i) = (n) - 1 ; (i) >= 0 ; (i)--)
#define range(i,v) for(auto& (i) : v)
#define mkp make_pair
#define mem(x,k) memset(x,k,sizeof(x))
#define vec vector
#define pb emplace_back
#define lb lower_bound
#define ub upper_bound
#define crdcomp(b) sort(all((b)));(b).erase(unique(all((b))),(b).end())
#define getidx(b,i) lower_bound(all(b),(i))-b.begin()
#define ssp(i,n) (i==n-1?"\n":" ")
#define ctoi(c) (int)(c-'0')
#define itoc(c) (char)(c+'0')
#define itn int
#define pri_que priority_queue
#define pp(x,y) pb(H{x,y})
#define ppp(x,y,z) pb(Q(x,y,z))
#define pppp(x,y,z,w) pb(S(x,y,z,w))
#define cyes cout<<"Yes\n"
#define cno cout<<"No\n"
#define endl "\n"
constexpr int mod = 1e9 + 7;
constexpr int Mod = 998244353;
constexpr int MXN = 500000 + 100;
constexpr ll inf = 3 * 1e18;
constexpr int Inf = 15 * 1e8;
const vec<int>dx{ -1,1,0,0 }, dy{ 0,0,-1,1 };
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
ll read() { ll u, k = scanf("%lld", &u); return u; }
string reads() { string s; cin >> s; return s; }
H readh(bool g = 0) { H u; ll k = scanf("%lld %lld", &u.fs, &u.sc); if (g) u.fs--, u.sc--; return u; }
void printh(H t) { printf("%lld %lld\n", t.fs, t.sc); }
bool inarea(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
ll mod_pow(ll x, ll n, ll p = mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x % p;
		x = x * x % p;
		n >>= 1;
	}
	return res;
}//x^n%p
ll bitcount(ll x) {
	int sum = 0; for (int i = 0; i < 60; i++)if ((1ll << i) & x) sum++;
	return sum;
}
/*constexpr int fn_ = 1000005;
ll fact_[fn_], comp_[fn_];
ll comb(ll x, ll y, ll Mod = mod) {
	if (!fact_[0]) {
		fact_[0] = 1; comp_[0] = 1;
		for (int i = 1; i < fn_; i++)
			fact_[i] = fact_[i - 1] * i % Mod;
		comp_[fn_ - 1] = mod_pow(fact_[fn_ - 1], Mod - 2, Mod);
		for (int i = fn_ - 2; i > 0; i--)
			comp_[i] = comp_[i + 1] * (i + 1) % Mod;
	}
	if (x < y) return 0;
	return fact_[x] * comp_[x - y] % Mod * comp_[y] % Mod;
}*/
//--------------------------------------------------------------
int n, m;
vec<int>e[1000000];
int a[1000000];
vec<int>d;
int solve(int pos, int t) {
	if (e[pos].size() == 0) {
		if (a[pos]) {
			if (a[pos] >= t) return 0;
			else return Inf;
		}
		else return 1;
	}
	vec<int>b;
	for (auto to : e[pos]) b.pb(solve(to, t));
	sort(all(b));
	return b[0] + b[1];
}
signed main() {
	cin >> n >> m;
	rep(i, m) {
		int u, v; cin >> u >> v;
		a[v - 1] = u;
	}
	rep(i, n - m) d.pb(read());
	deque<int>dq;
	rep(i, n) dq.push_back(i);
	int idx = n;
	while (dq.size() > 1) {
		e[idx].pb(dq.front()); dq.pop_front();
		e[idx].pb(dq.front()); dq.pop_front();
		e[idx].pb(dq.front()); dq.pop_front();
		dq.push_back(idx++);
	}
	sort(all(d));
	int ok = 0, ng = Inf, mid;
	while (abs(ng - ok) > 1) {
		mid = (ok + ng) / 2;
		int sum = (n - m) - (lower_bound(all(d), mid) - d.begin());
		if (solve(idx - 1, mid) <= sum) ok = mid;
		else ng = mid;
	}
	cout << ok << endl;
}
accuracy: 1
time_ms: 210
memory_kb: 30276
final_rank: 15

code_snippet: #pragma GCC optimize ("Ofast")
#pragma GCC target ("avx")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<unordered_map>
#include<climits>
#include<fstream>
#include<complex>
#include<time.h>
#include<cassert>
#include<functional>
#include<numeric>
#include<tuple>

using namespace std;
using ll = long long;
using ld = long double;
#define int long long
#define all(a) (a).begin(),(a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define H pair<int, int>
#define P pair<int, pair<int, int>>
#define Q(i,j,k) mkp(i,mkp(j,k))
#define R pair<pair<int, int>, pair<int, int>>
#define S(i,j,k,l) mkp(mkp(i,j),mkp(k,l))
#define rep(i,n) for(int (i) = 0 ; (i) < (n) ; (i)++)
#define rng(i,s,n) for(int (i) = (s) ; (i) < (n) ; (i)++)
#define req(n,i) for(int (i) = (n) - 1 ; (i) >= 0 ; (i)--)
#define range(i,v) for(auto& (i) : v)
#define mkp make_pair
#define mem(x,k) memset(x,k,sizeof(x))
#define vec vector
#define pb emplace_back
#define lb lower_bound
#define ub upper_bound
#define crdcomp(b) sort(all((b)));(b).erase(unique(all((b))),(b).end())
#define getidx(b,i) lower_bound(all(b),(i))-b.begin()
#define ssp(i,n) (i==n-1?"\n":" ")
#define ctoi(c) (int)(c-'0')
#define itoc(c) (char)(c+'0')
#define itn int
#define pri_que priority_queue
#define pp(x,y) pb(H{x,y})
#define ppp(x,y,z) pb(Q(x,y,z))
#define pppp(x,y,z,w) pb(S(x,y,z,w))
#define cyes cout<<"Yes\n"
#define cno cout<<"No\n"
#define endl "\n"
constexpr int mod = 1e9 + 7;
constexpr int Mod = 998244353;
constexpr int MXN = 500000 + 100;
constexpr ll inf = 3 * 1e18;
constexpr int Inf = 15 * 1e8;
const vec<int>dx{ -1,1,0,0 }, dy{ 0,0,-1,1 };
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
ll read() { ll u, k = scanf("%lld", &u); return u; }
string reads() { string s; cin >> s; return s; }
H readh(bool g = 0) { H u; ll k = scanf("%lld %lld", &u.fs, &u.sc); if (g) u.fs--, u.sc--; return u; }
void printh(H t) { printf("%lld %lld\n", t.fs, t.sc); }
bool inarea(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
ll mod_pow(ll x, ll n, ll p = mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x % p;
		x = x * x % p;
		n >>= 1;
	}
	return res;
}//x^n%p
ll bitcount(ll x) {
	int sum = 0; for (int i = 0; i < 60; i++)if ((1ll << i) & x) sum++;
	return sum;
}
/*constexpr int fn_ = 1000005;
ll fact_[fn_], comp_[fn_];
ll comb(ll x, ll y, ll Mod = mod) {
	if (!fact_[0]) {
		fact_[0] = 1; comp_[0] = 1;
		for (int i = 1; i < fn_; i++)
			fact_[i] = fact_[i - 1] * i % Mod;
		comp_[fn_ - 1] = mod_pow(fact_[fn_ - 1], Mod - 2, Mod);
		for (int i = fn_ - 2; i > 0; i--)
			comp_[i] = comp_[i + 1] * (i + 1) % Mod;
	}
	if (x < y) return 0;
	return fact_[x] * comp_[x - y] % Mod * comp_[y] % Mod;
}*/
//--------------------------------------------------------------
int n, m;
vec<int>e[9000000];
int a[1000000];
vec<int>d;
int solve(int pos, int t) {
	if (e[pos].size() == 0) {
		if (a[pos]) {
			if (a[pos] >= t) return 0;
			else return inf;
		}
		else return 1;
	}
	vec<int>b;
	for (auto to : e[pos]) b.pb(solve(to, t));
	sort(all(b));
	return b[0] + b[1];
}
signed main() {
	cin >> n >> m;
	rep(i, m) {
		int u, v; cin >> u >> v;
		a[v - 1] = u;
	}
	rep(i, n - m) d.pb(read());
	deque<int>dq;
	rep(i, n) dq.push_back(i);
	int idx = n;
	while (dq.size() > 1) {
		e[idx].pb(dq.front()); dq.pop_front();
		e[idx].pb(dq.front()); dq.pop_front();
		e[idx].pb(dq.front()); dq.pop_front();
		dq.push_back(idx++);
	}
	if (idx >= 1000000) assert(0);
	sort(all(d));
	int ok = -10, ng = inf, mid;
	while (abs(ng - ok) > 1) {
		mid = (ok + ng) / 2;
		int sum = (n - m) - (lower_bound(all(d), mid) - d.begin());
		if (solve(idx - 1, mid) <= sum) ok = mid;
		else ng = mid;
	}
	cout << ok << endl;
}
accuracy: 0.5882352941176471
time_ms: 70
memory_kb: 214192
final_rank: 19

code_snippet: #pragma GCC optimize ("Ofast")
#pragma GCC target ("avx")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<unordered_map>
#include<climits>
#include<fstream>
#include<complex>
#include<time.h>
#include<cassert>
#include<functional>
#include<numeric>
#include<tuple>

using namespace std;
using ll = long long;
using ld = long double;
#define int long long
#define all(a) (a).begin(),(a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define H pair<int, int>
#define P pair<int, pair<int, int>>
#define Q(i,j,k) mkp(i,mkp(j,k))
#define R pair<pair<int, int>, pair<int, int>>
#define S(i,j,k,l) mkp(mkp(i,j),mkp(k,l))
#define rep(i,n) for(int (i) = 0 ; (i) < (n) ; (i)++)
#define rng(i,s,n) for(int (i) = (s) ; (i) < (n) ; (i)++)
#define req(n,i) for(int (i) = (n) - 1 ; (i) >= 0 ; (i)--)
#define range(i,v) for(auto& (i) : v)
#define mkp make_pair
#define mem(x,k) memset(x,k,sizeof(x))
#define vec vector
#define pb emplace_back
#define lb lower_bound
#define ub upper_bound
#define crdcomp(b) sort(all((b)));(b).erase(unique(all((b))),(b).end())
#define getidx(b,i) lower_bound(all(b),(i))-b.begin()
#define ssp(i,n) (i==n-1?"\n":" ")
#define ctoi(c) (int)(c-'0')
#define itoc(c) (char)(c+'0')
#define itn int
#define pri_que priority_queue
#define pp(x,y) pb(H{x,y})
#define ppp(x,y,z) pb(Q(x,y,z))
#define pppp(x,y,z,w) pb(S(x,y,z,w))
#define cyes cout<<"Yes\n"
#define cno cout<<"No\n"
#define endl "\n"
constexpr int mod = 1e9 + 7;
constexpr int Mod = 998244353;
constexpr int MXN = 500000 + 100;
constexpr ll inf = 3 * 1e18;
constexpr int Inf = 15 * 1e8;
const vec<int>dx{ -1,1,0,0 }, dy{ 0,0,-1,1 };
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
ll read() { ll u, k = scanf("%lld", &u); return u; }
string reads() { string s; cin >> s; return s; }
H readh(bool g = 0) { H u; ll k = scanf("%lld %lld", &u.fs, &u.sc); if (g) u.fs--, u.sc--; return u; }
void printh(H t) { printf("%lld %lld\n", t.fs, t.sc); }
bool inarea(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
ll mod_pow(ll x, ll n, ll p = mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x % p;
		x = x * x % p;
		n >>= 1;
	}
	return res;
}//x^n%p
ll bitcount(ll x) {
	int sum = 0; for (int i = 0; i < 60; i++)if ((1ll << i) & x) sum++;
	return sum;
}
/*constexpr int fn_ = 1000005;
ll fact_[fn_], comp_[fn_];
ll comb(ll x, ll y, ll Mod = mod) {
	if (!fact_[0]) {
		fact_[0] = 1; comp_[0] = 1;
		for (int i = 1; i < fn_; i++)
			fact_[i] = fact_[i - 1] * i % Mod;
		comp_[fn_ - 1] = mod_pow(fact_[fn_ - 1], Mod - 2, Mod);
		for (int i = fn_ - 2; i > 0; i--)
			comp_[i] = comp_[i + 1] * (i + 1) % Mod;
	}
	if (x < y) return 0;
	return fact_[x] * comp_[x - y] % Mod * comp_[y] % Mod;
}*/
//--------------------------------------------------------------
int n, m;
vec<int>e[9000000];
int a[1000000];
vec<int>d;
int solve(int pos, int t) {
	if (e[pos].size() == 0) {
		if (a[pos]) {
			if (a[pos] >= t) return 0;
			else return inf;
		}
		else return 1;
	}
	vec<int>b;
	for (auto to : e[pos]) b.pb(solve(to, t));
	sort(all(b));
	return b[0] + b[1];
}
signed main() {
	cin >> n >> m;
	rep(i, m) {
		int u, v; cin >> u >> v;
		a[v - 1] = u;
	}
	rep(i, n - m) d.pb(read());
	deque<int>dq;
	rep(i, n) dq.push_back(i);
	int idx = n;
	while (dq.size() > 1) {
		e[idx].pb(dq.front()); dq.pop_front();
		e[idx].pb(dq.front()); dq.pop_front();
		e[idx].pb(dq.front()); dq.pop_front();
		dq.push_back(idx++);
	}
	sort(all(d));
	int ok = -10, ng = inf, mid;
	while (abs(ng - ok) > 1) {
		mid = (ok + ng) / 2;
		int sum = (n - m) - (lower_bound(all(d), mid) - d.begin());
		if (solve(idx - 1, mid) <= sum) ok = mid;
		else ng = mid;
	}
	cout << ok << endl;
}
accuracy: 0.5882352941176471
time_ms: 70
memory_kb: 214232
final_rank: 20

code_snippet: #pragma GCC optimize ("Ofast")
#pragma GCC target ("avx")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<unordered_map>
#include<climits>
#include<fstream>
#include<complex>
#include<time.h>
#include<cassert>
#include<functional>
#include<numeric>
#include<tuple>

using namespace std;
using ll = long long;
using ld = long double;
#define int long long
#define all(a) (a).begin(),(a).end()
#define fs first
#define sc second
#define xx first
#define yy second.first
#define zz second.second
#define H pair<int, int>
#define P pair<int, pair<int, int>>
#define Q(i,j,k) mkp(i,mkp(j,k))
#define R pair<pair<int, int>, pair<int, int>>
#define S(i,j,k,l) mkp(mkp(i,j),mkp(k,l))
#define rep(i,n) for(int (i) = 0 ; (i) < (n) ; (i)++)
#define rng(i,s,n) for(int (i) = (s) ; (i) < (n) ; (i)++)
#define req(n,i) for(int (i) = (n) - 1 ; (i) >= 0 ; (i)--)
#define range(i,v) for(auto& (i) : v)
#define mkp make_pair
#define mem(x,k) memset(x,k,sizeof(x))
#define vec vector
#define pb emplace_back
#define lb lower_bound
#define ub upper_bound
#define crdcomp(b) sort(all((b)));(b).erase(unique(all((b))),(b).end())
#define getidx(b,i) lower_bound(all(b),(i))-b.begin()
#define ssp(i,n) (i==n-1?"\n":" ")
#define ctoi(c) (int)(c-'0')
#define itoc(c) (char)(c+'0')
#define itn int
#define pri_que priority_queue
#define pp(x,y) pb(H{x,y})
#define ppp(x,y,z) pb(Q(x,y,z))
#define pppp(x,y,z,w) pb(S(x,y,z,w))
#define cyes cout<<"Yes\n"
#define cno cout<<"No\n"
#define endl "\n"
constexpr int mod = 1e9 + 7;
constexpr int Mod = 998244353;
constexpr int MXN = 500000 + 100;
constexpr ll inf = 3 * 1e18;
constexpr int Inf = 15 * 1e8;
const vec<int>dx{ -1,1,0,0 }, dy{ 0,0,-1,1 };
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
ll read() { ll u, k = scanf("%lld", &u); return u; }
string reads() { string s; cin >> s; return s; }
H readh(bool g = 0) { H u; ll k = scanf("%lld %lld", &u.fs, &u.sc); if (g) u.fs--, u.sc--; return u; }
void printh(H t) { printf("%lld %lld\n", t.fs, t.sc); }
bool inarea(H t, int h, int w) { return 0 <= t.fs && t.fs < h && 0 <= t.sc && t.sc < w; }
ll gcd(ll i, ll j) { return j ? gcd(j, i % j) : i; }
ll mod_pow(ll x, ll n, ll p = mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x % p;
		x = x * x % p;
		n >>= 1;
	}
	return res;
}//x^n%p
ll bitcount(ll x) {
	int sum = 0; for (int i = 0; i < 60; i++)if ((1ll << i) & x) sum++;
	return sum;
}
/*constexpr int fn_ = 1000005;
ll fact_[fn_], comp_[fn_];
ll comb(ll x, ll y, ll Mod = mod) {
	if (!fact_[0]) {
		fact_[0] = 1; comp_[0] = 1;
		for (int i = 1; i < fn_; i++)
			fact_[i] = fact_[i - 1] * i % Mod;
		comp_[fn_ - 1] = mod_pow(fact_[fn_ - 1], Mod - 2, Mod);
		for (int i = fn_ - 2; i > 0; i--)
			comp_[i] = comp_[i + 1] * (i + 1) % Mod;
	}
	if (x < y) return 0;
	return fact_[x] * comp_[x - y] % Mod * comp_[y] % Mod;
}*/
//--------------------------------------------------------------
int n, m;
vec<int>e[3000000];
int a[1000000];
vec<int>d;
int solve(int pos, int t) {
	if (e[pos].size() == 0) {
		if (a[pos]) {
			if (a[pos] >= t) return 0;
			else return inf;
		}
		else return 1;
	}
	vec<int>b;
	for (auto to : e[pos]) b.pb(solve(to, t));
	sort(all(b));
	return b[0] + b[1];
}
signed main() {
	cin >> n >> m;
	rep(i, m) {
		int u, v; cin >> u >> v;
		a[v - 1] = u;
	}
	rep(i, n - m) d.pb(read());
	deque<int>dq;
	rep(i, n) dq.push_back(i);
	int idx = n;
	while (dq.size() > 1) {
		e[idx].pb(dq.front()); dq.pop_front();
		e[idx].pb(dq.front()); dq.pop_front();
		e[idx].pb(dq.front()); dq.pop_front();
		dq.push_back(idx++);
	}
	sort(all(d));
	int ok = -10, ng = inf, mid;
	while (abs(ng - ok) > 1) {
		mid = (ok + ng) / 2;
		int sum = (n - m) - (lower_bound(all(d), mid) - d.begin());
		if (solve(idx - 1, mid) <= sum) ok = mid;
		else ng = mid;
	}
	cout << ok << endl;
}
accuracy: 0.5882352941176471
time_ms: 30
memory_kb: 73564
final_rank: 18

code_snippet: #include <bits/stdc++.h>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT_ON(bit, i) (((bit >> i) & 1) > 0)
#define BIT_COUNT(bit) (__builtin_popcount(bit))

typedef long long LL;
template<typename T> std::vector<T> make_v(size_t a){return std::vector<T>(a);}
template<typename T,typename... Ts> auto make_v(size_t a, Ts... ts){ return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}    // C++14
template<typename T,typename V> typename std::enable_if<std::is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename std::enable_if<std::is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
inline bool inside(long long y, long long x, long long H, long long W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }
template<typename T> T &chmin(T &a, const T &b) { return a = std::min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = std::max(a, b); }

template<class T> inline std::vector<T> unique(std::vector<T> v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    return v;
}
// 初項s, 交差dのn個の数列の和
long long sum_of_arithmetic_progression(long long s, long long d, long long n) {
    return n * (2 * s + (n - 1) * d) / 2;
}

// 2のべき乗数かどうか
bool is_power_of_two(long long x) {
    return !(x & (x - 1));
}

// aとbの最大公約数 O(log min(a, b))
long long gcd(long long a, long long b) {
    if (b == 0) { return a; }
    return gcd(b, a % b);
}

// 数列vの最大公約数 O(N log d)
long long gcd(std::vector<long long> &v) {
    long long ans = v[0];
    for (int i = 1; i < (int)v.size(); ++i) {
        ans = gcd(ans, v[i]);
    }
    return ans;
}

// aとbの最小公倍数 O(log min(a, b))
long long lcm(long long a, long long b) {
    long long g = gcd(a, b);
    return a / g * b;
}

const int INF = 1u << 30u;
const long long LINF = 1ull << 58u;
const double EPS = 1e-9;
const long double PI = acos(-1.0);
const std::vector<int> dy2 = {0, 1}, dx2 = {1, 0};  // 右，下
const std::vector<int> dy4 = {0, 1, 0, -1}, dx4 = {1, 0, -1, 0};    // 右，下，左，上
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;


bool ok(const long long x, const vector<LL> &fix, const vector<LL> &v) {
    auto dp = make_v<LL>(fix.size());
    FOR(i, 0, fix.size()) {
        if (fix[i] == 0) {
            dp[i] = 1;
        }
        else if (fix[i] >= x) {
            dp[i] = 0;
        }
        else {
            dp[i] = INF;
        }
    }

    int i = 0;
    while (i + 2 < dp.size()) {
        LL n = dp[i] + dp[i + 1] + dp[i + 2] - max({dp[i], dp[i + 1], dp[i + 2]});
        i += 3;
        dp.emplace_back(n);

        if (i >= dp.size()) {
            break;
        }
    }

    LL num_need_high = dp.back();
    int num_high = 0;
    FOE(a, v) {
        num_high += a >= x;
    }

    return num_high >= num_need_high;
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    LL N, M;
    cin >> N >> M;

    auto fix = make_v<LL>(N);
    FOR(i, 0, M) {
        LL D, P;
        cin >> D >> P;
        P--;
        fix[P] = D;
    }

    auto v = make_v<LL>(N - M);
    FOR(i, 0, N - M) {
        cin >> v[i];
    }

    long long low = -1;
    long long high = 10000000100ll;
    while (high - low > 1) {
        long long middle = (low + high) / 2;
        if (ok(middle, fix, v)) {
            low = middle;
        }
        else {
            high = middle;
        }
    }
    print(low);

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 6336
final_rank: 2

code_snippet: #include <bits/stdc++.h>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT_ON(bit, i) (((bit >> i) & 1) > 0)
#define BIT_COUNT(bit) (__builtin_popcount(bit))

typedef long long LL;
template<typename T> std::vector<T> make_v(size_t a){return std::vector<T>(a);}
template<typename T,typename... Ts> auto make_v(size_t a, Ts... ts){ return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}    // C++14
template<typename T,typename V> typename std::enable_if<std::is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename std::enable_if<std::is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
inline bool inside(long long y, long long x, long long H, long long W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }
template<typename T> T &chmin(T &a, const T &b) { return a = std::min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = std::max(a, b); }

template<class T> inline std::vector<T> unique(std::vector<T> v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    return v;
}
// 初項s, 交差dのn個の数列の和
long long sum_of_arithmetic_progression(long long s, long long d, long long n) {
    return n * (2 * s + (n - 1) * d) / 2;
}

// 2のべき乗数かどうか
bool is_power_of_two(long long x) {
    return !(x & (x - 1));
}

// aとbの最大公約数 O(log min(a, b))
long long gcd(long long a, long long b) {
    if (b == 0) { return a; }
    return gcd(b, a % b);
}

// 数列vの最大公約数 O(N log d)
long long gcd(std::vector<long long> &v) {
    long long ans = v[0];
    for (int i = 1; i < (int)v.size(); ++i) {
        ans = gcd(ans, v[i]);
    }
    return ans;
}

// aとbの最小公倍数 O(log min(a, b))
long long lcm(long long a, long long b) {
    long long g = gcd(a, b);
    return a / g * b;
}

const int INF = 1u << 30u;
const long long LINF = 1ull << 58u;
const double EPS = 1e-9;
const long double PI = acos(-1.0);
const std::vector<int> dy2 = {0, 1}, dx2 = {1, 0};  // 右，下
const std::vector<int> dy4 = {0, 1, 0, -1}, dx4 = {1, 0, -1, 0};    // 右，下，左，上
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;


bool ok(const long long x, const vector<LL> &fix, const vector<LL> &v) {
    auto dp = make_v<LL>(fix.size());
    FOR(i, 0, fix.size()) {
        if (fix[i] == 0) {
            dp[i] = 1;
        }
        else if (fix[i] >= x) {
            dp[i] = 0;
        }
        else {
            dp[i] = LINF;
        }
    }

    int i = 0;
    while (i + 2 < dp.size()) {
        LL n = dp[i] + dp[i + 1] + dp[i + 2] - max({dp[i], dp[i + 1], dp[i + 2]});
        i += 3;
        dp.emplace_back(n);

        if (i >= dp.size()) {
            break;
        }
    }

    LL num_need_high = dp.back();
    int num_high = 0;
    FOE(a, v) {
        num_high += a >= x;
    }

    return num_high >= num_need_high;
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    LL N, M;
    cin >> N >> M;

    auto fix = make_v<LL>(N);
    FOR(i, 0, M) {
        LL D, P;
        cin >> D >> P;
        P--;
        fix[P] = D;
    }

    auto v = make_v<LL>(N - M);
    FOR(i, 0, N - M) {
        cin >> v[i];
    }

    long long low = -1;
    long long high = 10000000100ll;
    while (high - low > 1) {
        long long middle = (low + high) / 2;
        if (ok(middle, fix, v)) {
            low = middle;
        }
        else {
            high = middle;
        }
    }
    print(low);

    return 0;
}
accuracy: 0.8235294117647058
time_ms: 20
memory_kb: 6432
final_rank: 17

code_snippet: #include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

ll N;
ll M;
ll D[100005];
ll pos[100005];
ll d_max = -1;
ll d_min = LLONG_MAX;

bool is_higher(ll val){
	queue<ll> que;

	ll cnt = 0;
	for (int i = M;i < N;i++){
		if(D[i] >= val) cnt++;
	}

	for (int i = (N/3)*3;i < N;i++){
		if(pos[i] == -1) que.push(1);
		else if(D[pos[i]] < val) que.push(INT_MAX/10);
		else que.push(0);
	}

	int itr;
	for (itr = 0;itr < N;itr += 3 ){
		if(itr+2 >= N) break;
		ll cnth = 0;
		ll cntl = 0;
		for (int j = 0;j < 3;j++){
			if(pos[itr+j] == -1) continue;
			ll tmp = D[pos[itr+j]];
			if(tmp >= val) cnth++;
			else cntl++;
		}
		if(cntl+cnth == 0) que.push(2);
		else if(cntl+cnth == 1){
			if(cntl == 1) que.push(2);
			else que.push(1);
		}
		else if(cntl+cnth == 2){
			if(cntl == 2) que.push(INT_MAX/10);
			else if(cnth == 2) que.push(0);
			else que.push(1);
		}
		else {
			if(cntl == 2 || cntl == 3) que.push(INT_MAX/10);
			else que.push(0);
		}

	}

	while(que.size() != 1){
		ll tr[3];
		for (int i = 0;i < 3;i++){
			tr[i] = que.front(); que.pop();
		}
		que.push(min(tr[0]+tr[1],min(tr[1]+tr[2],tr[2]+tr[0])));
	}
	
	return (que.front() <= cnt);

}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N >> M;

	memset(pos, -1, sizeof(pos));

	for (int i = 0;i < M;i++){
		int tmp;
		cin >> D[i] >> tmp;
		tmp--;
		pos[tmp] = i;
		d_max = max(d_max, D[i]);
		d_min = min(d_min, D[i]);
	}

	for (int i = M;i < N;i++){
		cin >> D[i];	
		d_max = max(d_max, D[i]);
		d_min = min(d_min, D[i]);
	}

	// [low, high)
	ll low = d_min;
	ll high = d_max+1;

	while(high-low > 1){
		ll mid = (low+high)/2;
		if(is_higher(mid)) low = mid;
		else high = mid;
	}

	cout << low << endl;

	return 0;

}
accuracy: 1
time_ms: 30
memory_kb: 4904
final_rank: 4

code_snippet: //1:23
#include<iostream>
#include<algorithm>
#include<queue>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;

using namespace std;
#define N 100010
#define T 150010
ll n,m,a[N],b[N],last;
vector<ll> g[T];
ll dfs(int x,ll lim){
    if(x<n){
	if(a[x]==0)return 1;
	if(a[x]>=lim)return 0;
	return mod;
    }
    ll sum=0,maxi=0;
    for(auto y:g[x]){
	ll t=dfs(y,lim);
	sum+=t,maxi=max(maxi,t);
    }
    return sum-maxi;
}
bool DP(ll lim){
    ll cnt=0;
    lol(i,n-m)if(b[i]>=lim)cnt++;
    return (cnt>=dfs(last-1,lim));
}
int main(){
    cin>>n>>m;
    lol(i,n)a[i]=0;
    lol(i,m){
	int x,y;
	cin>>x>>y;y--;
	a[y]=x;
    }
    lol(i,n-m)cin>>b[i];
    queue<int> Q;
    lol(i,n)Q.push(i);
    last=n;
    while(Q.size()>=3){
	lol(i,3){
	    g[last].push_back(Q.front());
	    Q.pop();
	}
	Q.push(last);
	last++;
    }
    ll l=0,r=mod,mid;
    while(l<r-1){
	mid=(l+r)/2;
	if(DP(mid))l=mid;
	else r=mid;
    }
    cout<<l<<endl;
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 10588
final_rank: 9

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 500000

enum Type{
	HIGH,
	LOW,
	UNDEFINED,
};

struct Info{
	int value,loc;
};

struct Data{
	Type type;
	int need_num;
};

int N,M,length,root;
int base[100000];
int parent[NUM];
Info info[100000];
Data data[NUM];

bool is_OK(int base_value){

	int count = 0;
	for(int i = 0; i < N; i++){
		if(info[i].loc == -1 && info[i].value >= base_value)count++;
	}

	for(int i = 0; i < length; i++){
		data[i].type = UNDEFINED;
		data[i].need_num = 1;
	}

	for(int i = 0; i < N; i++){
		if(base[i] != -1){
			if(base[i] >= base_value){
				data[i].type = HIGH;
			}else{
				data[i].type = LOW;
			}
		}
	}

	int loc[3] = {0,1,2};
	int num_HIGH,num_LOW,add;
	int work[3],sum;

	while(true){

		num_HIGH = 0,num_LOW = 0;

		for(int i = 0; i < 3; i++){
			if(data[loc[i]].type == HIGH){
				num_HIGH++;
			}else if(data[loc[i]].type == LOW){
				num_LOW++;
			}
		}

		if(num_HIGH >=2){

			data[parent[loc[0]]].type = HIGH;
			data[parent[loc[0]]].need_num = 0;

		}else if(num_LOW >= 2){

			data[parent[loc[0]]].type = LOW;
			data[parent[loc[0]]].need_num = 0;

		}else{
			add = 2-num_HIGH;

			for(int i = 0; i < 3; i++)work[i] = BIG_NUM;

			for(int i = 0; i < 3; i++){
				if(data[loc[i]].type == UNDEFINED){
					work[i] = data[loc[i]].need_num;
				}
			}

			sort(work,work+3);

			sum = 0;
			for(int i = 0; i < add; i++)sum += work[i];

			data[parent[loc[0]]].need_num = sum;
		}

		if(parent[loc[0]] == root)break;

		for(int i = 0; i < 3; i++){
			loc[i] += 3;
		}
	}

	if(data[root].type == HIGH)return true;
	if(data[root].type == LOW)return false;

	return count >= data[root].need_num;
}

int main(){

	scanf("%d %d",&N,&M);

	for(int i = 0; i < N; i++){
		base[i] = -1;
		info[i].loc = -1;
	}

	int maximum = 0,minimum = BIG_NUM;

	for(int i = 0; i < M; i++){
		scanf("%d %d",&info[i].value,&info[i].loc);
		info[i].loc--;
		base[info[i].loc] = info[i].value;
		maximum = max(maximum,info[i].value);
		minimum = min(minimum,info[i].value);
	}

	for(int i = 0; i < N-M; i++){
		scanf("%d",&info[M+i].value);
		maximum = max(maximum,info[M+i].value);
		minimum = min(minimum,info[M+i].value);
	}

	for(int i = 0; i < NUM; i++){
		parent[i] = -1;
	}

	int index = N;
	queue<int> Q;
	for(int i = 0; i < N; i++){
		Q.push(i);
	}

	int A,B,C;

	while(Q.size() != 1){
		A = Q.front();
		Q.pop();
		B = Q.front();
		Q.pop();
		C = Q.front();
		Q.pop();

		parent[A] = index;
		parent[B] = index;
		parent[C] = index;

		Q.push(index);
		index++;
	}

	length = index;
	index--;
	root = index;

	int left = minimum,right = maximum,m = (left+right)/2;
	int ans = minimum;

	while(left <= right){
		if(is_OK(m)){
			ans = m;
			left = m+1;
		}else{
			right = m-1;
		}
		m = (left+right)/2;
	}

	printf("%d\n",ans);

	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 7700
final_rank: 8

code_snippet: # include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <tuple>
# include <unordered_map>
# include <numeric>
# include <complex>
# include <bitset>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
constexpr long long MMOD = 500000004;
const double PI = acos(-1);
int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
# define ALL(x)     (x).begin(),(x).end()
# define UNIQ(c)    (c).erase(unique(ALL((c))), end((c)))
# define mp         make_pair
# define eb         emplace_back
# define FOR(i,a,b) for(int i=(a);i<(b);++i)
# define REP(i,n)   FOR(i,0,n)
# define INIT       std::ios::sync_with_stdio(false);std::cin.tie(0);


int n, m;
int member[101010];
vector<int> other;
int dp[202020];

bool C(int mid){

	for (int i = 0; i < 202020; i++)dp[i] = HINF;

	for (int i = 0; i < n; i++) {
		dp[i] = (~member[i] ? (member[i] >= mid ? 0 : HINF) : 1);
	}

	int count = 0;
	for (int i = 0;; i++) {
		dp[i + n] = min({ dp[i * 3] + dp[i * 3 + 1],dp[i * 3] + dp[i * 3 + 2],dp[i * 3 + 1] + dp[i * 3 + 2] });
		dp[i + n] = min(dp[i + n], HINF);
		count = dp[i + n];
		if (i * 3 + 3 == i + n) break;
	}

	int count2 = 0;

	for (int i = 0; i < other.size(); i++) {
		count2 += other[i] >= mid;
	}

	return (count <= count2);
}

int main(){
	cin >> n >> m;
	
	for (int i = 0; i < 101010; i++)member[i] = -1;

	for (int i = 0; i < m; i++) {
		int x, y;
		cin >> x >> y;
		member[--y] = x;
	}

	for (int i = 0; i < n - m; i++) {
		int x;
		cin >> x;
		other.emplace_back(x);
	}

	int left = 1, right = 1000000000;

	while (left != right) {
		int mid = (left + right + 1)>>1;

		if (C(mid)) left = mid;
		else right = mid - 1;
	}

	cout << left << endl;

}
accuracy: 1
time_ms: 50
memory_kb: 4560
final_rank: 7

code_snippet: #include <bits/stdc++.h>
#define INF 1e+10
#define int long long
using namespace std;

int n,m,a[100000];
vector<int> nums;

bool C(int x){
	queue<int> que;
	for(int i = 0;i < n;i++){
		if(!a[i]) que.push(1);
		else if(a[i] < x) que.push(INF);
		else que.push(0);
	}
	for(int i = 0;i < (n - 1) / 2;i++){
		int b[3],ma = 0,sum = 0;
		for(int j = 0;j < 3;j++){
			b[j] = que.front();
			que.pop();
			ma = max(ma,b[j]);
			sum += b[j];
		}
		que.push(sum - ma);
	}
	int cnt = n - m - (lower_bound(nums.begin(),nums.end(),x) - nums.begin());
	return que.front() <= cnt;
}

signed main(){
	cin >> n >> m;
	for(int i = 0;i < n;i++){
		if(i < m){
			int x,y;
			cin >> x >> y;
			a[y - 1] = x;
		}else{
			int x;
			cin >> x;
			nums.push_back(x);
		}
	}
	sort(nums.begin(),nums.end());
	int low = 0,up = INF;
	while(up - low > 1){
		int mid = (low + up) / 2;
		if(C(mid)) low = mid;
		else up = mid;
	}
	for(int i = 0;i <= 10;i++) C(i);
	cout << low << endl;
	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 5124
final_rank: 11

code_snippet: #include <cstdio>
int N, M, p, d, val[100009], a[100009], dp[150009];
int main() {
	scanf("%d %d", &N, &M);
	for (int i = 0; i < M; i++) scanf("%d %d", &d, &p), val[p - 1] = d;
	for (int i = 0; i < N - M; i++) scanf("%d", &a[i]);
	int l = 0, r = 1000000009;
	while (r - l > 1) {
		int m = (l + r) >> 1; p = 1;
		for (int i = 0; i < N - M; i++) {
			if (a[i] >= m) p++;
		}
		for (int i = 0; i < N; i++) dp[i] = (val[i] ? (val[i] >= m ? 0 : p) : 1);
		for (int i = 0; 2 * i < N; i++) {
			d = dp[i + N] = 0;
			for (int j = i * 3; j < i * 3 + 3; j++) dp[i + N] += dp[j], d = d > dp[j] ? d : dp[j];
			dp[i + N] -= d;
		}
		if (dp[N + (N >> 1) - 1] < p) l = m;
		else r = m;
	}
	printf("%d\n", l);
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3968
final_rank: 3

code_snippet: /*input
15 1
781314749 1
312224898
269509097
255956030
898895737
228424650
553611871
905685626
332572742
398251511
103523987
310943411
741678005
214559293
439294954
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
#define sp ' '
#define endl '\n'
#define fi first
#define se second
#define mp make_pair
#define int long long
#define N 200005
#define bit(x,y) ((x>>y)&1LL)
#define show(x) cout << (#x) << ": " << x << endl;
#define ii pair<int,int>
ostream& operator << (ostream &os, vector<int>&x) {
	for (int i = 0; i < x.size(); i++) os << x[i] << sp;
	return os;
}
ostream& operator << (ostream &os, pair<int, int> x) {
	cout << x.fi << sp << x.se << sp;
	return os;
}
ostream& operator << (ostream &os, vector<pair<int, int> >&x) {
	for (int i = 0; i < x.size(); i++) os << x[i] << endl;
	return os;
}

int n, m;
int peak;
vector<vector<int> > a(N);
int root = 0;
int cnt[2];
vector<int> allval;
int valPos[N];
vector<vector<int> > dp;
void addEdge(int x, int y) {
	a[x].push_back(y);
}

void createEdge(vector<int> cur) {
	if (cur.size() == 1) {
		root = cur.front();
		return;
	}
	vector<int> nxt;
	while (cur.size() % 3 != 0) {
		nxt.push_back(cur.back());
		cur.pop_back();
	}
	reverse(cur.begin(), cur.end());
	while (!cur.empty()) {
		nxt.push_back(++peak);
		for (int i = 1; i <= 3; i++) {
			addEdge(peak, cur.back()); cur.pop_back();
		}
	}
	createEdge(nxt);
}

int cal(int u, int wh, const int lim) {
	int &ret = dp[u][wh];
	if (ret != -1) return ret;
	ret = 1e18;
	if (u <= n) {
		if (valPos[u] == -1) return ret = wh;
		int x; if (valPos[u] < lim) x = 0; else x = 1;
		if (wh == x) return ret = wh;
		else return ret;
	}
	else {
		if (a[u].size() == 1)
			return ret = cal(a[u].front(), wh, lim);
		for (int mask = 0; mask < (1 << 3); mask++) {
			int num1 = __builtin_popcount(mask); int num0 = 3 - num1;
			if ((wh == 0 && num0 > num1) || (wh == 1 && num0 < num1)) {
				int sum = 0;
				for (int i = 0; i < 3; i++)
					sum += cal(a[u][i], bit(mask, i), lim);
				ret = min(ret, sum);
			}
		}
		return ret;
	}
}

bool check(int lim) {
	memset(cnt, 0, sizeof(cnt));
	for (auto u : allval) {
		if (u < lim) cnt[0]++;
		else cnt[1]++;
	}
	dp.assign(root + 5, vector<int>(2, -1));
	int rec = cal(root, 1, lim);
	return rec <= cnt[1];
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m; peak = n;
	vector<int> sta; for (int i = 1; i <= n; i++) sta.push_back(i);
	createEdge(sta); memset(valPos, -1, sizeof(valPos));
	for (int i = 1; i <= m; i++) {
		int x, y; cin >> x >> y;
		valPos[y] = x; allval.push_back(x);
	}
	for (int i = 1; i <= n - m; i++) {
		int x; cin >> x; allval.push_back(x);
	}
	sort(allval.begin(), allval.end());
	int l = 0, r = allval.size() - 1;
	while (l != r) {
		int mid = (l + r + 1) / 2;
		if (check(allval[mid])) l = mid;
		else r = mid - 1;
	}
	cout << allval[l] << endl;
}
accuracy: 1
time_ms: 160
memory_kb: 21148
final_rank: 14

code_snippet: #include<bits/stdc++.h>
const long long N = 2e5 + 5;
const long long inf = 1e9;
using namespace std;

vector <long long> adj[N];
long long n, m, d[N], p[N], cnt = 1, c;
long long num[N], dp[N][2];
long long pos[N];

void dfs(long long u, long long numm){
    num[u] = 0;
    for (long long i = 0; i < adj[u].size(); i++){
        long long v = adj[u][i];
        dfs(v, numm);
        num[u] += num[v];
    }
    if (adj[u].size() == 0){
        num[u] = 1;
        dp[u][0] = ((pos[u] && !(pos[u] < numm)) ? inf : 0); dp[u][1] = ((pos[u] && !(pos[u] >= numm)) ? inf : 1);
    }
    else{
        for (long long i = 0; i < 8; i++){
            bool x = (i & 1), y = (i&2), z = (i&4);
            if (__builtin_popcount(i) > 1) dp[u][1] = min(dp[u][1], dp[adj[u][0]][x] + dp[adj[u][1]][y] + dp[adj[u][2]][z]);
            else dp[u][0] = min(dp[u][0], dp[adj[u][0]][x] + dp[adj[u][1]][y] + dp[adj[u][2]][z]);
        }
    }
    if (dp[u][0] != inf && num[u] - dp[u][0] > cnt) dp[u][0] = num[u] - cnt;
}

bool check(long long num){
    cnt = 0;
    for (long long i = 1; i <= n; i++) if (d[i] < num) cnt++;
    for (int i = 1; i <= c; i++) dp[i][0] = inf, dp[i][1] = inf;
    dfs(c, num);
    return (dp[c][1] <= n-cnt);
}

long long bsearch(){
    long long l = 0, r = 1e9;
    while (l != r){
        long long mid = (l + r + 1) / 2;
        if (check(mid)) l = mid;
        else r = mid-1;
    }
    return l;
}

int main(){
    ios_base::sync_with_stdio(false); cin .tie(0); cout.tie(0);
    cin >> n >> m;
    c = n+1;
    for (long long i = 1; i <= m; i++) {
        cin >> d[i] >> p[i];
        pos[p[i]] = d[i];
    }
    for (long long i = m+1; i <= n; i++) cin >> d[i];
    while (cnt+1 != c){
        adj[c].push_back(cnt); cnt++;
        adj[c].push_back(cnt); cnt++;
        adj[c].push_back(cnt); cnt++;
        c++;
    }
    c--;
    cout << bsearch() << "\n";
}
accuracy: 1
time_ms: 120
memory_kb: 15912
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int N = 200005;
const long long inf = 1e9;

vector <int> G[N];
int n, m, d[N], a[N], b[N];
long long f[N][2];
int cnt;

int mex(int mask) {
	int r = 0; for (int i = 0; i < 3; ++i) if (mask & (1<<i)) ++r; return r >= 2;
}

void dfs(int u) {
	if (G[u].size() == 0) {
		if (b[u] == -1) f[u][0] = 0, f[u][1] = 1;
		else {
			if (b[u]) f[u][1] = 1, f[u][0] = inf;
			else f[u][0] = 0, f[u][1] = inf;
		}
		return;
	}
	f[u][0] = f[u][1] = inf;
	for (int v: G[u]) dfs(v);
	for (int mask = 0; mask < 8; ++mask) {
		f[u][mex(mask)] = min(f[u][mex(mask)], f[G[u][0]][mask >> 0 & 1] + f[G[u][1]][mask >> 1 & 1] + f[G[u][2]][mask >> 2 & 1]);
	}
}

bool check(int x) {
	memset(b, -1, sizeof b); int cnt1 = 0;
	for (int i = 1; i <= n; ++i) if (a[i]) { b[i] = a[i] >= x; if (a[i] >= x) ++cnt1; }
	for (int i = 1; i <= n - m; ++i) if (d[i] >= x) ++cnt1; 
	dfs(cnt);
	return f[cnt][1] <= cnt1;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= m; ++i) { int p; cin >> d[i] >> p; a[p] = d[i]; }
	for (int i = 1; i <= n - m; ++i) cin >> d[i];

	int cur = n; cnt = n;
	vector<int> nw, nxt;
	for (int i = 1; i <= n; ++i) nw.push_back(i);
	while(cur > 1) {
		for (int i = 0; i < (int)nw.size(); i += 3) {
			if (i > (int)nw.size() - 3) continue;
			++cnt; G[cnt].push_back(nw[i]); G[cnt].push_back(nw[i + 1]); G[cnt].push_back(nw[i + 2]);
			nxt.push_back(cnt);
		}
		reverse(nxt.begin(), nxt.end());
		for (int i = (int)nw.size()-1; i >= 0; --i) {
			if (i % 3 == 2) break;
 			nxt.push_back(nw[i]);
		}
		reverse(nxt.begin(), nxt.end());
		nw = nxt;
		nxt.clear();
		cur = (int)nw.size();
	}
	
	int l = 0, r = 1e9;
	while(l < r) {
		int mid = ((l + r + 1) >> 1);
		if (check(mid)) l = mid; else r = mid - 1;
	}
	cout << l << endl;
}
accuracy: 1
time_ms: 140
memory_kb: 13552
final_rank: 13