Problem:
Problem D: Let's Go To School
Problem
がっちょ君は期末試験が近づくとやる気がなくなってしまい、しばしば学校を休むようになる。
期末試験まで残り
N
日である。
がっちょ君の
i
日目のやる気は
X
i
であり、
i
日目に学校に行くために必要なやる気は
Y
i
である。
がっちょ君は
X
i
≥
Y
i
となる日のみ学校に行く。
がっちょ君を心配したハジ君は、がっちょ君を励ますことで、可能な限り学校に行かせようと考えた。
ハジ君は最初、励まし力
P
を持っている。
ハジ君が
i
日目に励まし力
t
(
t
は0以上の任意の実数)を消費してがっちょ君を励ますと、
がっちょ君の
i
日目のやる気は
t
だけ増加して、ハジ君の励まし力は
t
だけ減少する。さらに、
i
+1 ≤
N
のとき、
i
+1日目のがっちょ君のやる気
X
i+1
はmax(0,
X
i+1
−
t
) に変化する。
ハジ君は自分の持っている励まし力を超えて励ますことはできない。
ハジ君が最適な励まし方をしたときの、がっちょ君が学校に行く最大日数を求めよ。
Input
入力は以下の形式で与えられる。
N
P
X
1
Y
1
X
2
Y
2
...
X
N
Y
N
入力はすべて整数で与えられる。
1行目に期末試験までの日数
N
とハジ君が最初に持っている励まし力
P
が空白区切りで与えられる。
2行目から続く
N
行にはがっちょ君の
i
(
i
=1,2,...,
N
)日目のやる気
X
i
と学校に行くために必要なやる気
Y
i
が空白区切りで与えられる。
Constraints
入力は以下の条件を満たす。
1 ≤
N
≤ 100
0 ≤
P
≤ 10
6
0 ≤
X
i
≤ 10
6
0 ≤
Y
i
≤ 10
6
Output
がっちょ君が学校に行くことができる最大の日数を1行に出力せよ。
Sample Input 1
3 10
1 6
5 10
0 5
Sample Output 1
2
Sample Input 2
5 5
1 1
1 2
1 2
1 3
1 3
Sample Output 2
4

Submitted solutions:

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>
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;

enum Type{
	pre_yes,
	pre_no,
};

struct Info{
	Info(int arg_rest,Type arg_type){
		rest = arg_rest;
		type = arg_type;
	}
	int rest;
	Type type;
};

int dp[1000001][2],next_dp[1000001][2];

int main(){

	int N,P;
	scanf("%d %d",&N,&P);

	for(int i = 0; i <= P; i++){
		dp[i][pre_no] = -1;
		dp[i][pre_yes] = -1;

		next_dp[i][pre_no] = -1;
		next_dp[i][pre_yes] = -1;
	}

	int X[N],Y[N];
	for(int i = 0; i < N; i++){
		scanf("%d %d",&X[i],&Y[i]);
	}

	int ans = 0,minimum = P,next_minimum = P,today_value;
	vector<Info> Updated;

	dp[P][pre_no] = 0;
	dp[P][pre_yes] = 0;
	next_dp[P][pre_no] = 0;
	next_dp[P][pre_yes] = 0;

	for(int day = 0; day < N; day++){
		for(int rest = P; rest >= minimum; rest--){

			if(dp[rest][pre_no] != -1){

				if(X[day] >= Y[day]){

					next_dp[rest][pre_no] = dp[rest][pre_no]+1;
					ans = max(ans,next_dp[rest][pre_no]);
					Updated.push_back(Info(rest,pre_no));

				}else{ //X[day] < Y[day]

					if(rest >= Y[day]-X[day]){
						if(next_dp[rest-(Y[day]-X[day])][pre_yes] < dp[rest][pre_no]+1){
							next_dp[rest-(Y[day]-X[day])][pre_yes] = dp[rest][pre_no]+1;
							ans = max(ans,dp[rest][pre_no]+1);
							Updated.push_back(Info(rest-(Y[day]-X[day]),pre_yes));
							next_minimum = min(next_minimum,rest-(Y[day]-X[day]));
						}
					}
				}
			}

			if(dp[rest][pre_yes] == 0 || dp[rest][pre_yes] == -1)continue;

			today_value = max(0,X[day]-(Y[day-1]-X[day-1]));

			if(today_value >= Y[day]){
				if(next_dp[rest][pre_no] < dp[rest][pre_yes]+1){
					next_dp[rest][pre_no] = dp[rest][pre_yes]+1;
					ans = max(ans,next_dp[rest][pre_no]);
					Updated.push_back(Info(rest,pre_no));
				}
			}else{ //today_value < Y[day]

				if(next_dp[rest][pre_no] < dp[rest][pre_yes]){
					next_dp[rest][pre_no] = dp[rest][pre_yes];
					Updated.push_back(Info(rest,pre_no));
				}

				if(rest >= Y[day]-today_value){
					if(next_dp[rest-(Y[day]-today_value)][pre_yes] < dp[rest][pre_yes]+1){
						next_dp[rest-(Y[day]-today_value)][pre_yes] = dp[rest][pre_yes]+1;
						ans = max(ans,dp[rest][pre_yes]+1);
						Updated.push_back(Info(rest-(Y[day]-today_value),pre_yes));
						next_minimum = min(next_minimum,rest-(Y[day]-today_value));
					}
				}
			}
		}
		for(int k = 0; k < Updated.size(); k++){
			dp[Updated[k].rest][Updated[k].type] = next_dp[Updated[k].rest][Updated[k].type];
		}
		Updated.clear();
		minimum = next_minimum;
	}

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

	return 0;
}
accuracy: 0.28125
time_ms: 150
memory_kb: 17972
final_rank: 7

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>
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;

