Castle Invasion

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 1.0s
PyPy 2 2.5s
PyPy 3 2.5s
Memory limit: 64M
PyPy 2 256M
PyPy 3 256M

Authors:
Problem type
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

Bob is planning an attack on a suspicious castle owned by the evil villain Joe! The castle can be thought of as an N by N grid of towers, with each tower having an integer height. To scout for this attack, Bob sent his only drone that can instantly transmit pictures to take photos of the castle. However, the drone only managed to take 2 photos before critically running out of battery and tumbling into the forest. One photo was taken of the front side of the castle, while the other captured its right side. It was also discovered (slightly too late) that the drone's camera cannot capture depth! Since Bob does not want to fail this attack, he would like to know the maximum possible volume of the castle, or if the photos are incorrect and a castle cannot be reconstructed. Can you help him find out?

Input Specification

The first line of the input will contain an integer N, representing the dimensions of the castle.

The second line will contain N integers c_i (1 \le i \le N), representing the height of the tallest tower in the ith column.

The third line will contain N integers r_j (1 \le j \le N), representing the height of the tallest tower in the jth row.

Output Specification

A single integer, representing the maximum possible volume of the castle, or -1 if it is impossible to reconstruct a castle.

Constraints

For all subtasks,

1 \le N \le 10^6

1 \le c_i, r_j \le 10^6

Subtask 1 [30%]

1 \le N \le 1\,000

Subtask 2 [70%]

No additional constraints.

Sample Input 1

4
1 3 4 2
2 2 1 4

Sample Output 1

28

Sample Input 2

4
1 2 6 2
5 3 3 3

Sample Output 2

-1

Explanation for Sample Output 2

No matter how you assign heights to each tower, it is impossible to reconstruct a castle that satisfies both photos.


Comments


  • 0
    Mark123  commented on Oct. 12, 2020, 1:39 a.m.

    can someone help me, I keep getting the second last case wrong.


    • 0
      AlanL  commented on Oct. 17, 2020, 8:00 p.m.

      Your multiplication on line 50 is overflowing, as mentioned below.


    • 0
      HenryYi  commented on Oct. 17, 2020, 5:41 p.m.

      if you get out of memory error, then try different input tream. static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));


  • 1
    ross_cleary  commented on March 19, 2020, 4:41 p.m.

    Can someone take a look at my code, I am wrong answering on the second last case.


    • 2
      4fecta  commented on March 19, 2020, 6:55 p.m.

      Some of your variables declared as int are overflowing.


  • 8
    pblpbl  commented on Oct. 19, 2019, 9:56 a.m.

    I finally got this after 3 days! This is a good problem.


    • 5
      4fecta  commented on Oct. 19, 2019, 2:25 p.m.

      I admire your persistence.


  • 8
    d  commented on Oct. 16, 2019, 3:22 p.m.

    no, it is not necessary to create an n by n grid


    • 0
      pblpbl  commented on Oct. 17, 2019, 5:34 p.m.

      i'm still stuck on the second last case... hint pls?


      • 4
        4fecta  commented on Oct. 17, 2019, 11:31 p.m. edited

        Your code has a time complexity of O(N^2), which is too slow to pass the second subtask. Try thinking of how you can optimize your algorithm to a better complexity.


  • 0
    pblpbl  commented on Oct. 16, 2019, 1:57 p.m. edited

    Is an N by N grid required to solve this problem? I am getting a memory error at the second last test case.