Under construction

Still being built. Things may change

Vault Robbery

Time limit3sMemory limit1024 MB

Summary
Find the maximum loot on a route that ends at building E, moving only to visible buildings and never robbing two consecutive visited buildings, as values and E change over Q updates.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Segment tree
Solved
No attempts yet

Statement

The capital of GSHS country has NN buildings in a row, numbered 1,2,⋯ ,N1, 2, \cdots, N from left to right. The height of building ii is HiH_i, and all building heights are distinct. Each building has a vault on its roof, and the vault in building ii holds CiC_i won. HiH_i and CiC_i are positive integers.

You are a thief who robs the vaults of the capital. You start on the roof of any building and move between buildings while robbing vaults. After the robbery, your helper sends a helicopter to the roof of building EE to take you out of the city. So the last building you reach must be building EE. For safety, you must follow these rules.

  1. To move from building ii to building jj, building jj must be visible from the roof of building ii. Building jj is visible from building ii if every building between them (including buildings ii and jj) has height at most the height of building jj. Otherwise, another building blocks the view.
  2. While moving, you may rob at most one of any two consecutively visited buildings.
  3. For each visited building, your gain is CiC_i if you rob it and 0 otherwise. The total gain is the sum of the gains over all visited buildings.

The building heights are fixed, but the vault values can change, and the helper's destination building EE can also change. Each time the information is updated, compute the maximum total gain you can earn under the rules above.

Input

The first line contains two integers NN and EE, separated by a space.

The second line contains NN integers H1,H2,⋯ ,HNH_1, H_2, \cdots, H_N, separated by spaces.

The third line contains NN integers C1,C2,⋯ ,CNC_1, C_2, \cdots, C_N, separated by spaces.

The fourth line contains an integer QQ.

Each of the next QQ lines is one of the following updates.

  • 1 i c: the vault value in building ii changes to cc won.
  • 2 e: the helper's destination EE changes to ee.

Output

Print Q+1Q+1 lines. The first line contains the maximum gain for the initial state. Line i+1i+1 contains the maximum gain after applying the first ii updates (1≤i≤Q1 \le i \le Q).

Constraints

  • 1≤N≤1051 \le N \le 10^5
  • 1≤E≤N1 \le E \le N
  • 1≤Hi≤N1 \le H_i \le N, and the HiH_i are distinct.
  • 1≤Ci≤1091 \le C_i \le 10^9
  • 0≤Q≤1050 \le Q \le 10^5

Hint

You may start at the same building as EE.

Examples2

  1. Example 1

    Input
    5 5
    1 2 3 4 5
    1 3 4 2 5
    0
    
    Expected output
    10
    
  2. Example 2

    Input
    7 4
    1 5 2 7 3 6 4
    1 4 7 3 3 6 1
    1
    1 5 8
    
    Expected output
    10
    11