Problem:
ネットカフェ
あなたはネットカフェを経営しています。今日あなたは、顧客に指摘され続けている問題を解決しようと取り組んでいます。その問題とは、店舗の本棚の単行本が巻数順に並んでおらず、目的の単行本を探しだすのが面倒だという苦情です。
あなたの店舗で一番巻数の多い単行本は「名探偵 赤ベコ」（通称「赤ベコ」）です。あまりに長編なので、特別な本棚を「赤ベコ」のために用意しました。
単行本の各巻の重さと厚さは様々で、本棚の各段の幅と、各段に並べることができる本の重さの上限も様々です。あなたは、次の条件を満足するように本棚に本を並べることにしました。
1 巻からある巻までの「赤ベコ」が本棚に並んでいる。
それぞれの段には、巻数順に（途中で抜けている巻がないように）本が並ぶ。
各段に並べる本の重さの合計が、その段で定められた重さの上限を超えない。
各段に並べる本の厚さの合計が、その段の幅を超えない。
これらの条件を満たしたとき，この本棚に最大で何巻まで「赤ベコ」を並べることができるかを求めるプログラムを作成してください。
入力
入力は以下の形式で与えられる。
M
N
w
1
t
1
w
2
t
2
:
w
M
t
M
c
1
b
1
c
2
b
2
:
c
N
b
N
最初の１行に「赤ベコ」の巻数
M
(1 ≤
M
≤ 200000) と本棚の段数
N
(1 ≤
N
≤ 15) が与えられる。続く
M
行に、「赤ベコ」の単行本
i
巻目の重さ
w
i
(1 ≤
w
i
≤ 100) と厚さ
t
i
(1 ≤
t
i
≤ 100) を表す整数が与えられる。続く
N
行に、本棚の
i
段目の重さの上限
c
i
(1 ≤
c
i
≤ 10
8
)と幅
b
i
(1 ≤
b
i
≤ 10
8
) を表す整数が与えられる。
出力
本棚に並べることができる最大の「赤ベコ」巻数を１行に出力する。
入出力例
入力例１
3 4
2 2
3 3
4 4
3 3
4 4
1 1
2 2
出力例１
3
入力例２
2 2
1 2
2 1
2 1
2 1
出力例２
0
入力例３
3 2
1 2
2 2
2 1
3 3
2 2
出力例３
2
入力例４
3 2
1 2
2 1
2 2
2 2
3 3
出力例４
3

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	int M, N;
	cin >> M >> N;
	vector<int> W(M), T(M);
	rep(i, 0, M) cin >> W[i] >> T[i];
	vector<int> C(N), B(N);
	rep(i, 0, N) cin >> C[i] >> B[i];
	
	// 0b1b2b3b4b5
	vector table(N, vector<int>(M));
	rep(i, 0, N) {
		int r = 0;
		int wei = 0, wid = 0;
		rep(l, 0, M) {
			while (r < M && wei + W[r] <= C[i] && wid + T[r] <= B[i]) {
				wei += W[r];
				wid += T[r];
				++r;
			}
			table[i][l] = r;
			
			if (l == r) ++r;
			else {
				wei -= W[l];
				wid -= T[l];
			}
		}
	}
	
	vector<int> dp(1 << N, 0);
	dp[0] = 0;
	rep(bit, 1, 1 << N) {
		rep(i, 0, N) if ((bit >> i) & 1) {
			int sub = bit ^ (1 << i);
			dp[bit] = max(dp[bit], table[i][dp[sub]]);
		}
	}
	
	cout << dp[(1 << N) - 1] << '\n';
}
accuracy: 1
time_ms: 30
memory_kb: 17144
final_rank: 7

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;
int m, n, w[200009], t[200009], sw[200009], st[200009], c[16], b[16], dp[1 << 15];
int main() {
	cin >> m >> n;
	for (int i = 0; i < m; i++) cin >> w[i] >> t[i];
	for (int i = 0; i < m; i++) {
		sw[i + 1] = sw[i] + w[i];
		st[i + 1] = st[i] + t[i];
	}
	sw[m + 1] = st[m + 1] = 999999999;
	for (int i = 0; i < n; i++) cin >> c[i] >> b[i];
	for (int i = 1; i < 1 << n; i++) {
		for (int j = 0; j < n; j++) {
			if (!(i & (1 << j))) continue;
			int val = dp[i - (1 << j)];
			int ptr1 = lower_bound(sw, sw + m + 1, c[j] + sw[val] + 1) - sw - 1;
			int ptr2 = lower_bound(st, st + m + 1, b[j] + st[val] + 1) - st - 1;
			dp[i] = max(dp[i], min(ptr1, ptr2));
		}
	}
	cout << dp[(1 << n) - 1] << endl;
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 6512
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  int M, N;
  cin >> M >> N;
  vector<int> w(M), t(M);
  for (int i = 0; i < M; i++){
    cin >> w[i] >> t[i];
  }
  vector<int> c(N), b(N);
  for (int i = 0; i < N; i++){
    cin >> c[i] >> b[i];
  }
  vector<int> Sw(M + 1);
  Sw[0] = 0;
  for (int i = 0; i < M; i++){
    Sw[i + 1] = Sw[i] + w[i];
  }
  vector<int> St(M + 1);
  St[0] = 0;
  for (int i = 0; i < M; i++){
    St[i + 1] = St[i] + t[i];
  }
  vector<int> dp(1 << N, 0);
  dp[0] = 0;
  for (int i = 0; i < (1 << N); i++){
    for (int j = 0; j < N; j++){
      if ((i >> j & 1) == 0){
        int tv = dp[i], fv = M + 1;
        while (fv - tv > 1){
          int mid = (tv + fv) / 2;
          if (Sw[mid] - Sw[dp[i]] <= c[j] && St[mid] - St[dp[i]] <= b[j]){
            tv = mid;
          } else {
            fv = mid;
          }
        }
        int i2 = i | (1 << j);
        dp[i2] = max(dp[i2], tv);
      }
    }
  }
  cout << dp[(1 << N) - 1] << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 6248
final_rank: 8

code_snippet: #include<stdio.h>
#include<iostream>
#include<math.h>

#define BIG_NUM 100000000

int pow_n[16];
int w_m[200001], t_m[200001];
int c_n[16], b_n[16];
int a[15][200001];
int dp[1 << 15];
int m, n;


int max(int a, int b)
{
	return a > b ? a : b;
}

int main()
{
	for (int i = 0; i < 16; i++) pow_n[i] = pow(2, i);
	scanf("%d%d", &m, &n);
	for (int i = 0; i < m; i++)
	{
		scanf("%d%d", &w_m[i], &t_m[i]);
	}

	int k = 0;
	int sum_w, sum_t;
	for (int i = 0; i < n; i++)
	{
		scanf("%d%d", &c_n[i], &b_n[i]);
		k = 0;
		sum_w = 0;
		sum_t = 0;
		for (int j = 0; j < m; j++)
		{
			if (j) sum_w -= w_m[j-1], sum_t -= t_m[j-1];
			while (k < m && 
				sum_w + w_m[k] <= c_n[i] &&
				sum_t + t_m[k] <= b_n[i])
			{
				sum_w += w_m[k];
				sum_t += t_m[k];
				k++;
			}
			a[i][j] = k;
		}

	}

	for (int state = 0; state < pow_n[n]; state++)
	{
		for (int i = 0; i < n; i++)
		{
			if (state & pow_n[i])
				continue;
			else
			{
				dp[state | pow_n[i]] = max(dp[state | pow_n[i]], a[i][dp[state]]);
			}
		}
	}
	printf("%d\n", dp[(1 << n) - 1]);
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 16752
final_rank: 11

code_snippet: #include<stdio.h>
#include<iostream>
#include<math.h>

#define BIG_NUM 100000000

int pow_n[16];
int w_m[200001], t_m[200001];
int c_n[16], b_n[16];
int dp[1 << 15];
int m, n;

int max(int a, int b)
{
	return a > b ? a : b;
}

int main()
{
	for (int i = 0; i < 16; i++) pow_n[i] = pow(2, i);
	scanf("%d%d", &m, &n);
	for (int i = 1; i <= m; i++)
	{
		scanf("%d%d", &w_m[i], &t_m[i]);
		w_m[i] += w_m[i - 1];
		t_m[i] += t_m[i - 1];
	}

	for (int i = 0; i < n; i++)
	{
		scanf("%d%d", &c_n[i], &b_n[i]);
	}

	int start, end, mid;
	for (int state = 0; state < pow_n[n]; state++)
	{
		start = dp[state] + 1;
		for (int i = 0; i < n; i++)
		{
			start = dp[state] + 1;
			if (start == m + 1)
			{
				printf("%d\n", m);
				return 0;
			}
			if ((state >> i) & 1)
			{

			}
			else
			{
				int left = start;
				int right = m;
				end = BIG_NUM;
				while (left <= right)
				{
					mid = (left + right) >> 1;
					if (w_m[mid] - w_m[start - 1] <= c_n[i] &&
						t_m[mid] - t_m[start - 1] <= b_n[i])
					{
						end = mid;
						left = mid + 1;
					}
					else
					{
						right = mid - 1;
					}
				}
				if (end == BIG_NUM)
					end = dp[state];
				dp[state | pow_n[i]] = max(dp[state | pow_n[i]], end);
			}

		}
	}
	printf("%d\n", dp[(1 << n) - 1]);
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5000
final_rank: 2

code_snippet: #include<iostream>
#include<algorithm>
int M,N;
int w[2<<17],t[2<<17];
int ws[2<<17],ts[2<<17];
int c[15],b[15];
int dp[1<<15];
main()
{
	std::cin>>M>>N;
	for(int i=0;i<M;i++)
	{
		std::cin>>w[i]>>t[i];
		ws[i+1]=ws[i]+w[i];
		ts[i+1]=ts[i]+t[i];
	}
	for(int i=0;i<N;i++)std::cin>>c[i]>>b[i];
	for(int i=0;i<1<<N;i++)
	{
		int L=dp[i];
		for(int j=0;j<N;j++)if(!(i>>j&1))
		{
			int rL=L,rR=M+1;
			while(rR-rL>1)
			{
				int mid=(rL+rR)/2;
				if(ws[mid]-ws[L]<=c[j]&&ts[mid]-ts[L]<=b[j])rL=mid;
				else rR=mid;
			}
			if(dp[i|1<<j]<rL)dp[i|1<<j]=rL;
		}
	}
	std::cout<<dp[(1<<N)-1]<<std::endl;
}
accuracy: 1
time_ms: 80
memory_kb: 6352
final_rank: 16

code_snippet: #include "bits/stdc++.h"
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FOR(i, a) for (auto &i: a)
#define ALL(obj) begin(obj), end(obj)
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define SUM(x) accumulate(ALL(x), 0LL)
#define LOWER_BOUND(A, key) distance(A.begin(), lower_bound(ALL(A), key))
#define UPPER_BOUND(A, key) distance(A.begin(), upper_bound(ALL(A), key))

using namespace std;
const int MOD    = (int)(1e9 + 7);
const int INF    = (int)(1e13 + 7);
const double EPS = 1e-14;
const double PI  = acos(-1);

int CEIL(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); } //ceil() for int
int mod(int a, int b) { return a >= 0 ? a % b : a - (b * CEIL(a, b)); }             //always return positive num
int bpm(int a, int b) {                                                             //return x^y in order(log(y))
  int res = 1;
  for (a %= MOD; b; a = a * a % MOD, b >>= 1)
    if (b & 1) res = res * a % MOD;
  return res;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int M, N;
  cin >> M >> N;
  vector<int> w(M + 1);
  vector<int> t(M + 1);
  vector<int> c(N);
  vector<int> b(N);
  rep(i, M) cin >> w[i + 1] >> t[i + 1];
  rep(i, N) cin >> c[i] >> b[i];
  rep(i, M) w[i + 1] += w[i];
  rep(i, M) t[i + 1] += t[i];
  vector<int> dp((1 << N) + 100, -INF);
  dp[0] = 0;
  REP(i, 1, (1 << N)) {
    rep(j, N) {
      if ((i >> j) & 1) {
        int prev = dp[i - (1 << j)];
        int l = prev, r = M + 1;
        while (true) {
          int mid = (l + r) / 2;
          if (mid == l or mid == r) break;
          if (w[mid] - w[prev] <= c[j] and t[mid] - t[prev] <= b[j]) {
            l = mid;
          } else {
            r = mid;
          }
        }
        dp[i] = max(dp[i], l);
      }
    }
  }
  cout << MAX(dp) << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 6396
final_rank: 3

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
#define LOCAL
using namespace std;
//using namespace atcoder;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
//#define int long long
using P = pair<int,int>;
//#########################################
void chmax(ll &a,ll b){
    if(a < b)a = b;
}
int main(){
    int m,n;cin >> m >> n;
    vector<int> w(m),t(m);
    rep(i,m)cin >> w[i] >> t[i];
    vector<int> c(n),b(n);
    rep(i,n)cin>> c[i] >> b[i];
    vector<ll> dp(1<<n);
    vector<ll> wsum(m+1),tsum(m+1);
    rep(i,m)wsum[i+1] = wsum[i] + w[i];
    rep(i,m)tsum[i+1] = tsum[i] + t[i];

    rep(mask,1<<n){
        rep(i,n){
            if(mask>>i&1)continue;
            ll bot = dp[mask]-1,top = m+1;
            while(top - bot>1){
                ll mid = (bot + top)/2;
                bool ok = (wsum[mid] - wsum[dp[mask]] <= c[i]) && (tsum[mid] - tsum[dp[mask]] <= b[i]);
                if(ok)bot = mid;
                else top = mid;
            }
            chmax(dp[mask|(1<<i)],bot);
        }
    }
    cout << dp[(1<<n)-1] << endl;
    
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 7996
final_rank: 10

code_snippet: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

using ll = long long int;

void chmax(int &a, int b){
    a = max(a,b);
}

int main(){
    int n, m;
    cin >> m >> n;
    vector<ll> w(m+1), t(m+1), c(n), b(n);
    for(int i=0;i<m;i++){
        cin >> w[i+1] >> t[i+1];
    }
    for(int i=0;i<n;i++){
        cin >> c[i] >> b[i];
    }
    for(int i=0;i<m;i++){
        w[i+1] += w[i];
        t[i+1] += t[i];
    }
    vector<int> dp(1<<n);
    for(int S=0;S<1<<n;S++){
        for(int i=0;i<n;i++){
            if((S>>i)&1) continue;
            int now = dp[S];

            int cidx = upper_bound(w.begin(), w.end(),w[now]+c[i]) - w.begin();
            int bidx = upper_bound(t.begin(), t.end(),t[now]+b[i]) - t.begin();
            chmax(dp[S|(1<<i)], min(cidx,bidx)-1);
        }
    }
    cout << dp[(1<<n)-1] << endl;
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 5892
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
#define INF 2000000000000000000

int m,n,w[202020],t[202020],c[15],b[15],dp[1<<16];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    cin>>m>>n;
    rep(i,m){
        cin>>w[i+1]>>t[i+1];
        w[i+1] += w[i]; t[i+1] += t[i];
    } 
    rep(i,n) cin>>c[i]>>b[i];

    rep(i,(1<<n)){
        rep(j,n){
            if((i >> j) & 1) continue;
            int tanaW = upper_bound(w + dp[i], w + m + 1, w[dp[i]] + c[j]) - w - 1;
            int tanaT = upper_bound(t + dp[i], t + m + 1, t[dp[i]] + b[j]) - t - 1;
            dp[i | (1 << j)] = max(dp[i | (1 << j)], min(tanaW, tanaT));
        }
    }

    cout << dp[(1 << n) - 1] << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4808
final_rank: 4

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;

template<typename T>
bool chmax(T& a, T b){
    if(a < b){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
T meguru_search(T ok, T ng, function<bool(T)> check){
    while(max(ok, ng) - min(ok, ng) > T(1)){
        T mid = (ok + ng) / T(2);
        if(check(mid)){
            ok = mid;
        }else{
            ng = mid;
        }
    }
    return ok;
}

int main(){
    int m, n;
    cin >> m >> n;

    vector<int> w(m), t(m);
    for(int i = 0 ; i < m ; ++i){
        cin >> w[i] >> t[i];
    }

    vector<int> wsum(m + 1, 0), tsum(m + 1, 0);
    for(int i = 0 ; i < m ; ++i){
        wsum[i + 1] = wsum[i] + w[i];
        tsum[i + 1] = tsum[i] + t[i];
    }

    vector<int> c(n), b(n);
    for(int i = 0 ; i < n ; ++i){
        cin >> c[i] >> b[i];
    }

    vector<int> dp(1 << n, 0);

    for(int bit = 0 ; bit < (1 << n) ; ++bit){
        for(int i = 0 ; i < n ; ++i){
            if(bit >> i & 1){
                continue;
            }

            int left = dp[bit];
            int right = meguru_search<int>(left, m + 1, [&](int mid){ return wsum[mid] - wsum[left] <= c[i] && tsum[mid] - tsum[left] <= b[i]; });

            chmax(dp[bit | (1 << i)], right);
        }
    }

    cout << dp[(1 << n) - 1] << endl;

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 6260
final_rank: 14

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template <class T, class U>
vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); }

template<class... Ts, class U>
auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); }

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

int main() {
    int m, n;
    cin >> m >> n;
    vector<int> S(m+1), T(m+1);
    for (int i = 0; i < m; ++i) {
        scanf("%d %d", &S[i+1], &T[i+1]);
        S[i+1] += S[i]; T[i+1] += T[i];
    }
    vector<int> A(n), B(n);
    for (int i = 0; i < n; ++i) {
        cin >> A[i] >> B[i];
    }
    vector<int> dp(1 << n);
    for (int i = 0; i < (1 << n); ++i) {
        for (int j = 0; j < n; ++j) {
            if(i & (1 << j)) continue;
            chmax(dp[i^(1 << j)], dp[i]);
            int ok = dp[i^(1 << j)], ng = m+1;
            while(ng-ok > 1){
                int mid = (ok+ng)/2;
                if(S[mid]-S[dp[i]] <= A[j] && T[mid]-T[dp[i]] <= B[j]) ok = mid;
                else ng = mid;
            }
            dp[i^(1 << j)] = ok;
        }
    }
    cout << dp.back() << "\n";
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4280
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
const ll LINF=0x1fffffffffffffff;
#define _name3(_1,_2,_3,name,...) name
#define _rep1(n) for(ll i=0;i<(n);++i)
#define _rep2(i,n) for(ll i=0;i<(n);++i)
#define _rep3(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(...) _name3(__VA_ARGS__,_rep3,_rep2,_rep1)(__VA_ARGS__)
#define rrep(i,a,b) for(ll i=(b)-1;i>=(a);i--)
#define each(i,a) for(auto &i:a)
#define sum(a) accumulate(range(a),0LL)
#define range(i) (i).begin(),(i).end()
template<class T> inline constexpr bool chmin(T &a,const T &b){ if(a>b){ a=b; return 1; } else return 0; }
template<class T> inline constexpr bool chmax(T &a,const T &b){ if(a<b){ a=b; return 1; } else return 0; }

int main(){
    ll m,n;
    cin>>m>>n;
    vector<pll>a(m);
    each(i, a)cin>>i.first>>i.second;
    vector<pll>b(n);
    each(i, b)cin>>i.first>>i.second;
    ll book[n][m+1]={};
    rep(n){
        ll C=b[i].first,B=b[i].second;
        ll width=0,weight=0,at=0;
        rep(j,m){
            while(at<m&&weight+a[at].first<=C&&width+a[at].second<=B){
                weight+=a[at].first;
                width+=a[at].second;
                at++;
            }
            book[i][j]=at-j;
            weight-=a[j].first;
            width-=a[j].second;
        }
    }
    ll dp[1<<n]={};
    rep(1<<n){
        rep(j,n)if((i>>j)&1^1){
            chmax(dp[i|(1<<j)],dp[i]+book[j][dp[i]]);
        }
    }
    cout<<dp[(1<<n)-1]<<endl;
}
accuracy: 1
time_ms: 70
memory_kb: 29668
final_rank: 20

code_snippet: #include <iostream>
#include <vector>

using namespace std;

struct book {
    int w;
    int t;
};

int binSearch(vector<book> &c_sum, int start, book limit) {
    int ok = start;
    int ng = c_sum.size();

    while (ng - ok > 1) {
        int mid = ok + (ng - ok) / 2;

        if (c_sum[mid].w - c_sum[start].w <= limit.w &&
            c_sum[mid].t - c_sum[start].t <= limit.t) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    return ok;
}

int main() {
    int m, n;
    cin >> m >> n;

    vector<book> c_sum(m + 1);
    for (int i = 0; i < m; i++) {
        book now;
        cin >> now.w >> now.t;

        c_sum[i + 1].w = c_sum[i].w + now.w;
        c_sum[i + 1].t = c_sum[i].t + now.t;
    }

    vector<book> shelf(n);
    for (int i = 0; i < n; i++)cin >> shelf[i].w >> shelf[i].t;

    vector<int> dp(1 << n, -1145141919);
    dp[0] = 0;
    for (int i = 1; i < 1 << n; i++) {
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                int back = i ^(1 << j);
                dp[i] = max(dp[i], binSearch(c_sum, dp[back], shelf[j]));
            }
        }
    }

    int ret = 0;
    for (int i = 0; i < 1 << n; i++)ret = max(ret, dp[i]);
    cout << ret << endl;
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 4276
final_rank: 12

code_snippet: #include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::max;
using ll = long long;

ll n, m;
ll w[200005];
ll t[200005];
ll c[16], b[16];
ll ws[200005];
ll ts[200005];
ll dp[1 << 15];

int main() {
	cin >> m >> n;
	for (int i = 0; i < m; i++) {
		cin >> w[i] >> t[i];
		
		ws[i + 1] = ws[i] + w[i];
		ts[i + 1] = ts[i] + t[i];
	}
	ws[m + 1] = ts[m + 1] = 1ll << 60ll;
	
	for (int i = 0; i < n; i++) {
		cin >> c[i] >> b[i];
	}
	
	for (int i = 0; i < (1 << n); i++) {
		for (int j = 0; j < n; j++) {
			if (i & (1 << j)) continue;
			
			ll valid = dp[i];
			ll invalid = m + 1;
			ll mid;
			
			while (invalid - valid > 1) {
				mid = (invalid + valid) / 2;
				
				if (ws[mid] - ws[dp[i]] <= c[j] &&
					ts[mid] - ts[dp[i]] <= b[j]) {
					valid = mid;
				}
				else {
					invalid = mid;
				}
			}
			
			dp[i | (1 << j)] = max(valid, dp[i | (1 << j)]);
		}
	}
	
	cout << dp[(1 << n) - 1] << endl;
	
	return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 9576
final_rank: 18

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const ll inf=174417444174;
vector<ll> wn,tn,w_s,ts;
ll cn[15],bn[15];
ll dp[1<<15]={0};
ll m,n;

ll get_ans(int count,int idx){
  ll c_max=cn[idx]+w_s[count];
  int a1=upper_bound(w_s.begin(),w_s.end(),c_max)-w_s.begin()-1;
  ll b_max=bn[idx]+ts[count];
  int a2=upper_bound(ts.begin(),ts.end(),b_max)-ts.begin()-1;
  return min(a1,a2);
}

int main(){
  cin>>m>>n;
  wn.push_back(0);
  tn.push_back(0);
  w_s.push_back(0);
  ts.push_back(0);
  for(int i=1;i<=m;i++){
    ll w,t;
    cin>>w>>t;
    wn.push_back(w);
    tn.push_back(t);
    w_s.push_back(w_s[i-1]+w);
    ts.push_back(ts[i-1]+t);
  }
  w_s.push_back(inf);
  ts.push_back(inf);

/*
  int k,cc;
  cin>>k>>cc;
  cout<<upper_bound(w_s.begin(),w_s.end(),k+w_s[cc])-w_s.begin()<<endl;
  return 0;
  */

  for(int i=0;i<n;i++){
    cin>>cn[i]>>bn[i];
  }

  for(int i=0;i<(1<<n)-1;i++){
    for(int j=0;j<n;j++){
      if(((i>>j)&1)==0){
        int nb=(i|(1<<j));
      //  cout<<i<<" "<<j<<" "<<nb<<endl;
        dp[nb]=max(dp[nb],get_ans(dp[i],j));
      }
    }
  //  cout<<i<<" "<<dp[i]<<endl;
  }
  cout<<dp[(1<<n)-1]<<endl;
}
accuracy: 1
time_ms: 90
memory_kb: 9448
final_rank: 19

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

#define int long long

int m, n;
int w[200010];
int t[200010];
int c[20];
int d[20];

int dp[(1 << 15)];

int search(int idx, int l){
    int st = l;
    int r = m+1;

    while(r-l > 1){
        int mid = (r+l)/2;
        int ww = w[mid]-w[st];
        int tt = t[mid]-t[st];
        if(ww <= c[idx] && tt <= d[idx]){
            l = mid;
        }else{
            r = mid;
        }
    }

    return l;
}

int solve(){

    dp[0] = 0;

    for(int i = 0;i < (1 << n);i++){
        dp[i] = -1;
        for(int j = 0;j < n;j++){
            if((i & (1 << j)) != 0){
                dp[i] = max(dp[i], search(j, dp[i^(1 << j)]));
            }
        }
    }

    return dp[(1 << n)-1];
}

signed main(){
    cin >> m >> n;
    for(int i = 0;i < m;i++){
        cin >> w[i+1] >> t[i+1];
        w[i+1] += w[i];
        t[i+1] += t[i];
    }

    for(int i = 0;i < n;i++){
        cin >> c[i] >> d[i];
    }

    cout << solve() << endl;

    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 6444
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <array>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>

int main() {
	std::cin.tie(0); std::cin.sync_with_stdio(false);
	int m, n; std::cin >> m >> n;
	std::vector<long long int> weights(m + 1, 0), thicks(m + 1, 0);
	for (auto i = 1; i <= m; ++i) std::cin >> weights[i] >> thicks[i];
	for (auto i = 1; i <= m; ++i) {
		weights[i] += weights[i - 1];
		thicks[i] += thicks[i - 1];
	}
	std::vector<long long int> max_weight(n), max_thick(n);
	for (auto i = 0; i < n; ++i) std::cin >> max_weight[i] >> max_thick[i];
	std::vector<int> max_size(1 << n, 0);
	for (auto p = 0; p < (1 << n); ++p) {
		const int current = max_size[p];
		for (auto i = 0; i < n; ++i) if ((p & (1 << i)) == 0) {
			const int next_with_weight = (current == m) ? current : std::distance(weights.begin(), std::upper_bound(std::next(weights.begin(), current), weights.end(), weights[current] + max_weight[i])) - 1;
			const int next_with_thick = (current == m) ? current : std::distance(thicks.begin(), std::upper_bound(std::next(thicks.begin(), current), thicks.end(), thicks[current] + max_thick[i])) - 1;
			max_size[p | (1 << i)] = std::max(max_size[p | (1 << i)], std::min(next_with_weight, next_with_thick));
		}
	}
	std::cout << max_size.back() << '\n';
}
accuracy: 1
time_ms: 50
memory_kb: 6248
final_rank: 6

code_snippet: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <cassert>
#include <complex>
#define repi(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define endl "\n"
#define ing long long
using namespace std;
using ll = long long;
const int INF = 1<<30;
const int MOD = (int)1e9 + 7;
const int MAX_N = (int)1e5 + 5;
#define debug(x) cout << #x << ": " << x << endl
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2>& p) { os << p.first << " " << p.second; return os; }
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) { for(int i = 0; i < (int) v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : ""); return os; }

#define w first
#define t second
#define c first
#define d second

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

    int M, N;
    vector<pair<int,int>> in, cd;
    vector<int> dp, sum1, sum2;

    cin>>M>>N;

    in.resize(M);
    sum1.resize(M+1,0);
    sum2.resize(M+1,0);
    cd.resize(N);
    dp.resize((1<<N) + 10);
    
    for(int i = 0; i < M; i++){
        cin>>in[i].w>>in[i].t;
        sum1[i+1] += in[i].w;
        sum2[i+1] += in[i].t;
        sum1[i+1] += sum1[i];
        sum2[i+1] += sum2[i];
        // cout<<"i = "<<i+1<<" sum1 "<<sum1[i+1]<<endl;
        // 
        // cout<<"i = "<<i+1<<" sum2 "<<sum2[i+1]<<endl;
    }

    for(int i = 0; i < N; i++){
        cin>>cd[i].c>>cd[i].d;
    }

    dp[0] = -1;

    for(int i = 1; i < (1<<N); i++){
        int maximum = -1 ;
        for(int j = 0; j < N; j++){
            int bit = i&(~(1<<j));
            
            //cout<<"bit = "<<bit<<" "<<i<<endl;
            if(bit == i) continue;
            int num = dp[bit];
            int W = 0, T = 0;
            maximum = max(num, maximum);
            int x = 0,  y = 0;

            if(num == M-1){
                cout<<M<<endl;
                return 0;
            }
            //if(num != -1)
                x = sum1[num+1] +cd[j].c;
                //if(num != -1)
                y = sum2[num+1]+ cd[j].d;
            
            /*
            for(int k = num+1; k < M; k++){
                W += in[k].w;
                T += in[k].t;
                if(W > cd[j].c || T > cd[j].d) break;

                maximum = max(k,maximum);
            }

            */
            int temp1 = upper_bound(sum1.begin(), sum1.end(), x) - sum1.begin();
            int temp2 = upper_bound(sum2.begin(), sum2.end(), y) - sum2.begin();
            // cout<<i<<" "<<j<<" "<<temp1<<" "<<temp2<<endl;
            // cout<<"x = "<<x<<" y = "<<y<<" num = "<<num<<" temp = "<<min(temp1,temp2)-1<<endl;
            maximum = max(min(temp1,temp2)-2, maximum);
            
        }
        dp[i] = maximum;
        //cout<<i<<" "<<maximum<<endl;
    }

    cout<<dp[(1<<N)-1]+1<<endl;
    
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 5872
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
using ll = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;};
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;};

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

  Int m,n;
  cin>>m>>n;
  vector<Int> ws(m),ts(m);
  for(Int i=0;i<m;i++) cin>>ws[i]>>ts[i];

  vector<Int> cs(n),bs(n);
  for(Int i=0;i<n;i++) cin>>cs[i]>>bs[i];

  vector<Int> sw(m+1,0),st(m+1,0);
  for(Int i=0;i<m;i++){
    sw[i+1]=sw[i]+ws[i];
    st[i+1]=st[i]+ts[i];
  }
  
  Int s=1<<n;
  const Int INF = 1e9;
  vector<Int> dp(s,-INF);
  dp[0]=0;
  for(Int b=0;b<s;b++){
    for(Int i=0;i<n;i++){
      if((b>>i)&1) continue;
      Int nb=b|(1<<i);
      Int x=upper_bound(sw.begin(),sw.end(),sw[dp[b]]+cs[i])-sw.begin();
      Int y=upper_bound(st.begin(),st.end(),st[dp[b]]+bs[i])-st.begin();
      chmax(dp[nb],min(x,y)-1);
    }
  }  
  cout<<dp[s-1]<<endl;
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 9248
final_rank: 13