enum Type{
	pre_yes,
	pre_no,
};

struct Info{
	Info(int arg_rest,Type arg_type){
		rest = arg_rest;
		type = arg_type;
	}
	int rest;
	Type type;
};

int dp[1000001][2],next_dp[1000001][2];

int main(){

	int N,P;
	scanf("%d %d",&N,&P);

	for(int i = 0; i <= P; i++){
		dp[i][pre_no] = 0;
		dp[i][pre_yes] = 0;

		next_dp[i][pre_no] = 0;
		next_dp[i][pre_yes] = 0;
	}

	int X[N],Y[N];
	for(int i = 0; i < N; i++){
		scanf("%d %d",&X[i],&Y[i]);
	}

	int ans = 0,minimum = P,next_minimum = P,today_value;
	vector<Info> Updated;

	for(int day = 0; day < N; day++){
		for(int rest = P; rest >= minimum; rest--){

			if(X[day] >= Y[day]){

				next_dp[rest][pre_no] = dp[rest][pre_no]+1;
				ans = max(ans,next_dp[rest][pre_no]);
				Updated.push_back(Info(rest,pre_no));

			}else{ //X[day] < Y[day]

				if(rest >= Y[day]-X[day]){
					if(next_dp[rest-(Y[day]-X[day])][pre_yes] < dp[rest][pre_no]+1){
						next_dp[rest-(Y[day]-X[day])][pre_yes] = dp[rest][pre_no]+1;
						ans = max(ans,dp[rest][pre_no]+1);
						Updated.push_back(Info(rest-(Y[day]-X[day]),pre_yes));
						next_minimum = min(next_minimum,rest-(Y[day]-X[day]));
					}
				}
			}

			if(dp[rest][pre_yes] == 0)continue;

			today_value = max(0,X[day]-(Y[day-1]-X[day-1]));

			if(today_value >= Y[day]){
				if(next_dp[rest][pre_no] < dp[rest][pre_yes]+1){
					next_dp[rest][pre_no] = dp[rest][pre_yes]+1;
					ans = max(ans,next_dp[rest][pre_no]);
					Updated.push_back(Info(rest,pre_no));
				}
			}else{ //today_value < Y[day]

				if(next_dp[rest][pre_no] < dp[rest][pre_yes]){
					next_dp[rest][pre_no] = dp[rest][pre_yes];
					Updated.push_back(Info(rest,pre_no));
				}

				if(rest >= Y[day]-X[day]){
					if(next_dp[rest-(Y[day]-X[day])][pre_yes] < dp[rest][pre_yes]+1){
						next_dp[rest-(Y[day]-X[day])][pre_yes] = dp[rest][pre_yes]+1;
						ans = max(ans,dp[rest][pre_yes]+1);
						Updated.push_back(Info(rest-(Y[day]-X[day]),pre_yes));
						next_minimum = min(next_minimum,rest-(Y[day]-X[day]));
					}
				}
			}
		}
		for(int k = 0; k < Updated.size(); k++){
			dp[Updated[k].rest][Updated[k].type] = next_dp[Updated[k].rest][Updated[k].type];
		}
		Updated.clear();
		minimum = next_minimum;
	}

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

	return 0;
}
accuracy: 0.03125
time_ms: 80
memory_kb: 30088
final_rank: 18

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>
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;

enum Type{
	pre_yes,
	pre_no,
};

struct Info{
	Info(int arg_rest,Type arg_type){
		rest = arg_rest;
		type = arg_type;
	}
	int rest;
	Type type;
};

