# 【BZOJ1620】[Usaco2008 Nov]Time Management 时间管理

## 描述 Description

Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. He has N jobs conveniently numbered 1..N (1 <= N <= 1,000) to accomplish (like milking the cows, cleaning the barn, mending the fences, and so on). To manage his time effectively, he has created a list of the jobs that must be finished. Job i requires a certain amount of time T_i (1 <= T_i <= 1,000) to complete and furthermore must be finished by time S_i (1 <= S_i <= 1,000,000). Farmer John starts his day at time t=0 and can only work on one job at a time until it is finished. Even a maturing businessman likes to sleep late; help Farmer John determine the latest he can start working and still finish all the jobs on time.

N 个工作, 每个工作其所需时间, 及完成的 Deadline, 问要完成所有工作, 最迟要什么时候开始.

## 输入格式 InputFormat

Line 1: A single integer: N.

Lines 2..N+1: Line i+1 contains two space-separated integers: T_i and S_i.

## 输出格式 OutputFormat

Line 1: The latest time Farmer John can start working or -1 if Farmer John cannot finish all the jobs on time.

10
10 95
3 51
7 89
1 93
3 61
3 65
5 31
7 46
8 79
4 64

26

Silver

BZOJ 1620

## 代码 Code

``````#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define inf 0x7fffffff
struct tim
{
int z,y,x;
}a[1001];
int i,j,t,n,m,l,r,k,ans;
inline bool comp(tim a,tim b)
{
return a.y>b.y;
}
int main()
{
scanf("%d",&n);
for (i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+n+1,comp);
ans=inf;
for (i=1;i<=n;i++) ans=min(ans,a[i].y)-a[i].x;
if (ans<0) printf("-1n");
else printf("%d\n",ans);
return 0;
}``````