#CCC25J5. Connecting Territories
Connecting Territories
Problem Description
Ava is playing a strategy game on a grid of tiles. Each tile has a cost, forming a repeating sequence from 1 to M
across the grid (1, 2, 3, …, M, 1, 2, 3, …).
Ava must buy one tile in each row to create a path connecting the upper and lower territories. The first tile must be in the first row, and each subsequent tile must share an edge or corner with the previous one.
Your task is to determine the minimum cost of a connecting path.
Input Specification
- The first line contains an integer
R
, the number of rows. - The second line contains an integer
C
, the number of columns. - The third line contains an integer
M
(≤ 100000), the maximum cost of a tile.
Output Specification
Output a single integer — the minimum cost of the connecting path.
Sample Input
3
5
7
Sample Output
6