int main(){

	int N,P;
	scanf("%d %d",&N,&P);

	int** dp = new int*[P+1];
	int** next_dp = new int*[P+1];
	for(int i = 0; i <= P; i++){
		dp[i] = new int[2];
		dp[i][pre_no] = 0;
		dp[i][pre_yes] = 0;

		next_dp[i] = new int[2];
		next_dp[i][pre_no] = 0;
		next_dp[i][pre_yes] = 0;
	}

	int X[N],Y[N];
	for(int i = 0; i < N; i++){
		scanf("%d %d",&X[i],&Y[i]);
	}

	int ans = 0,minimum = P,next_minimum = P,today_value;
	vector<Info> Updated;

	for(int day = 0; day < N; day++){
		for(int rest = P; rest >= minimum; rest--){

			if(X[day] >= Y[day]){

				next_dp[rest][pre_no] = dp[rest][pre_no]+1;
				ans = max(ans,next_dp[rest][pre_no]);
				Updated.push_back(Info(rest,pre_no));

			}else{ //X[day] < Y[day]

				if(rest >= Y[day]-X[day]){
					if(next_dp[rest-(Y[day]-X[day])][pre_yes] < dp[rest][pre_no]+1){
						next_dp[rest-(Y[day]-X[day])][pre_yes] = dp[rest][pre_no]+1;
						ans = max(ans,dp[rest][pre_no]+1);
						Updated.push_back(Info(rest-(Y[day]-X[day]),pre_yes));
						next_minimum = min(next_minimum,rest-(Y[day]-X[day]));
					}
				}
			}

			if(dp[rest][pre_yes] == 0)continue;

			today_value = max(0,X[day]-(Y[day-1]-X[day-1]));

			if(today_value >= Y[day]){ //?????\?????¨??????????????????
				if(next_dp[rest][pre_no] < dp[rest][pre_yes]+1){
					next_dp[rest][pre_no] = dp[rest][pre_yes]+1;
					ans = max(ans,next_dp[rest][pre_no]);
					Updated.push_back(Info(rest,pre_no));
				}
			}else{ //today_value < Y[day]

				if(next_dp[rest][pre_no] < dp[rest][pre_yes]){
					next_dp[rest][pre_no] = dp[rest][pre_yes];
					Updated.push_back(Info(rest,pre_no));
				}

				if(rest >= Y[day]-X[day]){
					if(next_dp[rest-(Y[day]-X[day])][pre_yes] < dp[rest][pre_yes]+1){
						next_dp[rest-(Y[day]-X[day])][pre_yes] = dp[rest][pre_yes]+1;
						ans = max(ans,dp[rest][pre_yes]+1);
						Updated.push_back(Info(rest-(Y[day]-X[day]),pre_yes));
						next_minimum = min(next_minimum,rest-(Y[day]-X[day]));
					}
				}
			}
		}
		for(int k = 0; k < Updated.size(); k++){
			dp[Updated[k].rest][Updated[k].type] = next_dp[Updated[k].rest][Updated[k].type];
		}
		Updated.clear();
		minimum = next_minimum;
	}

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

	return 0;
}
accuracy: 0.03125
time_ms: 70
memory_kb: 57864
final_rank: 19

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

int dp[2][2][1000001];
int N, P;
int X[101], Y[101];

/*int dfs(int n, int p) {
  if ( n == N ) return 0;
  if ( dp[n][p] >= 0 ) return dp[n][p];

  int ret;
  if ( X[n] >= Y[n] ) ret = dfs(n+1, p) + 1;
  else {
    ret = dfs(n+1, p);
    if ( p >= Y[n]-X[n] ) ret = max(ret, dfs(n+1, p - (Y[n]-X[n]))+1); 
  }

  return dp[n][p] = ret;
  }*/

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> P;
  for ( int i = 1; i <= N; i++ ) cin >> X[i] >> Y[i];

  int now = 0, next = 1;
  dp[0][0][0] = 0;
  for ( int i = 1; i <= N; i++ ) {
    for ( int k = 0; k < 2; k++ ) {
      for ( int j = 0; j <= P; j++ ) {
	if ( dp[now][k][j] < 0 ) continue;
	if ( k == 0 ) {
	  dp[next][0][j] = max(dp[next][0][j], dp[now][0][j]);
	  if ( X[i] >= Y[i] ) dp[next][0][j] = max(dp[next][0][j], dp[now][0][j]+1);
	  else if ( j + (Y[i]-X[i]) <= P ) {
	    dp[next][1][j + (Y[i]-X[i])] = max(dp[next][1][j + (Y[i]-X[i])], dp[now][0][j]+1);
	  }
	} else {
	  dp[next][0][j] = max(dp[next][0][j], dp[now][1][j]);
	  int t = Y[i-1] - X[i-1];
	  int x = max(0, X[i] - t);
	  if ( x >= Y[i] ) dp[next][0][j] = max(dp[next][0][j], dp[now][1][j]+1);
	  else if ( j + (Y[i]-x) <= P ) {
	    dp[next][1][j + (Y[i]-x)] = max(dp[next][1][j + (Y[i]-x)], dp[now][1][j]+1);
	  }
	}
      }
    }
    swap(now, next);     
  }

    
  int ans = 0;
  for ( int i = 0; i <= P; i++ ){
    for ( int j = 0; j < 2; j++ ) {
      ans = max(ans, dp[now][j][i]);
    }
  }

  cout << ans << endl;
  
  return 0;
}
accuracy: 0.28125
time_ms: 120
memory_kb: 18700
final_rank: 6

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

int dp[2][2][1000001];
int N, P;
int X[100], Y[100];

