传送门
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
using namespace std;
#define ll long long
#define re register
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define P pair<int,int>
const int N=5005;
const int mod=1e9+7;
void read(int &a)
{
a=0;
int d=1;
char ch;
while(ch=getchar(),ch>'9'||ch<'0')
if(ch=='-')
d=-1;
a=ch-'0';
while(ch=getchar(),ch>='0'&&ch<='9')
a=a*10+ch-'0';
a*=d;
}
void write(int x)
{
if(x<0)
putchar(45),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
}
int f[1005][12005],a[1005],b[1005];
int main()
{
int n;
read(n);
for(re int i=1;i<=n;i++)
read(a[i]),read(b[i]);
memset(f,0x7f,sizeof(f));
f[0][N]=0;
for(re int i=1;i<=n;i++)
{
int d=a[i]-b[i];
for(re int j=-5000;j<=5000;j++)
f[i][j+N]=min(f[i-1][j+N+d],f[i-1][j+N-d]+1);
}
int ans=0;
for(re int i=0;i<=5000;i++)
{
ans=min(f[n][i+N],f[n][-i+N]);
if(ans<=1000)
{
write(ans);
break;
}
}
return 0;
}