This page is still under construction.

Parts of this page are still being built. What you see may change.

Monkey

Time limit2sMemory limit1024 MB

Summary
Given M allowed (x, y) handle pairs with banana counts on each handle, find the maximum total bananas collectable along a monotone path that only increases x or y.
Level

Hard8 of 10

Topics
Dynamic programming, Sorting, Combinatorics, Array
Solved
No attempts yet

Problem

Two pillars A and B stand side by side. Each pillar has NN handles, numbered 1 through NN from bottom to top. Each pillar has zero or more bananas hanging on it. AiA_i is the number of bananas hanging on handle ii of pillar A, and BjB_j is the number of bananas hanging on handle jj of pillar B. These values are integers between 0 and 10910^9 inclusive.

The monkey can grab handles on different pillars with its two arms. Note that it never grabs two handles on the same pillar. Also, the monkey cannot grab just any handle. A pair of handles the monkey can grab is written as (x,y)(x, y), meaning it can grab handle xx of pillar A and handle yy of pillar B at the same time. The monkey then eats all bananas remaining on those two handles. As expected, a banana once eaten is gone. There are MM such ordered pairs in total.

Initially the monkey starts at one of the grabbable pairs of handles. When the monkey is at (x,y)(x, y), it can move to another grabbable pair (x′,y′)(x', y') only if x<x′x < x' and y=y′y = y', or x=x′x = x' and y<y′y < y'.

Naturally, the monkey wants to eat as many bananas as possible. Given the grabbable handles and the numbers of bananas hanging on them, write a program that finds the maximum number of bananas the monkey can eat.

Constraints

  • 1≤N≤500 0001 \le N \le 500\,000
  • 1≤M≤500 0001 \le M \le 500\,000
  • M≤N2M \le N^2
  • 0≤Ai≤1090 \le A_i \le 10^9 (1≤i≤N)(1 \le i \le N)
  • 0≤Bi≤1090 \le B_i \le 10^9 (1≤i≤N)(1 \le i \le N)
  • Every ordered pair (x,y)(x, y) given as the input P is distinct, and satisfies 1≤x≤N1 \le x \le N, 1≤y≤N1\le y \le N.

Examples1

  1. Example 1

    Input
    1 1
    0
    0
    1 1
    
    Expected output
    0