/*int dfs(int n, int p) {
  if ( n == N ) return 0;
  if ( dp[n][p] >= 0 ) return dp[n][p];

  int ret;
  if ( X[n] >= Y[n] ) ret = dfs(n+1, p) + 1;
  else {
    ret = dfs(n+1, p);
    if ( p >= Y[n]-X[n] ) ret = max(ret, dfs(n+1, p - (Y[n]-X[n]))+1); 
  }

  return dp[n][p] = ret;
  }*/

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> P;
  for ( int i = 1; i <= N; i++ ) cin >> X[i] >> Y[i];

  int now = 0, next = 1;
  dp[0][0][0] = 0;
  for ( int i = 1; i <= N; i++ ) {
    for ( int k = 0; k < 2; k++ ) {
      for ( int j = 0; j <= P; j++ ) {
	if ( dp[now][k][j] < 0 ) continue;
	if ( k == 0 ) {
	  dp[next][0][j] = max(dp[next][0][j], dp[now][0][j]);
	  if ( X[i] >= Y[i] ) dp[next][0][j] = max(dp[next][0][j], dp[now][0][j]+1);
	  else if ( j + (Y[i]-X[i]) <= P ) {
	    dp[next][1][j + (Y[i]-X[i])] = max(dp[next][1][j + (Y[i]-X[i])], dp[now][0][j]+1);
	  }
	} else {
	  dp[next][0][j] = max(dp[next][0][j], dp[now][1][j]);
	  int t = Y[i-1] - X[i-1];
	  int x = max(0, X[i] - t);
	  if ( x >= Y[i] ) dp[next][0][j] = max(dp[next][0][j], dp[now][1][j]+1);
	  else if ( j + (Y[i]-x) <= P ) {
	    dp[next][1][j + (Y[i]-x)] = max(dp[next][1][j + (Y[i]-x)], dp[now][1][j]+1);
	  }
	}
      }
    }
    swap(now, next);     
  }

    
  int ans = 0;
  for ( int i = 0; i <= P; i++ ){
    for ( int j = 0; j < 2; j++ ) {
      ans = max(ans, dp[now][j][i]);
    }
  }

  cout << ans << endl;
  
  return 0;
}
accuracy: 0.109375
time_ms: 70
memory_kb: 18700
final_rank: 9

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

int dp[2][2][1000001];
int N, P;
int X[100], Y[100];

/*int dfs(int n, int p) {
  if ( n == N ) return 0;
  if ( dp[n][p] >= 0 ) return dp[n][p];

  int ret;
  if ( X[n] >= Y[n] ) ret = dfs(n+1, p) + 1;
  else {
    ret = dfs(n+1, p);
    if ( p >= Y[n]-X[n] ) ret = max(ret, dfs(n+1, p - (Y[n]-X[n]))+1); 
  }

  return dp[n][p] = ret;
  }*/

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> P;
  for ( int i = 1; i <= N; i++ ) cin >> X[i] >> Y[i];

  int now = 0, next = 1;
  dp[0][0][0] = 0;
  for ( int i = 1; i <= N; i++ ) {
    for ( int k = 0; k < 2; k++ ) {
      for ( int j = 0; j <= P; j++ ) {
	if ( dp[now][k][j] < 0 ) continue;
	if ( k == 0 ) {
	  dp[next][0][j] = max(dp[next][0][j], dp[now][0][j]);
	  if ( X[i] >= Y[i] ) dp[next][0][j] = max(dp[next][0][j], dp[now][0][j]+1);
	  else if ( j + (Y[i]-X[i]) <= P ) {
	    dp[next][1][j + (Y[i]-X[i])] = max(dp[next][1][j + (Y[i]-X[i])], dp[now][0][j]+1);
	  }
	} else {
	  dp[next][0][j] = max(dp[next][0][j], dp[now][1][j]);
	  int t = Y[i-1] - X[i-1];
	  if ( X[i] - t >= Y[i] ) dp[next][0][j] = max(dp[next][0][j], dp[now][1][j]+1);
	  else if ( j + (Y[i]-(X[i]-t)) <= P ) {
	    dp[next][1][j + (Y[i]-(X[i]-t))] = max(dp[next][1][j + (Y[i]-(X[i]-t))], dp[now][1][j]+1);
	  }
	}
      }
    }
    swap(now, next);     
  }

    
  int ans = 0;
  for ( int i = 0; i <= P; i++ ){
    for ( int j = 0; j < 2; j++ ) {
      ans = max(ans, dp[now][j][i]);
    }
  }

  cout << ans << endl;
  
  return 0;
}
accuracy: 0.109375
time_ms: 60
memory_kb: 18700
final_rank: 8

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

int dp[2][1000001];
int N, P;
int X[100], Y[100];

int dfs(int n, int p) {
  if ( n == N ) return 0;
  if ( dp[n][p] >= 0 ) return dp[n][p];

  int ret;
  if ( X[n] >= Y[n] ) ret = dfs(n+1, p) + 1;
  else {
    ret = dfs(n+1, p);
    if ( p >= Y[n]-X[n] ) ret = max(ret, dfs(n+1, p - (Y[n]-X[n]))+1); 
  }

  return dp[n][p] = ret;
}

int main() {
  memset(dp, -1, sizeof(dp));
  cin >> N >> P;
  for ( int i = 1; i <= N; i++ ) cin >> X[i] >> Y[i];

  int now = 0, next = 1;
  dp[0][0] = 0;
  for ( int i = 1; i <= N; i++ ) {
    for ( int j = 0; j <= P; j++ ) {
      if ( dp[now][j] < 0 ) continue;
      dp[next][j] = max(dp[next][j], dp[now][j]);
      if ( X[i] >= Y[i] ) dp[next][j] = max(dp[next][j], dp[now][j]+1);
      else if ( j + (Y[i]-X[i]) <= P ) {
	dp[next][j + (Y[i]-X[i])] = max(dp[next][j + (Y[i]-X[i])], dp[now][j]+1);
      }
    }
    swap(now, next);
  }

  int ans = 0;
  for ( int i = 0; i <= P; i++ ){
    ans = max(ans, dp[now][i]);
  }

  cout << ans << endl;
  
  return 0;
}
accuracy: 0.03125
time_ms: 10
memory_kb: 10908
final_rank: 13

code_snippet: #include<bits/stdc++.h>
 using namespace std;
