1 #include <bits/stdc++.h> 2 using namespace std; 3 const int m=6000; 4 int f[1010][m*2+2],n,a[2000],b[2000],k; 5 int main() { 6 memset(f, 0x3f, sizeof(f)); 7 scanf("%d", &n); 8 f[0][m] = 0; 9 for (int i = 1; i <= n; i++) { 10 scanf("%d%d", &a[i], &b[i]); 11 } 12 for (int i = 1; i <= n; i++) { 13 for (int j = 1; j <= m * 2; j++) { 14 f[i][j] = min(f[i - 1][j - a[i] + b[i]], f[i - 1][j + a[i] - b[i]] + 1); 15 } 16 } 17 k = 0; 18 if (f[n][m + k] == f[0][1] && f[n][m - k] == f[0][1]) { 19 k++; 20 } 21 printf("%d ", min(f[n][m + k], f[n][m - k])); 22 }