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. Java8特性详解 lambda表达式 Stream【转】

    本文转自http://www.cnblogs.com/aoeiuv/p/5911692.html 1.lambda表达式 Java8最值得学习的特性就是Lambda表达式和Stream API,如果有 ...

  2. 如何用纯 CSS 创作一个蝴蝶标本展示框

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/xzgZzQ 可交互视频教 ...

  3. JS中关于clientWidth offsetWidth scrollWidth 等的含义的详细介绍

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  4. struts向网页输出图片验证码

    前言:今天做个功能需要展示图片到页面,并不是下载,在网上搜了老半天,大部分都是下载,有的话也是只能在IE下进行输出,其它浏览器就都是下载了. Action代码: public String proce ...

  5. centos7 安装nodejs 最新版

    笔者在安装时,node为11.0.0版本.这里以11版本为例,以后更新,安装步骤时一致的. 下载node安装包到指定目录 wget https://npm.taobao.org/mirrors/nod ...

  6. 用openrowset函数操作远程数据库

    OPENROWSET 包含访问 OLE DB 数据源中的远程数据所需的全部连接信息.当访问链接服务器中的表时,这种方法是一种替代方法,并且是一种使用 OLE DB 连接并访问远程数据的一次性的.特殊的 ...

  7. c#笔记2019-01-06

    using System; using System.Collections.Generic; using System.Linq; using System.Text; /*2019-01-06C# ...

  8. HDU 5483 Nux Walpurgis

    Nux Walpurgis Time Limit: 8000ms Memory Limit: 131072KB This problem will be judged on HDU. Original ...

  9. 2017年 湘潭邀请赛(湖南)or 江苏省赛

    这套题是叉姐出的,好难啊,先扫一遍好像没有会做的题了,仔细一想好像D最容易哎 Super Resolution Accepted : 112   Submit : 178 Time Limit : 1 ...

  10. 在table第一行前插入一行

    在table的第一行前插入一行. //追加到第一行 var html="<tr><td>xxId</td><td>xxName</td& ...