#define INF 1000000000
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
typedef long long LL;
int N;
int P;
int X[111];
int Y[111];
int dp[111][111][111];
int cal(int x,int y){
    x--;y--;
    if(x==y)return max(Y[x]-X[x],0);

    int need=max(Y[x]-X[x],0);//?????\????????????????????£??????
    for(int i=x+1;i<=y;i++){
        int nX=max(0,X[i]-need);
        need=max(Y[i]-nX,0);

    }
    return need;
}
int main(){
    cin>>N>>P;
    REP(i,N){
        cin>>X[i]>>Y[i];
    }
    //dp????????????????????????
    REP(i,111){
        REP(j,111){
            REP(k,111){
                dp[i][j][k]=-1;
            }
        }
    }
    dp[0][0][0]=P;
    
    //dp?????´??°
    REP(i,N+1){
        if(i==0)continue;
        REP(j,N+1){
            //????????????
            REP(k,i){
                dp[i][j][i]=max(dp[i][j][i],dp[i-1][j][k]);
            }
            if(j==0)continue;
            //??????
            REP(k,N+1){               
                dp[i][j][k]=max(dp[i][j][k],dp[i-1][j-1][k]-cal(k+1,i));
            }
            //????????????
            dp[i][j][i-1]=max(dp[i][j][i-1],dp[i-1][j-1][i-1]-cal(i,i));
        }
    }
    //
    int ans=0;
    REP(i,N+1){
        REP(j,N+1){
            REP(k,N+1){
                if(dp[i][j][k]!=-1){
                   ans=max(ans,j);
                }
            }
        }
    }
    
   /* REP(i,N+1){
        REP(j,N+1){
            REP(k,N+1){
                cout<<"dp[i]["<<j<<"]["<<k<<"]="<<dp[i][j][k] <<" "<<endl;;
            }
        }
        cout<<endl;
    }*/
    
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 8436
final_rank: 1

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

using namespace std;

int N, P;
int X[100], Y[100];
int dp[50][1000000];

int rec(int idx, int p, int pre)
{
  if(idx == N) return (0);
  if(~dp[idx % 2][p]) return (dp[idx % 2][p]);
  int ret = rec(idx + 1, p, 0);
  int get = max(0, Y[idx] - max(0, X[idx] - pre));
  if(p - get >= 0) ret = max(ret, rec(idx + 1, p - get, get) + 1);
  return (dp[idx % 2][p] = ret);
}

int main()
{
  memset(dp, -1, sizeof(dp));
  cin >> N >> P;
  for(int i = 0; i < N; i++) cin >> X[i] >> Y[i];
  cout << rec(0, P, 0) << endl;
}
accuracy: 0.015625
time_ms: 40
memory_kb: 198392
final_rank: 20

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

using namespace std;

int main()
{
  int N, P;
  int X[100], Y[100];

  cin >> N >> P;
  for(int i = 0; i < N; i++) cin >> X[i] >> Y[i];

  int dp[1000001][2];
  memset(dp, -1, sizeof(dp));
  dp[P][0] = 0;
  for(int i = 0; i < N; i++) {
    int dp2[1000001][2];
    memset(dp2, -1, sizeof(dp2));

    for(int j = P; j >= 0; j--) {
      dp2[j][0] = max(dp2[j][0], dp[j][0]);
      dp2[j][0] = max(dp2[j][0], dp[j][1]);
    }

    for(int j = P; j >= 0; j--) {
      int XX = max(0, Y[i] - X[i]);
      if(j + XX <= P) {
        if(dp[j + XX][0] >= 0) {
          dp2[j][XX == 0] = max(dp2[j][XX == 0], dp[j + XX][0] + 1);
        }
      }

      if(i > 0) {
        int YY = max(0, Y[i - 1] - X[i - 1]);
        int ZZ = max(0, Y[i] - max(0, X[i] - YY));
        if(j + ZZ <= P) {
          if(dp[j + ZZ][1] >= 0) {
            dp2[j][ZZ == 0] = max(dp2[j][ZZ == 0], dp[j + XX][0] + 1);
          }
        }
      }
    }
    swap(dp, dp2);
  }

  int ret = 0;
  for(int i = 0; i <= P; i++) ret = max(ret, dp[i][0]);
  for(int i = 0; i <= P; i++) ret = max(ret, dp[i][1]);

  cout << ret << endl;
}
accuracy: 0.03125
time_ms: 90
memory_kb: 18728
final_rank: 17

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)

#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()

#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))

int n,p;
int x[125],y[125];
pii dp[1252830];

