/*
greedy method
sort the leaflets by Li by descending order
let sumTk = Ti + T2 + ..... Tk
answer = max ( sumTk + Lk), k = 1...n
proofproof by contradiction :
假设最优序列为 a = { 1, 2, 3 ....i, j ... n } (j = i + 1, Li < Lj )
构造序列 b = { 1,2,3 .....j, i ....n }
ans(a) = max {sumT(i-1)+Ti+Li, sumT(i-1)+Ti+Tj+Lj, sumTk+Lk }, k != i, j
ans(b) = max {sumT(i-1)+Tj+Lj, sumT(i-1)+Tj+Ti+Li, sumTk+Lk }, k != i, j
because Li < Lj, so Ti+Tj+Lj > Tj+Ti+Li
and it is obvious that Ti+Tj+Lj > Tj+Lj
we come to conclusion: ans(a) >= ans(b) , this means a is not the best ans.
*/
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
typedef struct {
int T;
int L;
}TL;
int cmp(const void* a, const void* b) {
TL* pa = (TL*)a;
TL* pb = (TL*)b;
return (pb->L) - (pa->L);
}
int main() {
int n, i;
TL tl[SIZE];
scanf("%d", &n);
for (i=0; i<n; ++i) {
scanf("%d", &tl[i].T);
}
for (i=0; i<n; ++i) {
scanf("%d", &tl[i].L);
}
qsort(tl, n, sizeof(TL), cmp);
int s = 0;
int e;
int ans = 0;
for (i=0; i<n; ++i) {
s += tl[i].T;
e = s + tl[i].L;
if (e > ans) {
ans = e;
}
}
printf("%d\n", ans);
return 0;
}
sgu 259
最新推荐文章于 2024-07-06 00:20:02 发布