Description
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 are ti and its pages ' width are equal to wI. The thickness of either 1 or 2. All books has the same page heights.
Shaass puts the books on the bookshelf in the following. 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 is 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 all 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 II integers ti and wI denoting the thickness and width of the I-th book correspondingly, (1≤ t I ≤ 2, 1≤ wi ≤100).
Output
On the-line of the the output print the minimum total thickness of the vertical books that we can achieve.
Sample Input
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
Title, the book you can stand upright, horizontally put (must be placed on the top of the book), the need to put the vertical book caused by the thickness of the smaller the better
Since the thickness of the book only 1 22 kinds of cases, then you can prepare two arrays a thickness of 1 of a put thickness of 2, the last greedy, first to two array sort, the length of the big in front, because you put the words, how long the length has nothing to do, the length is very problematic. Then you can start from small to large enumeration, select a few books to put below, calculate the thickness of these several, and then the rest of the books are put on the above, calculate their length, if the thickness is equal to length, exit the loop, output the answer
#include"Iostream"#include"Algorithm"UsingNamespace Std;Constint MAXN=100+10;int n;int FFb;int a[MAXN];int b[MAXN];boolCmp(int A1,int A2){Return A1>a2;}int Suma[MAXN],sumb[MAXN];voidInit(){int TT, TTT; F=fb=0;For(int I=0; I<n; I++){cin>>tt>>ttt;If(TT==1) A[F++]=ttt;If(TT==2) b[FB++]=ttt;}Sort(AA+fCmp);Sort(bB+fbCmp); Suma[0]=0; sumb[0]=0;For(int I=1; I<=f; I+ +) Suma[I]=a[I-1]+suma[I-1];For(int I=1; I<=fb; I+ +) Sumb[I]=b[I-1]+sumb[I-1];}voidWork(){int ans=100000;For(int I=0; I<=f; I++)For(Int J=0; j<=fb; j++){int thick=i+j*2;int width=suma[F]-suma[I]+sumb[FB]-sumb[j];If (Thick>=width&&ans>thick=thick}cout<<ans <<endl;} int main () { Cin>>n; init (); work (); return 0;}
Fourth week of training (efficient algorithm design) I problem (greedy)