int main(){
  scanf("%d%d",&n,&p);
  REP(i,n)scanf("%d%d",x+i,y+i);
  REP(i,1252830)dp[i]=pii(-125252,-125252);
  dp[p] = pii(0,0);
  REP(i,n){
    int xx = x[i];
    int yy = y[i];
    REP(j,p+1){
      int vl = dp[j].first;
      int dn = dp[j].second;
      int px = max(xx+dn,0);
      if(px>=yy){
        dp[j] = pii(vl+1,0);
      }else{
        dp[j] = pii(vl,0);
        int need = yy-px;
        if(need<=j)CHMAX(dp[j-need],pii(vl+1,-need));
      }
    }
  }
  int ans = 0;
  REP(i,p+1)CHMAX(ans,dp[i].first);
  printf("%d\n",ans);
  return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 12956
final_rank: 2

code_snippet: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int dp[2][1000010];
int fault[1000010],temp[1000010];
	

int main(){
	int n,p;
	cin>>n>>p;
	int x[n],y[n];
	rep(i,n)cin>>x[i]>>y[i];
	rep(i,2)rep(j,p+1)dp[i][j]=-1;
	dp[0][p]=0;
	rep(i,p+1){fault[i]=0;temp[i]=0;}
	rep(i,n){
		rep(j,p+1){
			if(dp[0][j]==-1)continue;
			dp[1][j]=dp[0][j];
			temp[j]=0;
			if(fault[j]==0){
				if(x[i]>=y[i]){
					if(dp[1][j]<dp[0][j]+1){
						dp[1][j]=dp[0][j]+1;
						temp[j]=0;
					}
				}else{
					if(j>=(y[i]-x[i])){
						if(dp[1][j-(y[i]-x[i])]<dp[0][j]+1){
							dp[1][j-(y[i]-x[i])]=dp[0][j]+1;
							temp[j-(y[i]-x[i])]=y[i]-x[i];
						}
					}
				}
			}else{
				if((x[i]-fault[j]>=y[i])){
					dp[1][j]=dp[0][j]+1;
					temp[j]=0;
				}else{
					int alt=max(x[i]-fault[j],0);
					if(j>=(y[i]-alt)){
						if(dp[1][j-(y[i]-alt)]<dp[0][j]+1){
							dp[1][j-(y[i]-alt)]=dp[0][j]+1;
							temp[j-(y[i]-alt)]=(y[i]-alt);
						}
					}
				}
			}
		}
		rep(j,p+1)dp[0][j]=dp[1][j];
		rep(j,p+1)fault[j]=temp[j];
	}
	int ans=0;
	rep(i,p+1){
		ans=max(ans,dp[1][i]);
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 18660
final_rank: 3

code_snippet: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int dp[2][1000010];
int fault[1000010],temp[1000010];
	

int main(){
	int n,p;
	cin>>n>>p;
	int x[n],y[n];
	rep(i,n)cin>>x[i]>>y[i];
	rep(i,2)rep(j,p+1)dp[i][j]=-1;
	dp[0][p]=0;
	rep(i,p+1){fault[i]=0;temp[i]=0;}
	rep(i,n){
		rep(j,p+1){
			if(dp[0][j]==-1)continue;
			dp[1][j]=dp[0][j];
			temp[j]=0;
			if(fault[j]==0){
				if(x[i]>=y[i]){
					if(dp[1][j]<dp[0][j]+1){
						dp[1][j]=dp[0][j]+1;
						temp[j]=0;
					}
				}else{
					if(j>=(y[i]-x[i])){
						if(dp[1][j-(y[i]-x[i])]<dp[0][j]+1){
							dp[1][j-(y[i]-x[i])]=dp[0][j]+1;
							temp[j-(y[i]-x[i])]=y[i]-x[i];
						}
					}
				}
			}else{
				if((x[i]-fault[j]>=y[i])){
					dp[1][j]=dp[0][j]+1;
					temp[j]=0;
				}else{
					int alt=max(x[i]-fault[j],0);
					if(j>=(y[i]-alt)){
						if(dp[1][j-(y[i]-alt)]<dp[0][j]+1){
							dp[1][j-(y[i]-alt)]=dp[0][j]+1;
							temp[j-(y[i]-alt)]=j-(y[i]-alt);
						}
					}
				}
			}
		}
		rep(j,p+1)dp[0][j]=dp[1][j];
		rep(j,p+1)fault[j]=temp[j];
	}
	int ans=0;
	rep(i,p+1){
		ans=max(ans,dp[1][i]);
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 0.0625
time_ms: 40
memory_kb: 13784
final_rank: 10

code_snippet: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int dp[110][1000010];

int main(){
	int n,p;
	cin>>n>>p;
	int x[n],y[n];
	rep(i,n)cin>>x[i]>>y[i];
	int fault[p+1],temp[p+1];
	rep(i,n+1)rep(j,p+1)dp[i][j]=-1;
	dp[0][p]=0;
	rep(i,p+1){fault[i]=0;temp[i]=0;}
	rep(i,n){
		rep(j,p+1){
			if(dp[i][j]==-1)continue;
			dp[i+1][j]=dp[i][j];
			temp[j]=0;
			if(i==0){
				if(x[i]>=y[i]){
					if(dp[i+1][j]<dp[i][j]+1){
						dp[i+1][j]=dp[i][j]+1;
						temp[j]=0;
					}
				}else{
					if(j>=(y[i]-x[i])){
						if(dp[i+1][j-(y[i]-x[i])]<dp[i][j]+1){
							dp[i+1][j-(y[i]-x[i])]=dp[i][j]+1;
							temp[j-(y[i]-x[i])]=y[i]-x[i];
						}
					}
				}
			}else{
				if(fault[j]==0){
					if(x[i]>=y[i]){
						if(dp[i+1][j]<dp[i][j]+1){
							dp[i+1][j]=dp[i][j]+1;
							temp[j]=0;
						}
					}else{
						if(j>=(y[i]-x[i])){
							if(dp[i+1][j-(y[i]-x[i])]<dp[i][j]+1){
								dp[i+1][j-(y[i]-x[i])]=dp[i][j]+1;
								temp[j-(y[i]-x[i])]=y[i]-x[i];
							}
						}
					}
				}else{
					if((x[i]-fault[j]>=y[i])){
						dp[i+1][j]=dp[i][j]+1;
						temp[j]=0;
					}else{
						int alt=max(x[i]-fault[j],0);
						if(j>=(y[i]-alt)){
							if(dp[i+1][j-(y[i]-alt)]<dp[i][j]+1){
								dp[i+1][j-(y[i]-alt)]=dp[i][j]+1;
								temp[j-(y[i]-alt)]=j-(y[i]-alt);
							}
						}
					}
				}
			}
		}
		rep(j,p+1)fault[j]=temp[j];
		/*cout<<"-----------------------"<<endl;
		rep(j,p+1)cout<<fault[j]<<" ";
		cout<<endl;
		cout<<"-----------------------"<<endl;
		cout<<"-----------------------"<<endl;
		rep(i,n+1){
			rep(j,p+1){
				cout<<dp[i][j]<<" ";
			}
			cout<<endl;
		}
		cout<<"-----------------------"<<endl;*/
	}
	int ans=0;
	/*cout<<"-----------------------"<<endl;
	rep(i,n+1){
		rep(j,p+1){
			cout<<dp[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<"-----------------------"<<endl;*/
	rep(i,p+1){
		ans=max(ans,dp[n][i]);
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 0.0625
time_ms: 50
memory_kb: 109448
final_rank: 11

code_snippet: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int dp[110][1000010];

int main(){
	int n,p;
	cin>>n>>p;
	int x[n],y[n];
	rep(i,n)cin>>x[i]>>y[i];
	int fault[p+1],temp[p+1];
	rep(i,n+1)rep(j,p+1)dp[i][j]=-1;
	dp[0][p]=0;
	rep(i,p+1){fault[i]=0;temp[i]=0;}
	rep(i,n){
		rep(j,p+1){
			if(dp[i][j]==-1)continue;
			dp[i+1][j]=dp[i][j];
			temp[j]=0;
			if(i==0){
				if(x[i]>=y[i]){
					if(dp[i+1][j]<dp[i][j]+1){
						dp[i+1][j]=dp[i][j]+1;
						temp[j]=0;
					}
				}else{
					if(j>=(y[i]-x[i])){
						if(dp[i+1][j-(y[i]-x[i])]<dp[i][j]+1){
							dp[i+1][j-(y[i]-x[i])]=dp[i][j]+1;
							temp[j-(y[i]-x[i])]=y[i]-x[i];
						}
					}
				}
			}else{
				if(fault[j]==0){
					if(x[i]>=y[i]){
						if(dp[i+1][j]<dp[i][j]+1){
							dp[i+1][j]=dp[i][j]+1;
							temp[j]=0;
						}
					}else{
						if(j>=(y[i]-x[i])){
							if(dp[i+1][j-(y[i]-x[i])]<dp[i][j]+1){
								dp[i+1][j-(y[i]-x[i])]=dp[i][j]+1;
								temp[j-(y[i]-x[i])]=y[i]-x[i];
							}
						}
					}
				}else{
					if((x[i]-fault[j]>=y[i])){
						dp[i+1][j]=dp[i][j]+1;
						temp[j]=0;
					}else{
						if(j>=(y[i]-(x[i]-fault[j]))){
							if(dp[i+1][j-(y[i]-(x[i]-fault[j]))]<dp[i][j]+1){
								dp[i+1][j-(y[i]-(x[i]-fault[j]))]=dp[i][j]+1;
								temp[j-(y[i]-(x[i]-fault[j]))]=j-(y[i]-(x[i]-fault[j]));
							}
						}
					}
				}
			}
		}
		rep(j,p+1)fault[j]=temp[j];
	}
	int ans=0;
	rep(i,p+1){
		ans=max(ans,dp[n][i]);
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 0.0625
time_ms: 50
memory_kb: 109500
final_rank: 12

code_snippet: #include<iostream>
#include<sstream>
#include<vector>
#include<deque>
#include<cstdio>
#include<set>
#include<cassert>
#include<cstring>
#include<algorithm>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)

using namespace std;
typedef long long ll;

#define MAX 210
int N,P,X[MAX],Y[MAX];
int dp[3000100];
int base;

void fix() {
  vector<int> x,y;
  rep(i,N) {
    if( X[i] >= Y[i] ){
      ++base;
    } else {
      x.push_back(X[i]);
      y.push_back(Y[i]);
    }
  }
  N = (int)x.size();
  rep(i,N) {
    X[i] = x[i];
    Y[i] = y[i];
  }
}

int main() {
  cin >> N >> P;
  rep(i,N) cin >> X[i] >> Y[i];
  fix();
  memset(dp,-1,sizeof dp);
  dp[0] = 0;
  rep(i,N) {
    REP(j,1,P) {
      dp[j] = max(dp[j],dp[j-1]);
    }
      assert( Y[i] - X[i] > 0);
    for(int j=P-1;j>=0;--j) {

      if( j + Y[i]-X[i] <= P )  {
	dp[j + Y[i]-X[i]] = max(dp[j + Y[i]-X[i]],
				       dp[j] + 1);
      }
    }
  }
  cout << base+dp[P] << endl;
  return 0;
}
accuracy: 0.03125
time_ms: 10
memory_kb: 14800
final_rank: 14

code_snippet: #include<iostream>
#include<sstream>
#include<vector>
#include<deque>
#include<cstdio>
#include<set>
#include<cassert>
#include<cstring>
#include<algorithm>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)

using namespace std;
typedef long long ll;

#define MAX 210
int N,P,X[MAX],Y[MAX];
int dp[3000100];

int main() {
  cin >> N >> P;
  rep(i,N) cin >> X[i] >> Y[i];
  memset(dp,-1,sizeof dp);
  dp[0] = 0;
  rep(i,N) {
    REP(j,1,P) {
      dp[j] = max(dp[j],dp[j-1]);
    }
    for(int j=P-1;j>=0;--j) {
      if( j + max(0,Y[i]-X[i]) <= P )  {
	dp[j + max(0,Y[i]-X[i])] = max(dp[j + max(0,Y[i]-X[i])],
				       dp[j] + 1);
      }
    }
  }
  cout << dp[P] << endl;
  return 0;
}
accuracy: 0.03125
time_ms: 50
memory_kb: 14780
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a; i<n; i++)
#define repq(i,a,n) for(int i=a; i<=n; i++)
#define repr(i,a,n) for(int i=a; i>=n; i--)
using namespace std;

int x[110], y[110];
int dp[1000010][2];
int main() {
    int n, p; cin >> n >> p;
    rep(i,0,n) cin >> x[i] >> y[i];

    int ans = 0;
    memset(dp, 0, sizeof(dp));
    rep(i,0,n) {
        repr(j,p,0) {
            int cost = y[i] - x[i];
            if(cost <= 0) {
                dp[j][0]++;
                dp[j][1] = max(dp[j][1], dp[j][0]);
            }
            else {
                rep(k,0,2) {
                    int prev;
                    if(i == 0) prev = -1;
                    else prev = max(0, y[i-1] - x[i-1]);
                    // printf("debug: cost = %d, prev = %d\n", cost, prev);

                    if(j - cost >= 0) dp[j][1] = max(dp[j][1], dp[j-cost][0] + 1);
                    if(i != 0 && j-cost-prev >= 0) dp[j][1] = max(dp[j][1], dp[j-cost][1] + 1);
                }

                ans = max(ans, max(dp[j][0], dp[j][1]));
                // printf("day = %d, dp[%d][%d] = %d\n", i+1, j, k, dp[j][k]);
            }
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.03125
time_ms: 30
memory_kb: 10824
final_rank: 15

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i = (a); i < (b); ++i)
#define RFOR(i,a,b) for (ll i = (b)-1; i >= (a); --i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

const ll linf = 1e18;
const ll inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    EACH(x,vec)is>>x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
    REP(i, vec.size()) {
        if(i) os << " ";
        os << vec[i];
    }
    return os;
}

P dp[2][1000001];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n, p; cin >> n >> p;
    vector<ll> X(n), Y(n);
    REP(i, n) cin >> X[i] >> Y[i];

    fill(dp[0], dp[2], P(-1,0));
    dp[0][0] = P(0, 0);
    REP(i, n) {
        fill(dp[i+1&1], dp[(i+1&1)+1], P(-1,-1));
        REP(j, p+1) {
            if (dp[i&1][j].first < 0) continue;
            // ??±????????????
            dp[i+1&1][j] = max(dp[i+1&1][j], P(dp[i&1][j].first, 0));
            // ??±??????
            ll x = max<ll>(0, X[i] - (-dp[i&1][j].second));
            ll need = max<ll>(0, Y[i] - x);
            if (j+need <= p) {
                ll nj = j+need;
                dp[i+1&1][nj] = max(dp[i+1&1][nj], P(dp[i&1][j].first+1, -need));
            }
        }
    }
    ll ans = 0;
    REP(j, p+1) {
        ans = max<ll>(ans, dp[n&1][j].first);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 510
memory_kb: 34388
final_rank: 4

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i = (a); i < (b); ++i)
#define RFOR(i,a,b) for (ll i = (b)-1; i >= (a); --i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

const ll linf = 1e18;
const ll inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    EACH(x,vec)is>>x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
    REP(i, vec.size()) {
        if(i) os << " ";
        os << vec[i];
    }
    return os;
}

P dp[2][1000001];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n, p; cin >> n >> p;
    vector<ll> X(n), Y(n);
    REP(i, n) cin >> X[i] >> Y[i];

    fill(dp[0], dp[2], P(-1,0));
    dp[0][0] = P(0, 0);
    REP(i, n) {
        fill(dp[i+1&1], dp[(i+1&1)+1], P(-1,-1));
        REP(j, p+1) {
            if (dp[i&1][j].first < 0) continue;
            // ??±????????????
            dp[i+1&1][j] = max(dp[i+1&1][j], P(dp[i&1][j].first, 0));
            // ??±??????
            ll need = max<ll>(0, Y[i] - X[i] + (-dp[i&1][j].second));
            if (j+need <= p) {
                ll nj = j+need;
                dp[i+1&1][nj] = max(dp[i+1&1][nj], P(dp[i&1][j].first+1, -need));
            }
        }
    }
    ll ans = 0;
    REP(j, p+1) {
        ans = max<ll>(ans, dp[n&1][j].first);
    }
    cout << ans << endl;
}
accuracy: 0.96875
time_ms: 510
memory_kb: 34396
final_rank: 5