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;
}


Codeforces Round #178 (Div. 2) B .Shaass and Bookshelf的更多相关文章

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

    题目链接:http://codeforces.com/contest/294/problem/B B. Shaass and Bookshelf time limit per test 1 secon ...

  2. Codeforces Round #178 (Div. 2)

    A. Shaass and Oskols 模拟. B. Shaass and Bookshelf 二分厚度. 对于厚度相同的书本,宽度竖着放显然更优. 宽度只有两种,所以枚举其中一种的个数,另一种的个 ...

  3. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  4. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  5. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  6. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  7. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  8. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  9. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

随机推荐

  1. RN性能

    https://facebook.github.io/react-native/docs/performance.html 一个使用RN而不是webView的重要原因是可实现60fps以及更类似原生a ...

  2. Flux reference

    https://facebook.github.io/flux/docs/dispatcher.html#content 首先安装 npm install --save flux Dispatcher ...

  3. appIcon

    原文地址:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconM ...

  4. sort_main_extable

    参考:Linux异常表 1.函数调用关系 asmlinkage void __init start_kernel(void) -->sort_main_extable(); -->sort ...

  5. 【实验吧】Once More&&【笔记】 PHP 函数漏洞总结

    <?php if (isset ($_GET['password'])) { if (ereg ("^[a-zA-Z0-9]+$", $_GET['password']) = ...

  6. PAT Basic 1070

    1070 结绳 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每次串连后,原 ...

  7. leetcode刷题——动态规划

    知识点 专题-B-动态规划 题目 斐波那契数列 矩阵路径 数组区间 分割整数 最长递增子序列 最大连续子序列和 最长公共子序列 最长回文子序列 最长公共子串 最长回文子串 背包 题解 CS-Notes ...

  8. NYOJ-525一道水题思路及详解

    一道水题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 今天LZQ在玩一种小游戏,但是这游戏数有一点点的大,他一个人玩的累,想多拉一些人进来帮帮他,你能写一个程序帮帮他 ...

  9. spring security 登录、权限管理配置

    登录流程 1)容器启动(MySecurityMetadataSource:loadResourceDefine加载系统资源与权限列表)  2)用户发出请求  3)过滤器拦截(MySecurityFil ...

  10. Chrome中输入框默认样式移除

    Chrome中输入框默认样式移除 在chrome浏览器中会默认给页面上的输入框如input.textarea等渲染浏览器自带的边框效果 IE8中效果如下: Chrome中效果如下:   这在我们未给输 ...