Under construction

Still being built. Things may change

Museum Visit

Interview

Time limit4sMemory limit1024 MB

Summary
Choose visit days with the smallest total discomfort so that every exhibition's day range contains at least one chosen day.
Level

Medium5 of 10

Topics
Dynamic programming, Intervals, Sliding window
Solved
No attempts yet

Problem

Every day is different in the Groninger Museum. Some days are nice, peaceful and quiet, and you can spend all day looking at the beautiful paintings, sculptures, and other artworks. Other days are busier, when weekends or public holidays fill the museum with hasty visitors, higher prices and screaming children. The discomfort varies a lot: some busy days are better because of the extra studentenkorting (student discount), and some quiet days get worse because of earthquake risks.

The museum also hosts special limited-time exhibitions, such as those on the local football club FC Groningen, the Martinitoren, or the eierbal (a local delicacy). These exhibitions are irregular. Some last for weeks, some last only a day, and several may open on the same day.

As a proud Grunneger, you want to visit each exhibition at least once. You subscribed to the newsletter, so you know the start and end days of all exhibitions in advance. You are also a regular visitor, so you know exactly how much discomfort you will receive on any given day.

On which days should you visit the museum to minimize your total discomfort while still seeing every exhibition planned in the foreseeable future?

For example, in the first sample, the best plan is to visit the first two exhibitions on the second day and the last exhibition on the fourth or fifth day.

Input

The input consists of:

  • One line with two integers nn and mm (1≤n,m≤2⋅1051\leq n,m\leq 2\cdot10^5), the number of days in the foreseeable future and the number of exhibitions planned in those days.
  • One line with nn integers cc (1≤c≤1091\leq c\leq 10^9), where cic_i is the discomfort you receive when you visit on day ii.
  • mm lines, each with two integers ss and ee (1≤s≤e≤n1\leq s\leq e\leq n), the start and end day of an exhibition. Both days are inclusive, so the exhibition can be visited on day ss, on day ee, and on any day in between.

Output

Output the minimum total discomfort you receive while visiting all exhibitions.

Examples3

  1. Example 1

    Input
    5 3
    1 1 3 1 1
    1 3
    2 3
    3 5
    
    Expected output
    2
    
  2. Example 2

    Input
    6 3
    1 2 4 4 2 1
    1 4
    2 5
    3 6
    
    Expected output
    3
    
  3. Example 3

    Input
    11 2
    3 1 4 1 5 9 2 6 5 3 5
    5 10
    1 1
    
    Expected output
    5