Each design picks two length-r intervals whose overlap height adds extra cost; find the k-th smallest total wall height over all interval pairs.
Hard8Binary searchPrefix sumDynamic programmingSortingNo attempts yetTime limit3sMemory limit512 MBYou have become the emperor of Sinai, a small country. To stop the raids coming across the border you decided to build a great wall there, and you hired W Corp, the only company in the world that builds walls nobody can break through.
W Corp builds every wall from the same pattern. The wall is n meters long, and each one meter piece is numbered from 1 to n along its length. Pieces may have different heights. The heights come from three fixed arrays a, b, c of n elements each, with ai<bi<ci for every 1≤i≤n, and from an integer r (1≤r<n). The three arrays and r are the same for every wall W Corp builds.
A specific design is fixed by two integers x and y with 1≤x<y≤n−r+1. Take the two ranges [x,x+r−1] and [y,y+r−1], both inclusive. The height of piece i is:
The strength of a wall is the sum of the heights of its n pieces.
Because a, b, c and r never change, W Corp hands you a price list of every possible design sorted in non-decreasing order of strength. You pick the k-th design on that list. Report the strength of the wall you picked.
The first line contains three integers n, r, k (2≤n≤30000, 1≤r<n, 1≤k≤2(n−r)(n−r+1)): the length of the wall, the length of the chosen ranges, and the position on the price list.
The second line contains the n elements of the array a (1≤ai≤106).
The third line contains the n elements of the array b (ai<bi≤106).
The fourth line contains the n elements of the array c (bi<ci≤106).
Print one integer, the strength of the k-th wall on the price list. Several designs can share the same strength, and the k-th strength value is the same whichever way those ties are ordered.
In the first example there are three walls you can build.