Pyramid
Time limit4sMemory limit512 MB
Build an n-row triangular pyramid from a linear recurrence, then answer queries for the maximum value inside a downward triangular sub-pyramid.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Array, Implementation
- Solved
- No attempts yet
Problem
Mirko and Slavko were walking through the desert when they came across a pyramid. A small console on its door shows a riddle made of six integers , , , , , . The door opens only after they build the pyramid those numbers describe and type it into the console.
The pyramid has rows. Row holds numbers , , , , and every value follows these rules.
- . No modulo is applied here, so stays equal to even when .
- for
- for and

Once the door opens, the console asks questions. Each question consists of three integers , , and names the sub-pyramid whose top is and whose side has length . That sub-pyramid holds every cell with and . For example, the sub-pyramid given by , , consists of the three cells , , , drawn in red in the picture. Answer each question with the largest number the sub-pyramid holds.
Input
The first line contains six integers , , , , , separated by spaces (, , , , , ).
The second line contains the number of questions ().
Each of the next lines contains three integers , , (, , ).
Output
Print lines. Line contains the largest number inside the sub-pyramid named by the -th question.