## RGPC '18 P5 - Wormhole

View as PDF

Points: 20 (partial)
Time limit: 1.0s
Memory limit: 64M

Author:
Problem types
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Daniyal the space lord loves the tax that he collects from his space colonies, so he decides to build a weird new colony in the shape of a 4-dimensional "cube". He can describe how big this colony is by the lengths of the four different axes of the colony. However, his citizens complain about a lack of transportation, and so Daniyal tries connecting each set of opposite sides of his city together such that the distance between each point in the colony is drastically shortened (i.e., he rolls the colony into a torus).

It's time to collect the tax! For each citizen in the colony, Daniyal gives citizen a tax estimate , describing how much the citizen will pay him. To make tax collection easy, he defines a neighbourhood to be some "rectangular"-shaped non-empty set of adjacent citizens, and decides to tax only the neighbourhood with the highest collective tax estimate. To make things more "fair", the citizens in that neighbourhood will split the payment equally, with individual payments rounded down. Daniyal is lazy, so help him calculate this average tax payment.

Note: if two neighbourhoods have the same collective tax estimate, Daniyal will only tax the one that minimizes payment per citizen. It is guaranteed that this will break any tie.

#### Input Specification

The first line of input will contain four space-separated integers , , , and , each denoting the length of one of the axes of the colony. The next line will contain space-separated integers, where the th integer represents , the tax estimate for the th citizen.

The integers are presented in order from a corner of the colony to the opposite corner (i.e., the flattened 4D colony, filling up each dimension one at a time).

#### Output Specification

The output should contain a single integer; the rounded-down average tax payment in the highest-paying neighbourhood in the colony.

#### Sample Input 1

2 3 1 1
3 2 5 7 1 -15

#### Sample Output 1

4

#### Explanation

The neighbourhood that will be taxed is that composed of citizens paying 3, 2, 5, and 7 dollars, resulting in a collective tax estimate of 17, split equally across 4 citizens. The citizen paying 1 dollar is not included in this neighbourhood, because to make the neighbourhood "rectangular"-shaped, the citizen paying -15 dollars would also have to be included, making the overall payment non-maximal.

#### Sample Input 2

2 2 3 2
442294612 -386253016 -505717487 122716090 -82536811 650826785 392919 269868536 -681924097 813589289 -272524140 913288054 442835178 -867109293 144211806 -250882696 -751764544 -754975539 -544290979 -159374076 -166647076 331222521 -845133014 -401480867

Note: the above input consists of only two lines.

#### Sample Output 2

661893166