{"raw_statement":[{"iden":"statement","content":"Вокруг живописного круглого озера расположено $N$ домов на одинаковом расстоянии $D$ друг от друга. Михаил живет в одном из этих домов, во всех остальных домах живут его друзья. Все нравится Михаилу: и прекрасная природа, и добрые соседи, и мягкий климат, но и в эти места добрался коронавирус... И вот теперь жители самоизолировались, и могут выходить на прогулку только по одному человеку, при этом все остальные находятся в своих домах.\n\nМихаил хочет во время своей прогулки пройти мимо каждого дома, чтобы оставить в почтовом ящике каждого из домов письмо-приветствие своим друзьям. При этом перемещаться между домами он будет по кратчайшему пути. Изначально Михаил выбирает некоторый порядок, в котором он будет посещать дома друзей, после чего начинает свою прогулку. Оставив письмо в почтовом ящике одного дома, он перемещается к другому дому, и так до тех пор, пока не пройдет все дома, после чего зайдет в гости к другу, который живет в последнем посещенном доме.\n\nМихаил, пользуясь возможностью прогуляться, хочет пройти во время своей прогулки как можно большее расстояние. Это расстояние ему нужно сообщить для получения пропуска на выход из дома. В таком важном деле ошибаться Михаилу нельзя. Помогите Михаилу верно вычислить максимальное расстояние, которое он может пройти во время прогулки.\n\nВ единственной строке через пробел записаны два числа: $N$ и $D$ ($3 <= N <= 10^6, 1 <= D <= 10^6$) — количество домов и расстояние между соседними домами соответственно.\n\nВыведите одно число — максимальное расстояние, которое сможет пройти Михаил во время прогулки.\n\n"},{"iden":"входные данные","content":"В единственной строке через пробел записаны два числа: $N$ и $D$ ($3 <= N <= 10^6, 1 <= D <= 10^6$) — количество домов и расстояние между соседними домами соответственно."},{"iden":"выходные данные","content":"Выведите одно число — максимальное расстояние, которое сможет пройти Михаил во время прогулки."},{"iden":"примеры","content":"Входные данные3 5\nВыходные данные10\nВходные данные6 10\nВыходные данные130\n"}],"translated_statement":null,"sample_group":[],"show_order":[],"formal_statement":"Let $N \\in \\mathbb{Z}$ be the number of houses equally spaced on a circle, and $D \\in \\mathbb{R}^+$ be the distance between adjacent houses. The houses lie at vertices of a regular $N$-gon with side length $D$. Michael starts at one house and must visit all other $N-1$ houses exactly once, moving along the perimeter (shortest path between adjacent houses). The goal is to maximize the total distance traveled.\n\nThe circumference of the circle is $C = N \\cdot D$. The maximum path corresponds to visiting the houses in an order that maximizes the sum of consecutive arc lengths, which is achieved by traversing as close as possible to the full circle without revisiting any house.\n\nSince Michael must start at his house and end at the last visited friend’s house, the optimal route is to walk almost the entire circle: starting at house $0$, visit all other houses in one direction (say, clockwise) until the last house, thereby covering a path of length $(N-1) \\cdot D$.\n\nBut this is not maximal: he can instead go in one direction to the farthest house, then backtrack in the opposite direction to cover the remaining houses — this allows him to traverse nearly the full circumference twice, minus the direct arc between the two endpoints.\n\nActually, the maximal path is obtained by visiting houses in an order that forces him to traverse the circle almost twice: he walks from his house to the house diametrically opposite (in one direction), then turns around and walks to the remaining houses in the opposite direction. This yields a path length equal to the full circumference minus the shortest gap between the first and last visited house.\n\nBut since he must visit all $N-1$ other houses exactly once, the optimal strategy is to walk in one direction to the end, then reverse direction and walk back — this covers the entire circle except for one segment.\n\nThe maximum distance is achieved when the path covers the entire circle **except one segment** of length $D$, and then returns along the other side — effectively covering $2(N-1)D - D = (2N - 3)D$? No.\n\nActually, the correct insight: Michael starts at house $0$. He must visit all other $N-1$ houses. The longest possible path is to walk clockwise to house $N-1$, then continue counterclockwise back to house $1$ — but that would revisit.\n\nWait — he cannot revisit houses. He must visit each of the other $N-1$ houses exactly once.\n\nSo the problem reduces to: **Given $N$ points on a circle, find the longest Hamiltonian path starting from a fixed point, visiting each other point exactly once, moving only along the circle (i.e., along the perimeter).**\n\nThe maximum length is achieved by going as far as possible in one direction, then turning around and going the rest of the way in the other direction — i.e., the path that covers the entire circle except for the direct arc between the first and last house.\n\nThe maximum distance is:\n\n$$\n\\text{Max distance} = (N - 1) \\cdot D + (N - 2) \\cdot D = (2N - 3) \\cdot D\n$$\n\nWait — no. Let’s think geometrically.\n\nLabel houses $0, 1, 2, \\dots, N-1$ clockwise, Michael starts at house $0$.\n\nHe can go clockwise to $1, 2, \\dots, N-1$: distance = $(N-1)D$\n\nOr he can go counterclockwise to $N-1, N-2, \\dots, 1$: same distance.\n\nBut to maximize, he can go one way to the farthest point, then turn around.\n\nFor example: go clockwise to house $k$, then counterclockwise to the remaining houses.\n\nThe optimal is to go from 0 to $N-1$ clockwise: distance $(N-1)D$, then from $N-1$ counterclockwise to $1$: distance $(N-2)D$, total = $(2N - 3)D$\n\nYes — because from 0 to $N-1$ clockwise: $N-1$ steps.\n\nFrom $N-1$ to $1$ counterclockwise: passes through $N-2, N-3, \\dots, 2, 1$ → that’s $N-2$ steps.\n\nTotal: $(N-1) + (N-2) = 2N - 3$ segments → distance = $(2N - 3)D$\n\nThis visits all $N-1$ other houses exactly once.\n\nAnd this is maximal: any other path will leave a longer gap untraversed.\n\nThus, the maximum distance is:\n\n$$\n(2N - 3) \\cdot D\n$$\n\n**Definitions**\nLet $N \\in \\mathbb{Z}$ be the number of houses arranged in a circle, and $D \\in \\mathbb{R}^+$ be the distance between adjacent houses. Michael starts at one fixed house and must visit the remaining $N-1$ houses exactly once, moving only along the perimeter.\n\n**Constraints**\n$3 \\le N \\le 10^6$, $1 \\le D \\le 10^6$\n\n**Objective**\nMaximize the total distance traveled along the perimeter, visiting each of the other $N-1$ houses exactly once. The maximum distance is:\n$$\n(2N - 3) \\cdot D\n$$","simple_statement":"You are given N houses evenly spaced around a circular lake, each D units apart. Michael starts at one house and wants to visit all other N-1 houses exactly once, walking the shortest path between houses. He can choose any order to visit them. What’s the maximum total distance he can walk before returning to his final destination?","has_page_source":false}