1 #include <bits/stdc++.h> 2 #define maxn 13003 3 #define _for(i,a,b) for(int i = (a);i < b;i ++) 4 typedef long long ll; 5 using namespace std; 6 inline ll read() 7 { 8 ll ans = 0; 9 char ch = getchar(), last = ' '; 10 while(!isdigit(ch)) last = ch, ch = getchar(); 11 while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); 12 if(last == '-') ans = -ans; 13 return ans; 14 } 15 inline void write(ll x) 16 { 17 if(x < 0) x = -x, putchar('-'); 18 if(x >= 10) write(x / 10); 19 putchar(x % 10 + '0'); 20 } 21 int N,V = 0; 22 int base = 0; 23 int m[maxn]; 24 int d[maxn]; 25 int dp[maxn]; 26 int vs[maxn]; 27 28 int main() 29 { 30 memset(dp,0,sizeof(dp)); 31 memset(m,0,sizeof(m)); 32 N = read(); 33 _for(i,0,N) 34 { 35 int a = read(),b = read(); 36 if(a<b) 37 m[i] = -1,base++; 38 else if(a>b) 39 m[i] = 1; 40 int t = abs(a-b); 41 V += t; 42 d[i] = t*2; 43 } 44 45 vs[0] = 1; 46 _for(i,0,N) 47 for(int v = V+6000; v >= 0; v --) 48 if(vs[v-d[i]]) 49 if(!vs[v]) 50 dp[v] = dp[v-d[i]]+m[i],vs[v] = 1; 51 else 52 dp[v] = min(dp[v],dp[v-d[i]]+m[i]); 53 54 int vv = V; 55 int d = 0; 56 while(!vs[vv]) 57 { 58 vv += d; 59 if(!d || d>0) 60 d ++; 61 d *= -1; 62 } 63 64 write(base+min(dp[V+d],dp[V-d])); 65 printf(" "); 66 return 0; 67 }