Under construction

Still being built. Things may change

Two Decks

Interview

Time limit1sMemory limit1024 MB

Summary
Wonbin removes at most K items from the top or bottom of two stacks to minimize the weight of the heavier backpack he is left carrying.
Level

Medium5 of 10

Topics
Prefix sum, Brute force, Array
Solved
No attempts yet

Problem

Seunghyeong and Wonbin prepared two backpacks for a trip. Each backpack holds NN items.

  • The first backpack has items with weights A1,A2,…,ANA_1, A_2, \dots, A_N stacked from bottom to top. ANA_N is the weight of the item on top.
  • The second backpack has items with weights B1,B2,…,BNB_1, B_2, \dots, B_N stacked from bottom to top. BNB_N is the weight of the item on top.

The weight of a backpack is the sum of the weights of the items left inside it. Wonbin can perform at most KK operations. In each operation, he picks one of the two backpacks and removes the item on the top or the bottom of it. The chosen backpack's weight drops by the weight of the removed item.

After Wonbin finishes, Seunghyeong decides which backpack to carry. Being shrewd, he always carries the lighter of the two. If the two weights are equal, he carries either one. Wonbin must carry the backpack that Seunghyeong does not choose.

Find the minimum possible weight of the backpack Wonbin has to carry.

Input

The first line contains two integers NN and KK (1≤N≤5 0001 \leq N \leq 5\,000, 0≤K≤N0 \leq K \leq N).

The second line contains NN integers A1,A2,…,ANA_1, A_2, \dots, A_N (1≤Ai≤1091 \leq A_i \leq 10^9), the item weights in the first backpack.

The third line contains NN integers B1,B2,…,BNB_1, B_2, \dots, B_N (1≤Bi≤1091 \leq B_i \leq 10^9), the item weights in the second backpack.

Output

Print the minimum possible weight of the backpack that Wonbin has to carry.

Examples2

  1. Example 1

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

    Input
    3 0
    3 1 4
    1 5 9
    
    Expected output
    15