This page is still under construction.

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

Coding Test

Time limit2.5sMemory limit1024 MB

Summary
Given A[i] fixed problems per level and B[i] ambiguous problems that can go to level i or i+1, find the maximum number of full sets for each firm's range [L, U].
Level

Medium7 of 10

Topics
Prefix sum, Greedy
Solved
No attempts yet

Problem

Coding tests have grown popular, and a company now makes problems for them and sells the problems to IT companies.

For convenience, the company divides the difficulty of its problems into levels from 00 to N−1N-1. The company currently has A[i]A[i] problems rated at difficulty level ii. It also has B[i]B[i] problems rated at either level ii or level i+1i+1, because the rating is ambiguous. No problems are rated in any other way.

The company is now looking for firms to sell problems to. A total of MM firms have shown interest, numbered from 00 to M−1M-1. Firm jj (0≤j≤M−10 \le j \le M-1) is only interested in problems with difficulty at least L[j]L[j] and at most U[j]U[j].

When selling to firm jj, the company wants to sell one problem of each difficulty from L[j]L[j] to U[j]U[j] as a bundle. Call this bundle a set.

If the company sells problems only to firm jj, what is the maximum number of sets it can sell?

A problem rated at either level ii or level i+1i+1 can be assigned to one of the two levels, so that the number of sets sold is as large as possible. No problem may appear more than once across all the sets sold.

Constraints

  • 2≤N≤1000002 \le N \le 100000
  • 1≤M≤1000001 \le M \le 100000
  • 0≤A[i]≤1080 \le A[i] \le 10^8 for all 0≤i≤N−10 \le i \le N-1
  • 0≤B[i]≤1080 \le B[i] \le 10^8 for all 0≤i≤N−20 \le i \le N-2
  • 0≤L[j]≤U[j]≤N−10 \le L[j] \le U[j] \le N-1 for all 0≤j≤M−10 \le j \le M-1

Examples1

  1. Example 1

    Input
    2 1
    0 0
    0
    1 1
    
    Expected output
    0