uva10154,

## Problem F: Weights and Measures

I know, up on top you are seeing great sights,
But down at the bottom, we, too, should have rights.
We turtles can't stand it. Our shells will all crack!
Besides, we need food. We are starving!" groaned Mack.

### The Problem

Mack, in an effort to avoid being cracked, has enlisted your advice as to the order in which turtles should be dispatched to form Yertle's throne. Each of the five thousand, six hundred and seven turtles ordered by Yertle has a different weight and strength. Your task is to build the largest stack of turtles possible.
Standard input consists of several lines, each containing a pair of integers separated by one or more space characters, specifying the weight and strength of a turtle. The weight of the turtle is in grams. The strength, also in grams, is the turtle's overall carrying capacity, including its own weight. That is, a turtle weighing 300g with a strength of 1000g could carry 700g of turtles on its back. There are at most 5,607 turtles.
Your output is a single integer indicating the maximum number of turtles that can be stacked without exceeding the strength of any one.

### Sample Input

```300 1000
1000 1200
200 600
100 101
```

`3`

## Source Code:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Using Dynamic Programming;
* dp[i][j] represents: get the minimum weight of choosing j turtles from first
* ith turtles;
*
* For this problem, the list requires to be sorted first, let the turtle with
* max-weight and min-weight located at the last.
*/

/**
* @author antonio081014
* @since Feb 3, 2012, 6:30:30 PM
*/
class Main {
public List<Turtle> list;

public static void main(String[] args) throws Exception {
Main main = new Main();
main.init();
System.out.println(main.solve());
}

public void init() throws Exception {
list = new ArrayList<Turtle>();
String strLine;
while ((strLine = br.readLine()) != null) {
String[] str = strLine.split(" ");
int w = Integer.parseInt(str);
int s = Integer.parseInt(str) - w;
}
}

public int solve() {
Collections.sort(list);
// for (int i = 0; i < list.size(); i++)
// System.out
// .println(list.get(i).weight + ", " + list.get(i).strength);
int N = list.size();
int[][] dp = new int[N][N + 1];
for (int i = 0; i < N; i++)
for (int j = 0; j <= N; j++)
dp[i][j] = (j == 0 ? 0 : Integer.MAX_VALUE);
dp = list.get(0).weight;
for (int i = 1; i < N; i++) {
for (int j = 1; j <= i + 1; j++) {
dp[i][j] = Math.min(dp[i][j], dp[i - 1][j]);
if (list.get(i).strength > dp[i - 1][j - 1]) {
dp[i][j] = Math.min(dp[i][j],
dp[i - 1][j - 1] + list.get(i).weight);
}
}
}
for (int i = N; i >= 0; i--)
if (dp[N - 1][i] != Integer.MAX_VALUE)
return i;
return 0;
}
}

class Turtle implements Comparable<Turtle> {
public int weight;
public int strength;

public Turtle(int w, int s) {
this.weight = w;
this.strength = s;
}

@Override
public int compareTo(Turtle o) {
if (this.strength == o.strength)
return this.weight - o.weight;
return (this.strength) - (o.strength);
}
}