Codeforces Round #178 (Div. 2) B .Shaass and Bookshelf

Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of the i-th book is ti and its pages' width is equal to wi. The thickness of each book is either 1 or 2. All books have the same page heights.

Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.

Help Shaass to find the minimum total thickness of the vertical books that we can achieve.

Input

The first line of the input contains an integer n, (1 ≤ n ≤ 100). Each of the next n lines contains two integers ti and wi denoting the thickness and width of the i-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).

Output

On the only line of the output print the minimum total thickness of the vertical books that we can achieve.

Sample Input

Input

51 121 32 152 52 1

Output

5

Input

31 102 12 4

Output

3

題意:

給你N本書,每本書由一個厚度t[i](1或者2),寬度w[i],高度都是一樣,把一些書豎着放,然後一些書橫着放在同一層,問你把所有的書放好之後豎着的書的總厚度是多少?

思路:

每本書的厚度要麼爲1要麼爲2,因此我們可以根據書的厚度分爲兩類,然後每類按書的寬度從大到小排序,然後用二重循環進行枚舉,把厚度爲1的前i個和厚度爲2的前j個豎着放,其他的橫着放,在橫着放的總寬度不超過豎着放的總厚度的前提下,求出一個豎着放總厚度最小的值來,這就是最終答案.

代碼:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define INF 0x1f1f1f1f
int a[1001];
int b[1001];
int f[1001];
int main()
{
    int n,V,i,j;
    while(scanf("%d",&n)!=EOF)
    {
        V=0;
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&b[i],&a[i]);
            V=V+b[i];
        }
        for(i=0;i<=V;i++)
        f[i]=INF;
        f[0]=0;     
        for(i=0;i<n;i++)
        for(j=V;j>=b[i];j--)
        f[j]=min(f[j],f[j-b[i]]+a[i]);
        for(i=V;i>=0;i--)
        {
            if(V-i>=f[i])break;
        }
        printf("%d\n",V-i);
    }
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章