#include #include // Required for std::swap // Use long long for coordinates and answer to avoid potential overflow, // as W*H can exceed the capacity of a 32-bit integer for W, H up to 10^5. using namespace std; int main() { // Optimize input/output operations for potentially large inputs. // This is a standard practice in competitive programming to speed up I/O. ios_base::sync_with_stdio(false); cin.tie(NULL); long long W, H; // Declare W and H as long long to handle large inputs. cin >> W >> H; // Read the input values for W and H. // Ensure W <= H by swapping if necessary. // This simplifies the case analysis logic below, making it easier to handle // edge cases like W=0 or W=1, and the W=H condition check when both W and H are even. if (W > H) { swap(W, H); } long long ans; // Variable to store the final computed answer. // Case 1: The grid is essentially a line or a single point (one dimension is 0). if (W == 0) { // If W=0, the grid points are (0, y) for 0 <= y <= H. if (H == 0) { // If W=0 and H=0, the grid contains only the point (0,0). // A path visiting just one point has 0 length and therefore 0 segments. ans = 0; } else { // If W=0 and H > 0, the points form a vertical line segment from (0,0) to (0,H). // The only possible path covering all points is this line itself (or its reverse). // This path consists of a single line segment. ans = 1; } } // Case 2: The grid is thin (the smaller dimension W is 1). else if (W == 1) { // The grid points are (0, y) and (1, y) for 0 <= y <= H. This is a 2 x (H+1) grid. // It has been shown or derived that for grids where one dimension is 1 (W=1 or H=1), // it's possible to construct a Hamiltonian path that makes a turn at every possible // intermediate point. Such a path has the maximum possible number of segments, which is N-1, // where N is the total number of points. // N = (W+1)(H+1) = (1+1)(H+1) = 2(H+1). // Max segments = N-1 = 2(H+1)-1 = 2H + 2 - 1 = 2H + 1. // This formula can also be expressed as W*H + W + H = 1*H + 1 + H = 2H + 1. ans = 2 * H + 1; } // Case 3: Both dimensions are at least 2 (W >= 2 and H >= 2). else { // For grids where both dimensions are at least 2, the maximum number of segments // depends on the parity (even or odd) of W and H. // Subcase 3a: Both W and H are even. if (W % 2 == 0 && H % 2 == 0) { // The formula for this case was derived based on analyzing sample cases, // including the large one provided, and identifying a pattern. // If W=H, the formula simplifies to W*H + W. // If W