Significant Cups (Codeforces 795B)
Tags: Others, two pointers

Stepan is a very experienced olympiad participant. He has n cups for Physics olympiads and m cups for Informatics olympiads. Each cup is characterized by two parameters — its significance ci and width wi.Stepan decided to expose some of his cups on a shelf with width d in such a way, that:  there is at least one Physics cup and at least one Informatics cup on the shelf,  the total width of the exposed cups does not exceed d,  from each subjects (Physics and Informatics) some of the most significant cups are exposed (i. e. if a cup for some subject with significance x is exposed, then all the cups for this subject with significance greater than x must be exposed too). Your task is to determine the maximum possible total significance, which Stepan can get when he exposes cups on the shelf with width d, considering all the rules described above. The total significance is the sum of significances of all the exposed cups.

Input: The first line contains three integers n, m and d (1 ≤ n, m ≤ 100 000, 1 ≤ d ≤ 109) — the number of cups for Physics olympiads, the number of cups for Informatics olympiads and the width of the shelf.Each of the following n lines contains two integers ci and wi (1 ≤ ci, wi ≤ 109) — significance and width of the i-th cup for Physics olympiads.Each of the following m lines contains two integers cj and wj (1 ≤ cj, wj ≤ 109) — significance and width of the j-th cup for Informatics olympiads.

Output: Print the maximum possible total significance, which Stepan can get exposing cups on the shelf with width d, considering all the rules described in the statement.If there is no way to expose cups on the shelf, then print 0.

Note: In the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals 3 and width equals 2, so after Stepan exposes it, the width of free space on the shelf becomes equal to 6. Also, Stepan must expose the second Physics cup (which has width 5), because it is the most significant cup for Physics (its significance equals 5). After that Stepan can not expose more cups on the shelf, because there is no enough free space. Thus, the maximum total significance of exposed cups equals to 8.

Sample Inputs:
3 1 8
4 2
5 5
4 2
3 2

4 3 12
3 4
2 4
3 5
3 4
3 5
5 2
3 4

2 2 2
5 3
6 3
4 2
8 1

Sample Outputs:
8

11

0

Solution (Python):
(N, M, W) = map(int, input().split())

def get(N):
    AB = []
    for _ in range(N):
        (a, b) = map(int, input().split())
        AB.append((-a, b))
    AB.sort()
    A = [0]
    B = [0]
    for (a, b) in AB:
        A.append(A[-1] - a)
        B.append(B[-1] + b)
    return (A, B)
(A, B) = get(N)
(C, D) = get(M)
ANS = 0
m = M
for n in range(1, N + 1):
    while m > 0 and B[n] + D[m] > W:
        m = m - 1
    if m == 0:
        break
    ANS = max(ANS, A[n] + C[m])
print(ANS)

Solution (Python):
from functools import cmp_to_key

def read_ints():
    return tuple(map(int, input().split()))
(n, m, d) = read_ints()
a = [read_ints() for _ in range(n)]
b = [read_ints() for _ in range(m)]

@cmp_to_key
def cmp(x, y):
    if x[0] != y[0]:
        return y[0] - x[0]
    return x[1] - y[1]
a.sort(key=cmp)
b.sort(key=cmp)
j = -1
val = b[0][0]
cw = b[0][1]
while j < n - 1 and cw + a[j + 1][1] <= d:
    j += 1
    cw += a[j][1]
    val += a[j][0]
sol = 0
if j >= 0:
    sol = val
for i in range(1, m):
    cw += b[i][1]
    val += b[i][0]
    while j > 0 and cw > d:
        cw -= a[j][1]
        val -= a[j][0]
        j -= 1
    if j >= 0 and cw <= d:
        sol = max(sol, val)
    else:
        break
print(sol)

Solution (Python):
from functools import cmp_to_key

def read_ints():
    return tuple(map(int, input().split()))
(n, m, d) = read_ints()
a = [read_ints() for _ in range(n)]
b = [read_ints() for _ in range(m)]

@cmp_to_key
def cmp(x, y):
    if x[0] != y[0]:
        return y[0] - x[0]
    return x[1] - y[1]
a.sort(key=cmp)
b.sort(key=cmp)
j = -1
val = b[0][0]
cw = b[0][1]
while j < n - 1 and cw + a[j + 1][1] <= d:
    j += 1
    cw += a[j][1]
    val += a[j][0]
sol = 0
if j >= 0:
    sol = val
for i in range(1, m):
    cw += b[i][1]
    val += b[i][0]
    while j > 0 and cw > d:
        cw -= a[j][1]
        val -= a[j][0]
        j -= 1
    if j >= 0 and cw <= d:
        sol = max(sol, val)
    else:
        break
print(sol)