We need to assign each of n positions to either 0 (rose) or 1 (lily) to maximize sum over m given segments of (number of roses in segment) * (number of lilies in segment). Let's denote for segment S: let a = number of roses, b = number of lilies, length L = a+b. Then beauty = ab = a(L-a) = aL - a^2. So it's a concave quadratic in a (or b). For a fixed segment length L, beauty is maximized when a = L/2 (as close as possible), i.e., equal numbers of roses and lilies if L even, or balanced if odd. However, the assignment must be consistent across overlapping segments.
This is an optimization problem: maximize sum over segments (x_i ... )? We can think of binary variables x_i = 0 for rose, 1 for lily (or maybe let's define variable indicating lily). Then for segment [l,r], let S = sum_{i=l}^r x_i = number of lilies, and length = L, then number of roses = L - S. Then beauty = S * (L - S) = S L - S^2.
So total objective = sum_{segments} (L_j * S_j - S_j^2). Since L_j are constants, objective = sum_j L_j S_j - sum_j S_j^2.
Now S_j = sum_{i in segment j} x_i, x_i ∈ {0,1}.
Thus objective = sum_j L_j * (∑{i in j} x_i) - sum_j (∑{i in j} x_i)^2.
Write ∑j L_j (∑{i∈j} x_i) = ∑i x_i * (∑{j: i∈j} L_j). So linear term with coefficient w_i = ∑_{j: i∈j} L_j.
Now quadratic term: -∑j (∑{i∈j} x_i)^2 = -∑j (∑{i∈j} x_i^2 + 2∑{i<k, i,k∈j} x_i x_k). Since x_i^2 = x_i (binary), we have = -∑j (∑{i∈j} x_i) - 2∑j ∑{i<k, i,k∈j} x_i x_k? Wait careful: (∑ x_i)^2 = ∑ x_i^2 + 2∑{i<k} x_i x_k. Since x_i binary, x_i^2 = x_i. So = ∑ x_i + 2∑_{i<k} x_i x_k. So -∑_j (∑ x_i)^2 = -∑j (∑{i∈j} x_i) - 2∑j ∑{i<k∈j} x_i x_k.
Now combine linear terms from both parts: The linear term from the first part: ∑_i w_i x_i. The linear term from quadratic part: -∑j ∑{i∈j} x_i = -∑_i (number of segments covering i) * x_i = -∑_i c_i x_i, where c_i = number of segments that include i.
So total linear coefficient: (w_i - c_i) = ∑j (L_j - 1) for each j covering i? Actually w_i = ∑{j: i∈j} L_j. So linear term is ∑i (∑{j: i∈j} L_j - c_i) x_i = ∑i (∑{j: i∈j} (L_j - 1)) x_i.
Quadratic term: -2 ∑j ∑{i<k∈j} x_i x_k. So total objective: ∑i α_i x_i - 2 ∑j ∑{i<k∈j} x_i x_k, with α_i = ∑{j: i∈j} (L_j - 1). Since we want to maximize, we can treat it as a quadratic binary optimization problem. However, the quadratic term has negative coefficients (since -2 < 0) on each product term, meaning it's submodular? Actually negative coefficients on product terms encourage the variables to be different? Wait: For a product term x_i x_k, if both are 1 then penalty -2 * 1 = -2 (since objective is to maximize, negative contribution reduces objective). If at least one is 0, product term 0. So having both 1 yields a penalty relative to having them not both 1. Similarly, linear terms can be positive or negative. But overall objective is not necessarily trivial.
But note that we have n,m ≤ 1000. That's not huge. Could we solve exactly using DP? Since the segments are arbitrary and the objective sums over segments, it might be tackled by some form of local search or maybe we can see that the optimal assignment is alternating pattern? Let's test small examples.
Observation: For any single segment, the beauty is maximized when number of roses and lilies are as balanced as possible. That suggests that globally, we want each segment to have roughly half and half. However, the segments overlap, so we need a global assignment that balances counts across all segments.
This problem reminds me of "maximizing sum of products" which often reduces to some kind of quadratic programming. But with n,m up to 1000, we might be able to solve via reduction to maximum cut? Let's examine sign of coefficients.
Rewrite the objective as constant plus something? Let’s express in terms of both 0/1 or maybe use ±1 variables? Let’s define y_i = 2x_i - 1? That transforms to spin variables ±1. However, perhaps there is a known result: For maximizing sum of ab over intervals, the optimal assignment is to alternate 0 and 1? But examples: In first example n=5, m=3, segments [1,3],[2,4],[2,5], output 01100 means positions: 0 (rose),1(lily),1(lily),0(rose),0(rose). So pattern: 0,1,1,0,0. That's not alternating. In second example output 110010: 1,1,0,0,1,0 -> not alternating either.
Maybe the problem can be solved by converting to a maximum flow/min cut? Since quadratic binary optimization with negative interaction coefficients (i.e., repulsive) and arbitrary linear terms can be cast as a graph cut problem if the quadratic terms are submodular. Check submodularity: For a function of binary variables, a pairwise term f(x_i, x_k) is submodular if f(0,0)+f(1,1) ≤ f(0,1)+f(1,0). In our objective, we have a term -2 x_i x_k (plus some linear terms). Let's compute: if we include only the pairwise contribution from all segments where i,k co-appear, the sum of -2*(# segments covering both i and k) x_i x_k. So pairwise term: -2 w_{ik} x_i x_k, where w_{ik} = number of segments covering both i and k. For submodularity, we need -2 w (00) + -2 w (11) ≤ -2 w (01) + -2 w (10) => -2w0 + -2w1 ≤ -2w0 + -2w0 => -2w ≤ 0 which holds because w≥0. So it is submodular. But we have linear terms that can be absorbed. So the objective is a submodular function (since it's sum of submodular components). Maximizing a submodular function is NP-hard in general, but here the structure is special: intervals, and the objective is quadratic with specific coefficients. Also n,m up to 1000, maybe we can solve via DP because the constraints are intervals? Possibly there is a polynomial solution for this specific objective.
We need to find a binary string maximizing sum over intervals of (count0 * count1). This is reminiscent of maximizing sum of variances? Alternatively, we can think in terms of balancing.
Another approach: Since n,m ≤ 1000, we could try to use integer programming or some heuristics? But the problem expects an exact solution; it's from Codeforces likely (problem 713B? not sure). It might have a known solution: the optimal assignment is to put roses and lilies in a way that the sequence is sorted by some "pressure" value. Could be solved via linear programming relaxation? But given constraints are only 1000, we can do a greedy or maybe use the fact that the objective can be expressed as sum over all pairs of positions of some weight times (x_i xor x_k) etc.
Let's try to expand the sum of beauties differently. For each segment j, beauty = (∑{i∈j} (1 - x_i)) * (∑{i∈j} x_i) = (∑ x_i)(L_j - ∑ x_i). Expand: = L_j ∑ x_i - (∑ x_i)^2. That's what we had.
Alternative: Sum of beauties = ∑j (∑{i∈j} x_i) (∑{k∈j} (1 - x_k)) = ∑j ∑{i∈j} ∑{k∈j} x_i (1 - x_k). Then = ∑j ∑{i,k∈j} x_i - x_i x_k = ∑j (∑{i∈j} ∑{k∈j} x_i) - ∑j ∑{i,k∈j} x_i x_k. But careful: ∑{i∈j} ∑{k∈j} x_i = L_j * ∑{i∈j} x_i. Then = ∑_j L_j S_j - ∑_j (S_j^2). Same.
Maybe we can rewrite as sum over ordered pairs (i,k) of contributions: For each pair (i,k), consider how many segments include both i and k. Then x_i (1 - x_k) contributes to those segments. Or symmetrically, beauty = ∑j ∑{i<k} [x_i(1-x_k) + x_k(1-x_i)]? Because for each unordered pair (i,k) in segment, either one is lily and other rose gives contribution 1? Actually check: In segment, product of rose count and lily count: For each unordered pair (i,k) where i and k are distinct, does it contribute? Let's compute: (∑ x)(∑ (1-x)) = ∑_i x_i * ∑k (1-x_k) = ∑{i,k} x_i (1-x_k) = ∑_i x_i (L - ∑_k x_k)?? That's not helpful.
But expand: ∑{i,k} x_i (1-x_k) = ∑i x_i ∑k (1-x_k) = S * (L - S). That includes i=k terms: when i=k, x_i (1-x_i) = 0. So the diagonal terms are zero. So it's sum over ordered pairs (i,k) with i possibly equal to k? Actually i=k gives x_i(1-x_i)=0. So it's sum over all ordered pairs (i,k) (including i=k) but those vanish. So we can treat as sum over all ordered pairs (i,k), i≠k: x_i (1-x_k). Then the total for a segment is ∑{i∈j} ∑{k∈j, k≠i} x_i (1-x_k). But note each unordered pair (i,k) appears twice: once as (i,k) and once as (k,i). So beauty = (1/2) ∑{i≠k, i,k∈j} [x_i (1-x_k) + x_k (1-x_i)] = (1/2) ∑{i≠k, i,k∈j} (x_i + x_k - 2 x_i x_k)?? Let's compute: x_i(1-x_k) + x_k(1-x_i) = x_i - x_i x_k + x_k - x_k x_i = x_i + x_k - 2 x_i x_k. So beauty = (1/2) ∑{i≠k} (x_i + x_k - 2 x_i x_k). Sum_{i≠k} x_i = (L-1) ∑i x_i because each x_i appears in (L-1) pairs (with each other k). Similarly ∑{i≠k} x_k = same. So ∑{i≠k} (x_i + x_k) = 2 (L-1) S. And ∑{i≠k} 2 x_i x_k = 2 * (∑{i≠k} x_i x_k) = 2 * (S^2 - S) because ∑{i,k} x_i x_k = S^2, and diagonal gives S. So beauty = 1/2 [2 (L-1) S - 2 (S^2 - S)] = (L-1) S - (S^2 - S) = (L-1)S - S^2 + S = L S - S^2, matches. So that's consistent.
Thus we can think of contributions from each unordered pair (i,k) of positions to segments that include both i and k. Specifically, for each unordered pair (i,k), consider the set of segments that include both i and k. In each such segment, the unordered pair contributes (x_i + x_k - 2 x_i x_k)/2? Actually from above, the total beauty can be expressed as sum over unordered pairs (i,k) of something times the number of segments covering both? Let's derive:
Beauty = ∑j (∑{i<k∈j} (x_i(1-x_k) + x_k(1-x_i))) because each unordered pair appears exactly once in the double sum but we counted each ordered pair? Actually from earlier, we had beauty = ∑_{i,k∈j, i≠k} x_i(1-x_k). That's ordered pairs, and each unordered pair appears twice: (i,k) and (k,i). But if we sum over unordered pairs, we would need to divide by something. But perhaps we can sum over unordered pairs and each unordered pair contributes something that is independent of order.
Let's attempt: beauty_j = ∑{i<k∈j} [x_i(1-x_k) + x_k(1-x_i)] = ∑{i<k∈j} (x_i + x_k - 2 x_i x_k).
Thus total beauty = ∑{i<k} (x_i + x_k - 2 x_i x_k) * w{ik}, where w_{ik} = number of segments that include both i and k. Because each segment j contributes to each unordered pair (i,k) within it exactly that term.
So objective = ∑{i<k} w{ik} (x_i + x_k - 2 x_i x_k).
Now separate: ∑{i<k} w{ik} (x_i + x_k) = ∑i x_i * (∑{k≠i} w_{ik}) (since each term w_{ik} x_i appears for each k>i or <i, but careful: For fixed i, each pair (i,k) with k>i gives w_{ik} x_i, and also (k,i) with k<i gives w_{ki} x_i, but since w_{ik}=w_{ki} symmetric, total coefficient of x_i from the sum over i<k of w_{ik} x_i is: for each k>i, w_{ik} x_i. Also in sum over i<k, terms w_{ik} x_k also appear, but they contribute x_k, not x_i. So to get coefficient for x_i, we sum all occurrences where x_i appears: that is in terms (i,k) for k>i as x_i, and in terms (k,i) for k<i as x_i. In the expression ∑{i<k} w{ik} (x_i + x_k), we have for each unordered pair, both x_i and x_k appear exactly once. So each pair (i,k) contributes w_{ik} x_i and w_{ik} x_k. So total coefficient for x_i is ∑{k≠i} w{ik}.
Thus linear part: ∑i (∑{k≠i} w_{ik}) x_i.
Quadratic part: -2 ∑{i<k} w{ik} x_i x_k.
Thus objective = ∑i A_i x_i - 2 ∑{i<k} w_{ik} x_i x_k, where A_i = ∑{k≠i} w{ik}.
Now compare with earlier expression: we had linear coefficient α_i = ∑{j: i∈j} (L_j - 1). Let's see if they match. Since w{ik} = number of segments covering both i and k. Then A_i = ∑{k≠i} w{ik} = total number of pairs (i,k) over segments, i.e., sum over segments covering i of (L_j - 1) because for a given segment j that includes i, it contributes to w_{ik} for each other k in that segment. So indeed A_i = ∑_{j: i∈j} (L_j - 1) = α_i. So consistent.
Thus we have an Ising model with external fields A_i and anti-ferromagnetic pairwise interactions (since coefficient -2 w_{ik} is negative, encouraging x_i and x_k to not both be 1). This is like a weighted independent set? Not exactly, because there is also linear terms encouraging some nodes to be 1.
We can consider transformation to a cut problem: Let's set y_i = 1 if assigned lily (1), and maybe we want to choose a set S of positions to be lilies. Then objective f(S) = ∑{i∈S} A_i - 2 ∑{i<k, i,k∈S} w_{ik}. This is a set function: f(S) = ∑{i∈S} A_i - 2 ∑{i,k∈S, i<k} w_{ik}. Since w_{ik} ≥ 0, the second term penalizes having both i and k in S, with penalty 2w_{ik}. So it's like we have a graph with vertices 1..n, each vertex i has weight A_i (which could be positive or negative?), and edges (i,k) with weight 2 w_{ik}. The objective is to choose S to maximize total weight of selected vertices minus sum of edge weights within S. This is exactly a maximum weight closure? Actually it's a standard problem: maximize ∑ w_i x_i - ∑{i<j} c{ij} x_i x_j with c_{ij} ≥ 0, which can be reduced to a minimum cut (or maximum cut?) by complementing variables? It is a submodular function and can be minimized via min cut if we want to minimize a submodular function? But we want to maximize. However, maximizing a submodular function is NP-hard in general. But this particular form (quadratic pseudo-Boolean with negative interactions) can be transformed to a max cut? Let's see: The function is f(x) = ∑ a_i x_i - ∑{i<j} b{ij} x_i x_j, with b_{ij} ≥ 0. This is a supermodular function? Actually we can complement some variables to transform. Since x_i ∈ {0,1}, define y_i = 1 - x_i. Then x_i = 1 - y_i. Substitute: f = ∑ a_i (1 - y_i) - ∑ b_{ij} (1 - y_i)(1 - y_j) = constant + ∑(-a_i) y_i - ∑ b_{ij} (1 - y_i - y_j + y_i y_j) ... Wait compute properly.
Let’s do: f(x) = ∑ a_i x_i - ∑{i<j} b{ij} x_i x_j. Write x_i = 1 - y_i, where y_i ∈ {0,1}. Then:
∑ a_i (1 - y_i) = ∑ a_i - ∑ a_i y_i.
And -∑{i<j} b{ij} (1 - y_i)(1 - y_j) = -∑ b_{ij} (1 - y_i - y_j + y_i y_j) = -∑ b_{ij} + ∑ b_{ij} (y_i + y_j) - ∑ b_{ij} y_i y_j.
But careful: The sum over i<j, for each term b_{ij}, the contributions: y_i appears in terms where i<j, and also in terms where j<i? But since we sum only i<j, each unordered pair appears once. In the expression ∑{i<j} b{ij} (y_i + y_j), each y_i appears in all pairs with j>i as coefficient b_{ij} and also in pairs with j<i as part of term b_{ji}? But we don't have b_{ji} because we sum i<j. So in ∑{i<j} b{ij} (y_i + y_j), for a given vertex i, it appears in pairs (i,j) with j>i as y_i with coefficient b_{ij}, and in pairs (j,i) with j<i as y_i appears as y_j in the term (y_j + y_i)? Actually for pair (j,i) with j<i, the sum is over i<j, so (j,i) is not included; we only include ordered pairs with first index smaller. So to account properly, we should treat the sum as over unordered pairs. So define b_{ij} symmetric. Then the term -∑{i<j} b{ij} x_i x_j becomes -∑{i<j} b{ij} (1 - y_i)(1 - y_j). Expand: = -∑{i<j} b{ij} + ∑{i<j} b{ij} (y_i + y_j) - ∑{i<j} b{ij} y_i y_j.
Now, ∑{i<j} b{ij} (y_i + y_j) = ∑i y_i (∑{j>i} b_{ij} + ∑{j<i} b{ji})? Since for each unordered pair (i,j), the term b_{ij} (y_i + y_j) appears exactly once. So coefficient of y_i is ∑{j≠i} b{ij}. So ∑{i} y_i * (∑{j≠i} b_{ij}).
Thus f = constant (∑ a_i - ∑{i<j} b{ij}) + ∑ y_i * ( - a_i + ∑{j≠i} b{ij} ) - ∑{i<j} b{ij} y_i y_j.
So after complementing, we have f = const + ∑ a'i y_i - ∑{i<j} b_{ij} y_i y_j, with a'i = -a_i + ∑{j≠i} b_{ij} and the quadratic term is still negative (since - b_{ij} y_i y_j, but note we have a negative sign? Actually we got - ∑{i<j} b{ij} y_i y_j, which is also a negative coefficient on the product. So the structure remains the same: linear terms (maybe positive or negative) and negative quadratic coefficients. So complementing didn't change the sign of quadratic term. So it's still maximizing a function of that form.
Another transformation: Consider variables z_i = ±1 indicating lily vs rose. Let’s set z_i = 1 if lily, z_i = -1 if rose. Then x_i = (1+z_i)/2 (since x_i=1 for lily, 0 for rose). Then plug into objective:
Beauty per segment: S = number of lilies = ∑ (1+z_i)/2 = (L + ∑ z_i)/2. Number of roses = L - S = (L - ∑ z_i)/2. Then product = ((L + Σz)/2) * ((L - Σz)/2) = (L^2 - (Σz)^2)/4.
Thus total beauty = 1/4 ∑j (L_j^2 - (∑{i∈j} z_i)^2) = constant - 1/4 ∑j (∑{i∈j} z_i)^2.
So maximizing beauty is equivalent to minimizing ∑j (∑{i∈j} z_i)^2, where z_i ∈ {+1, -1}. That's a more symmetric formulation. Since constant is ∑ L_j^2 /4, independent of assignment.
Now, minimize ∑j (Σ{i∈j} z_i)^2, subject to z_i ∈ {±1}. This is like we want the sum of z_i over each interval to be as close to zero as possible (in terms of squared sum). This is a classic problem: minimize sum of squares of interval sums given ±1 assignments. This looks like a quadratic programming problem that could be tackled via eigenvalue methods? But with n up to 1000, maybe we can solve with semi-definite programming? However, that seems overkill for a contest problem.
But maybe there is a simpler solution: Because the intervals are arbitrary but m also up to 1000, we can consider a greedy or local optimization? Possibly the answer is to assign based on sorting by some "force" derived from intervals, like treat each position as having a bias to be +1 or -1 based on sum of lengths of intervals that include it? Let's analyze the minimization of ∑ (∑_i z_i)^2.
Expand: ∑j (∑{i∈j} z_i)^2 = ∑j ∑{i,k∈j} z_i z_k = ∑{i,k} z_i z_k * w{ik}, where w_{ik} is number of intervals covering both i and k (including i=k? Actually when i=k, w_{ii} = number of intervals covering i, because interval j includes i and i (trivially) so yes, w_{ii} = c_i = number of intervals containing i). So = ∑i w{ii} z_i^2 + 2 ∑{i<k} w{ik} z_i z_k. Since z_i^2 = 1, we get = ∑i c_i + 2 ∑{i<k} w_{ik} z_i z_k. So minimizing ∑ (∑ z)^2 is equivalent to minimizing ∑{i<k} w{ik} z_i z_k (since constant part fixed). So we want to minimize ∑{i<k} w{ik} z_i z_k, or equivalently maximize -∑{i<k} w{ik} z_i z_k. Since z_i z_k = +1 if same sign, -1 if opposite. So the term w_{ik} z_i z_k is positive if same, negative if opposite. So we want to minimize sum over pairs of w_{ik} * (agreement). So we want to assign signs to make as many large w_{ik} pairs have opposite signs (to make product negative, decreasing sum). That's essentially a Max-Cut problem on a complete graph with edge weights w_{ik}. Because maximizing ∑{i<k} w{ik} * ( -z_i z_k )? Let's derive:
We want to minimize H = ∑{i<k} w{ik} z_i z_k. This is a standard formulation for finding ground state of an Ising model with couplings w_{ik} (positive). Minimizing H is NP-hard in general (equivalent to max-cut? Actually, note: Since z_i z_k = 1 if same, -1 if opposite. Let’s define variable s_i = z_i. Then H = ∑{i<k} w{ik} s_i s_k. The max-cut problem on a graph with edge weights w_{ik} is: maximize ∑{i<k} w{ik} (1 - s_i s_k)/2 = (1/2)(∑ w_{ik} - ∑ w_{ik} s_i s_k) = constant - (1/2) ∑ w_{ik} s_i s_k. So minimizing ∑ w_{ik} s_i s_k is equivalent to maximizing cut weight (since constant). So indeed, minimizing H is equivalent to finding a maximum cut in the graph with edge weights w_{ik}. So our problem reduces to a Max-Cut problem on a complete (or general) graph with vertices = positions, edge weights w_{ik} = number of intervals that include both i and k.
Now, maximum cut is NP-hard in general, but here the graph is defined by intervals, and n,m ≤ 1000. Could there be a special structure that makes max cut polynomial? Possibly the graph is a "interval graph"? Actually, w_{ik} depends on how many intervals cover both i and k. This is like the co-occurrence matrix of intervals. This might have a property that it is a "line graph" or something. But maybe the max cut can be solved in polynomial time on such graphs? Let's think.
Given intervals on a line, consider the matrix w_{ik}. For a fixed i, w_{ik} as a function of k is something like the number of intervals covering both i and k. For each interval [l,r], it contributes 1 to w_{ik} for all i,k in [l,r]. So the matrix w is the sum over intervals of the indicator of both endpoints in the interval. That is like the matrix M where M_{i,k} = number of intervals covering both. This matrix is actually a "two-dimensional" thing. Could it be that w_{ik} is a "totally positive" matrix? Or maybe the graph is perfect? Not sure.
But maybe there is a simple assignment that always works: assign positions in order based on some threshold? For instance, let’s denote for each position i a "pressure" p_i = sum_{j: i∈j} (L_j) - c_i? Not clear.
Another thought: Since the intervals are arbitrary but m,n up to 1000, we could try to solve the max cut via some method like branch and bound? But n=1000, complete graph with ~500k edges, max cut is NP-hard, but maybe with special structure and relatively small n=1000, we could use an approximation? But the problem asks for exact optimal solution (any). In Codeforces, constraints 10^3 often allow O(n^3) or O(n^2 * something) DP if the structure is special. Could it be that the optimal assignment is to sort positions by some cumulative sum and assign 0 or 1 based on sign? That would be a cut based on ordering, i.e., partition the line into a prefix and suffix? Actually, a cut on a line graph (where vertices are arranged on a line) with arbitrary weights could be solved by DP if the graph is a "path"? But our graph is complete, but edge weights have a special structure: w_{ik} can be expressed as sum over intervals of 1_{i∈[l,r]} * 1_{k∈[l,r]}. So w_{ik} = ∑j I(i∈[l_j,r_j] and k∈[l_j,r_j]). This is like the dot product of two vectors: for each interval j, define characteristic vector v_j of length n; then w{ik} = ∑j v_j(i) v_j(k). So w = ∑j v_j v_j^T. So w is positive semidefinite? Indeed, each v_j v_j^T is PSD, so w is PSD. The matrix w is a sum of rank-1 PSD matrices, so it is PSD. So the graph with edge weights w{ik} is actually defined by a set of vectors in ℝ^m (or each vertex i corresponds to a vector u_i ∈ ℝ^m, where u_i(j) = 1 if i in interval j else 0). Then w{ik} = u_i · u_k (dot product). So our graph is the Gram matrix of vectors u_i. Max cut on a graph with edge weights being dot products is known to be equivalent to optimizing a quadratic form, and it can be solved via a semidefinite relaxation (Goemans-Williamson) which gives a 0.878 approximation. But we need exact optimum.
But maybe because vectors u_i are binary and intervals, there is a simple solution: Sort the positions by the cumulative sum of something? Let's explore the geometry: u_i is a vector in {0,1}^m, where each coordinate corresponds to an interval. For interval j, u_i(j) = 1 if i ∈ [l_j, r_j]. Since intervals are on a line, these vectors have a special structure: For a fixed ordering of positions, the set of intervals containing i is monotone? Actually, as i moves along the line, the set of intervals that contain i changes: intervals start and end. So u_i are like "interval membership" vectors. The dot product between u_i and u_k is the number of intervals that cover both. This is reminiscent of the "intersection kernel". For points on a line, the matrix w_{ik} might be "totally positive" in some order? Possibly the Gram matrix for such vectors is "almost" a "one-dimensional" representation, but it's not obvious.
Could the max cut be solved by a simple threshold cut: assign all positions i ≤ t as lily, others rose? That would be a cut that partitions the line into two contiguous blocks. Is that always optimal? Let's test with given examples. In first example, optimal assignment (from output) is 0,1,1,0,0. That corresponds to lilies at positions 2,3; roses at 1,4,5. So it's not a contiguous block of lilies (they are positions 2-3, which is contiguous but not whole prefix or suffix). But could we partition by a threshold? No, because it's two separate lily clusters? Actually it's one cluster: lilies at positions 2,3 only. That's a contiguous block indeed: it's a block of length 2. So it's a contiguous block of lilies, not necessarily prefix. But could be any contiguous block. In second example, output 110010 means lilies at positions 1,2,5? That's 1,2 and 5. That's not a single contiguous block; it's two blocks: 1-2 and 5 alone. So it's not a single contiguous block. So not that.
Maybe optimal assignment corresponds to sorting positions by some "score" and assign top half as lilies? In second example, positions: 1,2,5 are lilies. The scores? Might be something like cumulative sum of something.
Let's compute w_{ik} for second example to see max cut. n=6, intervals: [5,6], [1,4], [4,6]. Compute w matrix:
- Interval1 covers 5,6.
- Interval2 covers 1,2,3,4.
- Interval3 covers 4,5,6.
Then w_{ik} for each pair:
(1,2): both in interval2 ->1; (1,3): both in interval2 ->1; (1,4): both in interval2 ->1; (1,5): none? 1 not in interval1, 1 not in interval3? Only interval2 contains 1 but not 5; interval3 contains 5 but not 1; so 0. (1,6): similarly 0.
(2,3): both in interval2 ->1; (2,4): both in interval2 ->1; (2,5):0; (2,6):0.
(3,4): both in interval2 ->1; (3,5):0; (3,6):0.
(4,5): both? interval3 contains both 4 and 5? interval3 is [4,6] -> yes contains both 4 and5 ->1; interval2 contains 4 but not 5 ->0; interval1 contains 5 but not 4->0; so total 1. (4,6): both in interval3 ->1; also interval? interval2 has 4 but not6; interval1 has6 but not4; so 1. (5,6): both in interval1 and interval3? interval1 covers 5,6 ->1; interval3 covers 5,6 ->1; so w_{5,6}=2.
Also diagonal c_i: number intervals covering i: position1: only interval2 ->1; pos2:1; pos3:1; pos4: intervals2 and3 ->2; pos5: intervals1 and3 ->2; pos6: intervals1 and3 ->2.
Now, minimizing H = ∑{i<k} w{ik} z_i z_k, with z_i ∈ {±1}. Let's compute H for assignment from output: lilies (1) correspond to z=+1? Wait earlier we defined z_i = +1 for lily, -1 for rose. Output 110010: positions:1:1(lily)=>z=+1;2:1=>+1;3:0=>-1;4:0=>-1;5:1=>+1;6:0=>-1. Compute ∑ w_{ik} z_i z_k. Let's compute edge contributions:
List edges with weight:
(1,2): w=1, z1z2 = (+1)(+1)=1 => contribution 1.
(1,3): w=1, z1z3 = +1 * -1 = -1 => -1.
(1,4): w=1, +1 * -1 = -1 => -1.
(1,5): w=0 ->0.
(1,6):0 ->0.
(2,3):1, (+1)(-1)=-1 => -1.
(2,4):1, (+1)(-1)=-1 => -1.
(2,5):0 ->0.
(2,6):0 ->0.
(3,4):1, (-1)(-1)=1 =>1.
(3,5):0 ->0.
(3,6):0 ->0.
(4,5):1, (-1)(+1)=-1 => -1.
(4,6):1, (-1)(-1)=1 =>1.
(5,6):2, (+1)*(-1)=-1 => -2.
Sum contributions: (1) + (-1) + (-1) + (-1) + (-1) + (1) + (-1) + (1) + (-2) = let's sum carefully:
List: 1, -1, -1, -1, -1, 1, -1, 1, -2.
Group: (1-1)=0; 0-1=-1; -1-1=-2; -2+1=-1; -1-1=-2; -2+1=-1; -1-2=-3. So total H = -3.
Constant term ∑ c_i = 1+1+1+2+2+2 = 9. So ∑ (Σ z)^2 = constant + 2 H? Actually earlier we had ∑ (Σ z)^2 = ∑ c_i + 2∑{i<k} w{ik} z_i z_k = 9 + 2*(-3) = 9 -6 = 3. Then total beauty = 1/4 (∑ L_j^2 - 3). Compute L: intervals lengths: [5,6] length=2, [1,4] length=4, [4,6] length=3. L^2: 4,16,9 sum=29. Beauty = (29 - 3)/4 = 26/4 = 6.5? That's not integer? But earlier example said total beauty = 7. Wait inconsistency: I got 6.5 but should be 7. Let's check: Maybe my z assignment? Actually product ab should be integer. Let's compute directly from assignment: Output 110010: pos1 lily, pos2 lily, pos3 rose, pos4 rose, pos5 lily, pos6 rose. Then segments:
[5,6]: positions 5(lily),6(rose): lilies=1, roses=1 => beauty=1.
[1,4]: positions1(lily),2(lily),3(rose),4(rose): lilies=2, roses=2 => beauty=4.
[4,6]: positions4(rose),5(lily),6(rose): lilies=1, roses=2 => beauty=2.
Total = 7. So beauty=7. In my calculation, beauty = 1/4 (∑ L_j^2 - ∑ (∑ z)^2). ∑ L_j^2 = 4+16+9=29. So ∑ (∑ z)^2 should be 1, because 29 - 47 = 29-28=1. Indeed, ∑ (∑ z)^2 should equal 1, not 3. So I must have miscalculated H or constant. Let's recalc ∑ (∑ z)j^2 for each segment:
Segment1 [5,6]: z5=+1, z6=-1, sum = 0, square 0.
Segment2 [1,4]: z1=+1, z2=+1, z3=-1, z4=-1, sum = 0, square 0.
Segment3 [4,6]: z4=-1, z5=+1, z6=-1, sum = -1, square 1.
Total sum of squares = 1. Good. So my earlier calculation gave 3, error because I used formula ∑ (∑ z)^2 = ∑ c_i + 2∑{i<k} w_{ik} z_i z_k. Let's verify that formula: For each segment j, (∑{i∈j} z_i)^2 = ∑{i∈j} z_i^2 + 2∑{i<k∈j} z_i z_k = ∑{i∈j} 1 + 2∑{i<k∈j} z_i z_k = L_j + 2∑{i<k∈j} z_i z_k. Sum over j: ∑j L_j + 2∑j ∑{i<k∈j} z_i z_k = ∑ L_j + 2∑{i<k} (∑{j: i,k∈j} z_i z_k) = ∑ L_j + 2∑{i<k} w_{ik} z_i z_k. But wait, the constant term is ∑ L_j, not ∑ c_i. c_i = number of intervals covering i, so ∑ L_j = ∑i c_i because each interval j contributes L_j = number of positions in it, and summing over j gives total coverage count = ∑i c_i. Yes, indeed ∑j L_j = ∑i c_i. So constant = ∑i c_i. In our example, ∑ L_j = 2+4+3 = 9, which matches ∑ c_i = 1+1+1+2+2+2 = 9. Good. So formula is ∑ (∑ z)^2 = ∑ L_j + 2∑{i<k} w{ik} z_i z_k. So it is not ∑ c_i? It's same. So my H earlier was ∑{i<k} w{ik} z_i z_k, which I computed as -3. Then ∑ L_j + 2H = 9 + 2*(-3) = 9 -6 = 3. But actual sum of squares is 1. So my computed H must be wrong; maybe I miscomputed some w{ik} or some product. Let's recompute H = ∑{i<k} w{ik} z_i z_k for the given assignment.
We need to list all pairs (i<k) and compute w and product.
Positions: 1: +1, 2: +1, 3: -1, 4: -1, 5: +1, 6: -1.
Compute w matrix carefully:
We have intervals:
j1: [5,6]
j2: [1,4]
j3: [4,6]
For each pair (i,k), w = number of intervals covering both.
- (1,2): both in j2? Yes, j2 covers 1-4, so both included => w=1.
- (1,3): both in j2 => w=1.
- (1,4): both in j2 => w=1.
- (1,5): intervals covering both? j1 covers 5 but not 1; j2 covers 1 but not 5; j3 covers 5 but not 1? j3 covers 4-6, includes 5 but not 1. So none => w=0.
- (1,6): similar: none => w=0.
- (2,3): both in j2 => w=1.
- (2,4): both in j2 => w=1.
- (2,5): none? j2 covers 2 but not5; j3 covers 5 but not2; j1 covers5 but not2 =>0.
- (2,6): none =>0.
- (3,4): both in j2 => w=1.
- (3,5): none? j2 covers3 but not5; j3 covers5 but not3; =>0.
- (3,6): none? j2 covers3 but not6; j3 covers6 but not3? Actually j3 covers 4-6, so includes 6 but not 3 =>0; =>0.
- (4,5): both in j3? j3 covers 4,5 =>1. Also j2 covers 4 but not5 => so total 1.
- (4,6): both in j3? j3 covers 4,6 =>1. Also j2 covers4 but not6 =>0; j1 covers6 but not4 =>0 =>1.
- (5,6): both in j1 and j3: j1 covers5,6 =>1; j3 covers5,6 =>1 => w=2.
Now compute z_i*z_k for each:
(1,2): +1 * +1 = +1 => contribution = 1*1 = 1
(1,3): +1 * -1 = -1 => 1 * (-1) = -1
(1,4): +1 * -1 = -1 => 1 * (-1) = -1
(1,5): 0 * (?) = 0
(1,6): 0
(2,3): +1 * -1 = -1 => 1 * (-1) = -1
(2,4): +1 * -1 = -1 => -1
(2,5): 0
(2,6): 0
(3,4): -1 * -1 = +1 => 1 * 1 = 1
(3,5): 0
(3,6): 0
(4,5): -1 * +1 = -1 => 1 * (-1) = -1
(4,6): -1 * -1 = +1 => 1 * 1 = 1
(5,6): +1 * -1 = -1 => 2 * (-1) = -2
Now sum contributions: list: 1, -1, -1, -1, -1, 1, -1, 1, -2.
Let's sum stepwise:
Start 0.
+1 =>1
-1 =>0
-1 => -1
-1 => -2
-1 => -3
+1 => -2
-1 => -3
+1 => -2
-2 => -4
Total = -4? Wait I had -3 earlier but now I got -4. Let's recount the list: We have edges (1,2), (1,3), (1,4), (2,3), (2,4), (3,4), (4,5), (4,6), (5,6). That's 9 edges. I have values: (1,2):1, (1,3):-1, (1,4):-1, (2,3):-1, (2,4):-1, (3,4):1, (4,5):-1, (4,6):1, (5,6):-2. That's 9 numbers. Sum: 1 -1 =0; 0-1=-1; -1-1=-2; -2+1=-1; -1-1=-2; -2+1=-1; -1-2=-3. Actually recalc with order:
1
-1 =>0
-1 => -1
-1 => -2
-1 => -3? Wait careful: After (1,2):1.
Add (1,3): 1 + (-1) = 0.
Add (1,4): 0 + (-1) = -1.
Add (2,3): -1 + (-1) = -2.
Add (2,4): -2 + (-1) = -3.
Add (3,4): -3 + 1 = -2.
Add (4,5): -2 + (-1) = -3.
Add (4,6): -3 + 1 = -2.
Add (5,6): -2 + (-2) = -4.
Now I got -4. Let's recompute each term precisely: maybe I misassigned some w values. Check edge (4,5): w=1? Yes. z4=-1, z5=+1 => product -1, so contribution -1. Edge (4,6): w=1, z4=-1, z6=-1 => product +1, contribution +1. Edge (5,6): w=2, product -1 => -2. So sum includes -1, +1, -2 => net -2 from these three. Previous sum up to (3,4) was -2? Actually after (3,4) we had -2? Let's compute sequentially:
Initialize sum = 0.
(1,2): +1 -> sum=1.
(1,3): -1 -> sum=0.
(1,4): -1 -> sum=-1.
(2,3): -1 -> sum=-2.
(2,4): -1 -> sum=-3.
(3,4): +1 -> sum=-2.
(4,5): -1 -> sum=-3.
(4,6): +1 -> sum=-2.
(5,6): -2 -> sum=-4.
Yes sum = -4. Then ∑ L_j + 2H = 9 + 2*(-4) = 9 -8 = 1. That matches the correct sum of squares! So H = -4, not -3 earlier. So my earlier miscalculation was in the list: I missed one edge maybe? I had only 9 edges but earlier I counted 9 and got -3; now I got -4. The discrepancy: earlier I omitted (2,3) maybe? Actually earlier list I wrote: (1,2):1, (1,3):-1, (1,4):-1, (2,3):-1, (2,4):-1, (3,4):1, (4,5):-1, (4,6):1, (5,6):-2. That's same. Summation mistake earlier. So H = -4. Good.
Thus objective: minimize H = ∑ w_{ik} z_i z_k.
Now maximum cut: Max cut value = (∑ w_{ik} - ∑ w_{ik} z_i z_k)/2 = (W_total - H)/2, where W_total = ∑{i<k} w{ik}. So minimizing H is same as maximizing cut. So we need to find a cut that maximizes total weight of edges crossing. So it's a max cut problem on this specific graph.
Now, since w_{ik} is defined as number of intervals covering both i and k, the graph is not arbitrary: it's the "intersection graph" of intervals? Actually each vertex i corresponds to a point on line, and w_{ik} is the number of intervals that cover the segment from i to k (i.e., that contain both points). This is a "compatibility" measure. For points on a line, the matrix w_{ik} is "totally positive"? Possibly it is a "symmetric ultrametric"? Not exactly.
But maybe max cut on such a graph can be solved by dynamic programming because the graph is "co-interval"? Let's think of the complement: If we consider the graph's adjacency (or weights), it might have a structure that allows a simple solution: perhaps the optimal cut corresponds to a threshold on some ordering. In many one-dimensional problems, the max cut on a graph where edge weights are a function of distance (like decreasing) can be solved via DP, but here edge weight w_{ik} is the number of intervals covering both i and k, which is essentially the number of intervals that cover the whole range from i to k. For fixed i and k, w_{ik} is the number of intervals that start ≤ min(i,k) and end ≥ max(i,k). So if we sort positions, then w_{ik} is a function of left index l = min(i,k) and right index r = max(i,k): w(l, r) = count of intervals with L ≤ l and R ≥ r. That's a "rectangular" function: it's the number of intervals that cover [l,r]. This is like a 2D range count: each interval is a point (l_j, r_j) in a 2D grid where l ≤ r. Then w_{ik} = number of points (l_j, r_j) such that l_j ≤ min(i,k) and r_j ≥ max(i,k). So it's the count in a quadrant: for given i,k, with l = min, r = max, we count intervals with l_j ≤ l and r_j ≥ r. So the weight matrix is defined by a set of points in the plane. This structure might allow a simple optimal cut: maybe the optimal cut corresponds to ordering the points along the line and assigning them based on whether they lie to the left or right of a pivot? But the cut can be any partition of vertices (positions). That's like partitioning the line points into two sets, not necessarily contiguous. However, note that the graph is "perfect" maybe? But max cut on interval graphs is NP-hard? I'm not sure.
Wait: The vertices are points on a line (positions 1..n). The edge weight between i and k depends only on the number of intervals covering both, which is a function of the min and max. This graph is known as a "co-interval graph"? Actually, consider the complement: define an edge weight w_{ik}. If we think of intervals as "cliques" in some sense? Alternatively, note that w_{ik} can be expressed as sum over intervals of 1_{i∈I} * 1_{k∈I}. So the matrix is a sum of rank-1 matrices, each corresponding to an interval. This suggests that the graph might be a "line graph" of something? Hmm.
Perhaps we can solve the max cut exactly using a simple algorithm because the problem size is moderate (n,m ≤ 1000). Could we use brute force? n up to 1000, so 2^1000 impossible. But maybe the graph is bipartite? Not necessarily.
We could try to formulate as a quadratic programming and use some heuristic? But problem statement expects a single correct output (any optimal). It's likely there is a greedy or constructive solution.
Let's re-express the objective in original variables x_i (0/1). We have f(S) = ∑{i∈S} A_i - 2∑{i<k, i,k∈S} w_{ik}. This is a quadratic pseudo-boolean function that is submodular (since coefficient on x_i x_k is negative). Submodular maximization with binary variables is NP-hard in general, but sometimes if the function is also something like "concave in cardinality"? Could be solvable by a min cut if we add a constant and minimize? Actually, submodular minimization is polynomial; maximization is NP-hard. But here the function is of a special form: it's like a weighted sum minus double sum of pairwise weights within the set. This is similar to the "maximum weight independent set" in a graph? Not exactly: If we let complement, maybe we get a cut? Let's complement to get f'(T) where T is set of roses (0). Define y_i = 1 - x_i = 1 if rose. Then x_i = 0 for roses, 1 for lilies. Then we can write objective in terms of both sets. But maybe we can treat both types symmetrically. Since the objective is symmetric if we swap roses and lilies? Not exactly because linear term A_i depends on intervals, but swapping would change A_i to something else? Let's check: If we swap all flowers (0<->1), then S (lilies count) becomes L - S, and beauty = (L - S)*S same, so symmetric. In terms of f(S), swapping corresponds to complement set S' = positions not in S. Then f(S') = ∑{i∉S} A_i - 2∑{i<k, i,k∉S} w_{ik}. Does this equal f(S)? Possibly not, but total beauty should be same because product symmetric. Let's verify: If we flip all bits, then each segment's a and b swap, product same. So the objective value is symmetric under global complement. So f(S) = f(complement of S). That imposes constraints on A_i and w_{ik}. Let's derive: f(S) = ∑{i∈S} A_i - 2∑{i<k∈S} w_{ik}. f(S^c) = ∑{i∉S} A_i - 2∑{i<k∉S} w_{ik}. Since total beauty is same, f(S) = f(S^c) for all S? Not necessarily for all S, but the maximum value will be achieved by both S and its complement if one is optimal, but the function itself may not be symmetric for all S. However, due to the symmetry of the original problem (swap roses/lilies), the objective function should be symmetric under complement if we also swap the definition? Wait original beauty depends on product of count0 and count1. If we swap 0 and 1, count0 becomes count1 and vice versa, product unchanged. So for any assignment (x_i), if we define y_i = 1 - x_i, then the total beauty is the same. So f(S) where S is set of lilies gives same value as f(complement). So indeed f(S) = f(complement) for every S. Because the beauty depends only on the partition of each segment into two groups. So the function is complement-symmetric. Let's check if our derived f(S) satisfies that. f(S) = ∑{i∈S} A_i - 2∑{i<k∈S} w_{ik}. f(S^c) = ∑{i∉S} A_i - 2∑{i<k∉S} w_{ik}. For them to be equal for all S, we need ∑i A_i = constant? Let's compute difference: f(S) - f(S^c) = (∑{i∈S} A_i - ∑{i∉S} A_i) - 2(∑{i<k∈S} w_{ik} - ∑{i<k∉S} w{ik}). This should be 0 for all S. That implies some constraints on A_i and w_{ik}. Possibly A_i = ∑{ik} (from earlier) which indeed satisfies complement symmetry? Let's test: Since A_i = ∑{ik}, we have ∑{i∈S} ∑{ik} = ∑{ik} + 2∑{ik} + ∑{k≠i} w_{ik}, each unordered pair (i,k) both in S is counted twice (once from i and once from k), and each pair with i in S, k not in S is counted once (from i). So ∑{i<k∈S} w_{ik} + ∑{ik}. Similarly, ∑{i<k∉S} w_{ik} + ∑{ik}. Therefore, f(S) = ∑{i<k∈S} w_{ik} = (2∑{ik} + ∑{ik}) - 2∑{ik} = ∑{ik}. So f(S) simplifies to the total weight of edges crossing between S and its complement! That's a key simplification. Let's verify:
We have f(S) = ∑{i∈S} A_i - 2∑{i<k∈S} w_{ik}. But we just expressed ∑{i∈S} A_i = ∑{i∈S} ∑{k≠i} w{ik} = ∑{i∈S, k∉S} w{ik} + 2∑{i<k∈S} w{ik}. Therefore f(S) = (∑{i∈S, k∉S} w{ik} + 2∑{i<k∈S} w{ik}) - 2∑{i<k∈S} w{ik} = ∑{i∈S, k∉S} w{ik}. Indeed! So the objective reduces to the total weight of edges crossing the cut (S, complement). Because the original beauty sum is equal to that crossing weight? Let's check: Earlier we had total beauty = constant/4 + something? But we derived that total beauty = (∑ L_j^2 - ∑ (∑ z)^2)/4. And then we expressed that ∑ (∑ z)^2 = ∑ L_j + 2∑{i<k} w{ik} z_i z_k. Also, we had f(S) as the beauty? Wait careful: Our f(S) earlier was defined as ∑{i∈S} A_i - 2∑{i<k∈S} w_{ik}. That came from the original objective in terms of x_i (0/1). Let's confirm: We had total beauty = ∑j (L_j S_j - S_j^2) with S_j = ∑{i∈j} x_i. Then we derived that total beauty = ∑{i<k} w{ik} (x_i + x_k - 2 x_i x_k). Then we rewrote as f(S) = ∑{i∈S} A_i - 2∑{i<k∈S} w_{ik}? Actually we set x_i = 1 if i ∈ S (lily). Let's recalc from that expression:
Total beauty = ∑{i<k} w{ik} (x_i + x_k - 2 x_i x_k). Write as: ∑{i<k} w{ik} (x_i + x_k) - 2 ∑{i<k} w{ik} x_i x_k.
Now, ∑{i<k} w{ik} (x_i + x_k) = ∑i x_i ∑{k≠i} w_{ik} = ∑i A_i x_i. So total beauty = ∑i A_i x_i - 2 ∑{i<k} w{ik} x_i x_k. That's exactly f(S). So indeed total beauty = f(S). And we just simplified f(S) = ∑{i∈S, k∉S} w{ik}? Let's check: For binary x, we have:
∑i A_i x_i - 2∑{i<k} w_{ik} x_i x_k. Use the identity: ∑i A_i x_i = ∑i x_i ∑{k≠i} w{ik} = ∑{i,k, i≠k} w{ik} x_i. But we need to be careful: In ∑{i<k} w{ik} x_i x_k, each pair i<k both with x_i=x_k=1 contributes w_{ik}. So we can rewrite:
∑i A_i x_i = ∑{i,k, i≠k} w_{ik} x_i = ∑{i<k} w{ik} (x_i + x_k). Because each unordered pair contributes w_{ik} (x_i + x_k). So then f = ∑{i<k} w{ik} (x_i + x_k) - 2∑{i<k} w{ik} x_i x_k = ∑{i<k} w{ik} (x_i + x_k - 2 x_i x_k). But note that x_i + x_k - 2 x_i x_k = 0 if both 0? Actually if x_i=0, x_k=0, then 0+0-0=0. If x_i=1, x_k=0: 1+0-0=1. If x_i=0, x_k=1: 1. If both 1: 1+1-2=0. So indeed this expression is 1 exactly when x_i != x_k (i.e., one is 1, other 0). So f = ∑{i<k} w{ik} * I(x_i != x_k). That is exactly the total weight of edges crossing the cut (between set of lilies and roses). Because if we define S = {i: x_i=1} (lilies) and complement = roses, then w_{ik} contributes if i and k are in different sets. So f(S) = ∑{i∈S, k∉S} w{ik}. This is a much simpler expression! Great.
Thus the total beauty equals the sum over all unordered pairs (i,k) of w_{ik} times indicator that the two flowers are of different types. So we want to maximize the sum of w_{ik} over pairs that are assigned different types.
So the problem reduces to: Given a complete graph on n vertices (positions) with edge weights w_{ik} (number of intervals covering both i and k), partition the vertices into two sets (roses and lilies) to maximize total weight of edges crossing the partition. That's exactly the Max-Cut problem.
But we have a crucial simplification: w_{ik} is defined as number of intervals that include both i and k. That's a specific structure, but still max-cut might be NP-hard in general. However, maybe this specific weight matrix has a property that the max cut is trivial: you can always achieve the sum of all weights? That would be if the graph is bipartite? But it's complete with positive weights, so max cut can't be sum of all weights because some edges must be inside each partition. The maximum cut value is at most half total weight (if weights can be balanced). Actually, for a complete graph with uniform weights, max cut is roughly half total weight. But maybe with this structure, the optimal cut is obtained by ordering the vertices and taking a threshold (i.e., cut based on position index). Is that plausible? Let's test with our examples.
First example: n=5, intervals: [1,3], [2,4], [2,5]. Compute w_{ik}. Then compute total crossing weight for cut S = {2,3} (lilies) as output. Let's compute crossing weight: pairs (i,k) with i in S and k not in S. S={2,3}, complement={1,4,5}. Compute w for each crossing pair:
(2,1): w? intervals covering both 1 and 2: [1,3] covers both, [2,4] covers 2 but not 1? Actually [2,4] covers 2,4 but 1 not; [2,5] covers 2,5 but 1 not; so w=1.
(2,4): intervals covering both 2 and4: [2,4] covers both =>1; [2,5] covers 2,5 but not4? Actually [2,5] covers 2,4? yes includes 4? [2,5] includes 2,3,4,5 so yes covers both 2 and4 =>1; [1,3] covers 2 but not 4 =>0; so total w=2? Wait check: [1,3] doesn't cover 4. So w_{2,4}=2 (from intervals 2 and 3). Actually interval2=[2,4], interval3=[2,5], both cover 2 and4. So w=2.
(2,5): intervals covering both 2 and5: [2,5] covers both =>1; [2,4] does not cover 5; [1,3] does not cover 5 => w=1.
(3,1): intervals covering both 1 and3: [1,3] covers both =>1; others? [2,4] covers 3 but not1; [2,5] covers 3 but not1 => w=1.
(3,4): intervals covering both 3 and4: [2,4] covers both =>1; [2,5] covers both =>1; [1,3] covers 3 but not4 => w=2.
(3,5): intervals covering both 3 and5: [2,5] covers both =>1; others? [2,4] does not cover5; [1,3] does not cover5 => w=1.
Sum crossing = 1+2+1+1+2+1 = 8. That matches total beauty 8. Good.
Now, is there a cut that yields higher crossing? Let's compute total weight sum W_total = ∑{i<k} w{ik}. Compute all w:
List all pairs:
(1,2):1
(1,3):1
(1,4): intervals covering both? [1,3] covers 1 but not4; [2,4] covers 4 but not1; [2,5] covers 4? Actually [2,5] includes 4 but not1 =>0. So w=0.
(1,5):0
(2,3): both in all three? [1,3] covers 2,3 =>1; [2,4] covers 2,3 =>1; [2,5] covers 2,3 =>1 => w=3.
(2,4):2
(2,5):1
(3,4):2
(3,5):1
(4,5): intervals covering both 4 and5? [2,5] covers both =>1; [2,4] covers 4 but not5; [1,3] none =>1? Also check: interval [2,5] covers 4 and5; interval [2,4] covers 4 but not5; so w=1.
Sum: 1+1+0+0+3+2+1+2+1+1 = let's add: (1+1)=2; +0+0=2; +3=5; +2=7; +1=8; +2=10; +1=11; +1=12. Total W_total = 12.
Maximum possible cut is at most W_total (if all edges crossing). But we can't have all edges crossing because cut partitions vertices: any partition cuts some edges, but not all; at most floor? Actually maximum cut can be up to total weight if graph is bipartite? But complete graph with positive weights is not bipartite, but cut can include all edges if partition is such that every edge is between sets? In a complete graph, any edge connects vertices either in same set or across. To have every edge be crossing, you would need that for any two vertices, they are in opposite sets, which is impossible because if we have at least two vertices in same set, then the edge between them is internal. With two sets, edges within each set are internal. So maximum cut weight is total weight minus weight of edges inside both sets. The best you can do is to maximize crossing. Upper bound is total weight, but not achievable unless graph has no edges inside? That would require at most one vertex in each set? Actually if one set is empty, then no crossing edges either. So max cut is strictly less than total weight for complete graph with more than 1 vertex in each set? But could be close to total weight if one set is very small? Actually edges internal to small set are few. The max cut in a complete graph with equal weights would be about half total weight for balanced partition? But with non-uniform weights, we can perhaps do better than half by putting vertices with high weights between them in opposite sets.
But maybe for this specific weight matrix, there is a simple optimal cut: assign each position i to 0 or 1 based on the sign of some function like g(i) = ∑{j: i∈j} (something). Could be related to the idea that w{ik} is a sum over intervals of indicator, so crossing weight for a cut S is ∑{i∈S, k∉S} ∑{j} I(i∈I_j and k∈I_j) = ∑_j (number of lilies in I_j) * (number of roses in I_j). That's exactly the original beauty sum. So it's consistent.
Now, note that w_{ik} = number of intervals covering both i and k. So the crossing weight = ∑j ∑{i∈I_j∩S} ∑_{k∈I_j∩S^c} 1 = ∑_j |I_j∩S| * |I_j∩S^c|. So we are back to original.
Now, perhaps the maximum is always achieved by a cut where S is an interval (contiguous block) or union of at most two intervals? In second example, S = {1,2,5} which is not contiguous (two blocks). So not just one block.
Maybe the max cut on this graph can be solved via a reduction to a flow problem because the graph is "co-bipartite"? Let's examine the structure: The edge weight w_{ik} is defined by intervals. Perhaps the graph's complement (edges with zero weight) has structure? But we have complete graph with varying weights.
Another idea: Because n,m ≤ 1000, we could solve the max cut using semi-definite programming with rounding? But contest problems rarely require SDP.
Wait, maybe there is a polynomial solution because the objective is linear in x_i when we treat it as maximizing crossing weight, and the crossing weight is a quadratic function that can be linearized by introducing variables for edges? But that would be an integer quadratic programming.
But maybe the problem can be solved exactly via a greedy algorithm: assign each position one by one to maximize the increase in crossing weight, perhaps with some sort of "majority" rule? Could be analogous to: For each position, the contribution to the total beauty when flipping its type depends on the types of others. It might be solved by a local search that converges to a global optimum because the function is submodular? But submodular maximization with cardinality constraints is NP-hard, but here there is no constraint; we are free to choose any set. However, note that the function f(S) = ∑{i∈S, j∉S} w{ij} is actually a cut function, which is submodular. Maximizing a cut function is NP-hard (max cut). So indeed the problem is NP-hard in general unless the graph has special structure. So it must be that the specific graph defined by intervals has special structure that makes max cut easy. Could it be that the graph is bipartite? No, because w_{ii} not considered, but there can be triangles. However, maybe the weights are such that the graph is "threshold" or "difference of two monotone functions" enabling a simple solution via sorting by some potential. There is a known class of graphs called "co-interval graphs" or "interval graphs" for which max cut is polynomial? Let's recall: Max cut is polynomial for graphs that are planar? Not exactly. But for graphs that are "weakly bipartite"? Not sure.
Let's search memory: There is a Codeforces problem "Exhibition of Flowers" maybe? I recall a problem where you need to assign 0/1 to maximize sum of products over intervals, and the solution was to assign based on the sign of some cumulative sum computed from intervals. I think I've seen something like: let d_i = number of intervals that start at i minus number that end at i-1? Something like that. Or maybe it's related to the idea that the optimal assignment can be found by solving a system of linear equations, which reduces to a network flow because the objective is convex? Wait, we are maximizing a concave function? Actually the function is quadratic with negative semi-definite matrix? In terms of x (0/1), the quadratic form is -2∑ w_{ik} x_i x_k. The matrix of quadratic coefficients is -2W, where W is the Gram matrix (PSD). So the quadratic part is negative semidefinite? Since W is PSD, -2W is negative semidefinite, so the quadratic function is concave. But the domain is binary, not continuous. Maximizing a concave quadratic over binary variables is NP-hard in general (e.g., max cut). But sometimes if the matrix has a special structure like "M-matrices" etc., it can be solved.
Alternatively, note that the function f(S) = ∑{i∈S, j∉S} w{ij} can be expressed as 1/4 ∑{i,j} w{ij} (z_i - z_j)^2? Actually, if we set z_i = 1 if i∈S, -1 if not, then (z_i - z_j)^2/4 = 1 if z_i ≠ z_j, else 0. So f = 1/4 ∑{i<j} w{ij} (z_i - z_j)^2. And we want to maximize that. But that's equivalent to minimizing -∑ w_{ij} (z_i - z_j)^2? Not helpful.
Maybe we can think of this as: For each interval, we want the product of counts to be high, which encourages balanced numbers. That could be interpreted as we want each interval to have roughly half lilies. This suggests that the solution might be to assign each position a real number in [0,1] that indicates probability, and then round? But again NP-hard.
Given constraints are only up to 1000, maybe the intended solution is to use a max flow/min cut reduction? Because maximizing a cut can be transformed to a minimum s-t cut if we add a source and sink and appropriate edges? Actually, maximum cut is not reducible to min cut in general, but we can reduce a quadratic pseudo-boolean with submodular terms to a graph cut for minimization, not maximization. However, note that our function f(S) = ∑{i∈S, j∉S} w{ij} is actually the cut weight. That's what we want to maximize. That is exactly the Max-Cut problem. But Max-Cut is not reducible to Min-Cut in polynomial time unless P=NP. So if the graph were arbitrary, this would be NP-hard. But here graph is special, maybe it's a "bipartite" or "planar" or something. But is the graph planar? The vertices are points on a line, edges have weights but the graph is complete, so not planar for n>4.
Thus, there must be a clever observation that makes the problem tractable. Perhaps the optimal assignment can be found by sorting positions by their "midpoint" or by solving a system of linear equations because the matrix W has rank at most m? Because w_{ik} = u_i · u_k, where u_i ∈ {0,1}^m, so the matrix W is of rank ≤ m (since it's sum of m rank-1 matrices). Here m ≤ 1000, n ≤ 1000. So the rank is at most m, but that doesn't simplify max cut.
But maybe the max cut on a graph with low rank weight matrix can be solved by enumerating something? Since rank r, we can represent each vertex by a vector in ℝ^r, and the edge weight is dot product. Max cut then is maximize ∑_{i<j} (u_i·u_j) * (1 - z_i z_j)/2? Not sure.
Actually, if we have vectors u_i, and we want to assign signs s_i = ±1 to maximize ∑{i<j} (u_i·u_j) * (1 - s_i s_j)/2 = constant/2 - 1/2 ∑{i<j} (u_i·u_j) s_i s_j. That's equivalent to minimize ∑{i<j} (u_i·u_j) s_i s_j. This is like finding signs to minimize the quadratic form with matrix Gram. This can be thought as: we want to assign signs to minimize the squared norm of ∑ s_i u_i. Because (∑ s_i u_i)^2 = ∑ s_i s_j (u_i·u_j). So minimizing ∑{i<j} (u_i·u_j) s_i s_j is equivalent to minimizing ∑{i,j} s_i s_j (u_i·u_j) = ||∑ s_i u_i||^2. Since diagonal terms also: actually ||∑ s_i u_i||^2 = ∑i ||u_i||^2 + 2∑{i<j} s_i s_j (u_i·u_j). So minimizing ∑{i<j} (u_i·u_j) s_i s_j is equivalent to minimizing the total squared norm, given fixed ∑ ||u_i||^2 constant. So we want to choose signs to make the signed sum vector as small as possible. That is a known problem: find signs to minimize the norm of a sum of vectors. This is equivalent to the "balanced assignment" problem, which is NP-hard in general? Actually, the problem of minimizing the norm of signed sum of given vectors is known as "sign-optimization", which is NP-hard (it's like partition problem). Indeed, given a set of numbers, we can represent them as 1-dimensional vectors; minimizing the absolute value of the sum is the partition problem, which is NP-hard. So indeed the problem is NP-hard in general, even for m=1? Let's test: if m=1, each u_i is a scalar 0 or 1 (since intervals are single interval? Actually if m=1, we have one interval. Then u_i = 1 if i in interval, else 0. Then w_{ik} = u_i u_k. Then minimize ||∑ s_i u_i||^2 = (∑_{i∈interval} s_i)^2. This is minimized by making the sum as close to zero as possible, i.e., partition the set of positions in the interval into two groups with as equal size as possible. That's easy: we can assign arbitrary outside interval; within interval, we can assign half 1, half -1 to get sum close to 0. That's simple. But if we have many intervals, it's a combination. But is it NP-hard even when vectors are 0/1 and have interval structure? Possibly not; because the vectors have a consecutive-ones property: For each interval j, u_i(j) is 1 for i in a contiguous block. So each coordinate j corresponds to a contiguous block of 1's. This is a very structured set: each vector u_i is the characteristic vector of the set of intervals that contain i. Since intervals are on a line, this set has the "interval property": the set of intervals covering i form a set of intervals in the index space? Not exactly. But each u_i is a 0/1 vector of length m where the 1's correspond to intervals that cover i. Because intervals are arbitrary, the pattern of coverage for positions along the line can be arbitrary: as i increases, intervals start and end, so u_i can change arbitrarily? Since intervals are arbitrary subsets of positions that are contiguous. As i moves from 1 to n, each interval contributes a contiguous block of 1's. So each coordinate j has a contiguous block of indices where u_i(j)=1. This is essentially a matrix with each column (coordinate) having a consecutive block of 1's. This matrix is known as a "consecutive-ones matrix" (if rows are positions, columns are intervals, each column has a consecutive block of 1's). That's a well-known property. So the matrix U (n x m) has the consecutive-ones property for columns. This is a special structure. Could it be that the minimization of ||∑ s_i u_i||^2 (or equivalently max cut) becomes tractable for such matrices? Possibly yes, because the problem can be transformed to a network flow on a line.
Let's think: We want to assign signs s_i = ±1 to positions 1..n to minimize F(s) = ||∑ s_i u_i||^2 = ∑{j=1}^m (∑{i∈I_j} s_i)^2, because each coordinate j is just the sum of s_i over i in interval I_j. So F(s) = ∑j (S_j)^2, where S_j = ∑{i∈I_j} s_i. And we want to minimize this sum. Equivalent to minimizing ∑_j (Σ s_i)^2, same as earlier. So it's a quadratic optimization with consecutive-ones constraints? Actually, the intervals are given, so we have a set of m linear combinations, each over a contiguous set of variables, and we want to minimize sum of squares of these linear combinations, with variables ±1. This is reminiscent of the problem of "balancing intervals" which might be solved by dynamic programming using the fact that the intervals are on a line and the quadratic form can be expressed as a sum of local interactions. Perhaps we can set up a recurrence using prefix sums.
Let’s define prefix sums: Let p_i = ∑{t=1}^i s_t, with p_0 = 0. Then for an interval [l, r], the sum S_j = ∑{i=l}^r s_i = p_r - p_{l-1}. So ∑j (p{r_j} - p_{l_j-1})^2. And s_i = p_i - p_{i-1} ∈ {+1, -1}. So we have variables p_0, p_1, ..., p_n, with p_0 = 0, and each difference d_i = p_i - p_{i-1} ∈ {±1}. p_i are integers that change by ±1 each step, so they are like a walk. The objective is a quadratic form in the p's: ∑j (p{r_j} - p_{l_j-1})^2.
This is starting to look like a problem of choosing a path (p_i) to minimize a sum of squared differences between certain pairs. That is similar to a "regression" on a line with constraints that differences are ±1. But p_i can take values from -n to n, but they are integer and with step ±1.
We can try to think of this as: we want to assign +1 or -1 to each s_i to minimize ∑_j (S_j)^2. This is like we want each interval sum to be as close to zero as possible. This is similar to the "discrepancy" problem: given a set system (intervals), color the points ±1 to minimize the maximum discrepancy, but here we minimize sum of squares. However, discrepancy for intervals is easy: you can achieve all interval sums bounded by O(log n) etc., but we need to minimize sum of squares, not supremum.
Maybe we can view it as an energy minimization on a line with pairwise interactions. Expand the squares: ∑j (p{r_j} - p_{l_j-1})^2 = ∑j (p{r_j}^2 + p_{l_j-1}^2 - 2 p_{r_j} p_{l_j-1}). Summing over intervals, we get ∑i a_i p_i^2 - 2 ∑{i<k} b_{ik} p_i p_k, where a_i and b_{ik} come from occurrences of indices as endpoints. Since p_i^2 is not fixed; we have constraints on p_i. This looks like a quadratic form that might be convex in p_i (since it's a sum of squares). If we ignore the discrete constraints, the continuous optimum would set all S_j = 0 if possible, but that might not be achievable with ±1 steps.
Maybe we can solve via DP over positions, considering the current prefix sum p_i, because the intervals that end at i involve p_i, and those that start at i+1 involve p_i as well. Since n ≤ 1000, p_i can range from -n to n (2001 possible values). We could do DP where state is p_i (the prefix sum up to i) and we compute minimal cost contributed by intervals that have ended by i. However, intervals contribute squared terms that involve two endpoints, so we need to account for them when both endpoints are known. If we process left to right, when we reach position r, for each interval [l,r] we would know p_{l-1} from earlier state. But if we store all previous p values in the state, that's too many. However, perhaps we can use the fact that intervals are contiguous and we can treat the cost incrementally.
Let’s define cost C(s) = ∑{j} (p{r_j} - p_{l_j-1})^2. We can rewrite this as ∑{i=0}^{n} ∑{j: l_j-1 = i} something? Not straightforward.
Consider building a graph with nodes 0..n representing prefix indices, and edges representing s_i = p_i - p_{i-1} with weight? Not.
Alternatively, note that this is a quadratic form that can be expressed as a quadratic function of the vector p = (p_0, p_1, ..., p_n). Let's compute the matrix explicitly. The objective is ∑j (e{r_j} - e_{l_j-1})^T p )^2, where e_i is unit vector with 1 at index i. So it's p^T M p, where M = ∑j (e{r_j} - e_{l_j-1})(e_{r_j} - e_{l_j-1})^T. This M is positive semidefinite. The constraints: p_0=0, p_i - p_{i-1} ∈ {±1}. So p is a walk starting at 0 with steps ±1.
Thus we need to find a walk that minimizes p^T M p. This is a quadratic integer programming problem with a tridiagonal structure in constraints, but M is from intervals.
Given n ≤ 1000, we might be able to do DP with state p_i (value of prefix sum) and compute minimum cost up to i, where cost includes contributions from all intervals that have been completed up to i. For each new step, when we go from i to i+1, we choose s_{i+1}=±1, so p_{i+1}=p_i ± 1. The intervals that end at i+1 will add a term (p_{i+1} - p_{l-1})^2. But p_{l-1} is from earlier; if we have stored only p_i, we don't know p_{l-1} unless we remember all previous p values. However, perhaps we can accumulate these contributions in a way that separates. Because the cost is quadratic, we can maybe maintain a linear term that aggregates contributions from intervals that are still open.
Think of processing positions from left to right, maintaining a set of intervals that have started but not yet ended. When we move, the sum over these active intervals of something might be updated. This is similar to online optimization of a quadratic function.
Alternatively, we can use the fact that the cost can be expressed as sum over pairs of positions of w_{ik} (1 - s_i s_k)/2? That's the cut form. But maybe the cut perspective is easier: maximize crossing weight. However, perhaps there is a known result: For a graph where edge weights are given by w_{ik} = ∑{j} I(i∈I_j and k∈I_j), the maximum cut can be found by a simple greedy: assign each vertex i to side 0 or 1 based on the sign of some potential, computed as ∑{j} (something like (r_j - l_j + 1) * ...). But let's test with second example to see if there is a simple rule.
Compute for each position i, maybe compute "bias" B_i = ∑{j: i∈I_j} (something). In the earlier simplified f(S) = ∑{i∈S, k∉S} w_{ik}, we can think of each vertex's contribution to the cut if flipped. If we start with all 0, then flipping a vertex i from 0 to 1 adds contributions: for each neighbor k currently 0, we gain w_{ik} (since edge becomes crossing) but for each neighbor k currently 1, we lose w_{ik} (since edge was crossing, now internal). So the net gain of flipping i given current assignment is: Δ_i = (∑{k: x_k=0} w{ik}) - (∑{k: x_k=1} w{ik}) = (∑{k} w{ik}) - 2∑{k: x_k=1} w{ik} = A_i - 2∑{k∈S} w{ik}. This is like gradient. At optimum, for all i, Δ_i ≤ 0 (since flipping would not increase). That gives conditions: For i in S, Δ_i ≤ 0? Actually for i∈S, flipping from 1 to 0 would have Δ'i = (∑{k∈S} w_{ik}) - (∑{k∉S} w{ik}) = 2∑{k∈S} w{ik} - A_i. For local optimum, we need both not positive? But it's a discrete problem, may have multiple local optima.
Maybe the optimal solution satisfies that the set S corresponds to positions where some "potential" exceeds a threshold. Since the graph is defined from intervals, the potential might be something like the total length of intervals covering i? Let's compute A_i = ∑{j: i∈j} (L_j - 1). For first example:
Positions: 1: intervals covering: [1,3] (L=3) => L-1=2, [2,4]? no, [2,5]? no => A1=2.
2: intervals: [1,3] (2), [2,4] (3-1? L=3 =>2? Wait L-1 for each interval: For interval length L, contribution L-1. For [1,3] L=3 =>2; [2,4] L=3 =>2; [2,5] L=4 =>3? Actually [2,5] length 4 =>3. So A2 = 2+2+3=7.
3: intervals: [1,3] (2), [2,4] (2), [2,5] (3) => total 7.
4: intervals: [2,4] (2), [2,5] (3) =>5.
5: intervals: [2,5] (3) =>3.
Now output S={2,3} (lilies). For i=2, sum over k in S w{2k}? w_{2,3}=3, and no other in S? S includes 2 and 3, so ∑{k∈S} w{2k} = w_{2,3}=3. A2=7, so Δ'2 (flip from 1 to 0) would be 2*3 - 7 = -1 ≤0, so flipping would decrease or not increase. For i=3 similarly. For i=1 not in S, flipping from 0 to 1 would give Δ_1 = A1 - 2∑{k∈S} w_{1k} = 2 - 2*(w_{1,2}+w_{1,3}) = 2 - 2*(1+1)=2-4=-2 ≤0, so flipping would not increase. So conditions satisfied.
Now, is there a simple rule like: put lily if A_i > something? In this case A2=7, A3=7, A4=5, A5=3, A1=2. The chosen lilies are those with highest A_i? Yes, positions 2 and 3 have highest A (7), next 4 has 5, 5 has 3, 1 has 2. So they picked the top two. In second example, compute A_i = ∑_{j: i∈j} (L_j-1). Intervals: [5,6] L=2 => L-1=1; [1,4] L=4 =>3; [4,6] L=3 =>2. So:
Pos1: only interval2 =>3
Pos2: interval2 =>3
Pos3: interval2 =>3
Pos4: interval2 (3) + interval3 (2) =5
Pos5: interval1 (1) + interval3 (2) =3
Pos6: interval1 (1) + interval3 (2) =3
Thus A: 1:3,2:3,3:3,4:5,5:3,6:3. Highest is position 4 with 5. But output lilies are positions 1,2,5. That's not the highest; they chose 1,2,5 (A=3) and not 4 (A=5). So not simply top A.
Maybe it's based on some "pressure" from both sides. Let's compute something else: For each position i, define B_i = ∑_{j: i∈j} (some signed quantity) like number of intervals where i is to the left of the midpoint? Not sure.
Maybe the solution is to treat this as a maximum cut problem on a graph that is a "line graph"? Actually, note that w_{ik} = number of intervals covering both i and k. This can be expressed as: For each interval, it contributes 1 to all pairs within it. So the graph is the union of cliques (each interval defines a clique with weight 1 per edge, but note that if multiple intervals cover the same pair, weight adds). So the graph is a sum of cliques (not necessarily overlapping in a structured way). Maximum cut on a graph that is a sum of cliques might have a simple solution: Because the cut weight contributed by a clique of size L is equal to (number of edges crossing) = |S|*(L - |S|). If we have multiple cliques with weights, the total cut weight is ∑_{j} |S ∩ I_j| * (|I_j| - |S ∩ I_j|). That's exactly our original formulation. So it's like we have a collection of cliques (intervals) with unit weight per edge each, and we want to choose a set S to maximize sum over cliques of |S∩I| * (|I|-|S∩I|). That is a 0-1 quadratic optimization.
Now, could it be that this problem is equivalent to maximizing a quadratic function that is separable? Maybe we can consider each interval independently, but they interact via shared vertices.
Another perspective: The objective is ∑_{j} f_j(|S∩I_j|) where f_j(k) = k*(L_j - k) which is concave in k (since it's a parabola opening downward). So each interval's contribution is a concave function of the number of lilies in that interval. The total is sum of concave functions of linear functions of the binary variables. Maximizing a sum of concave functions over binary variables is generally hard, but sometimes can be solved via linear programming if the concave functions are piecewise linear? Not exactly.
Given that n,m ≤ 1000, maybe we can solve using a min-cut reduction by introducing auxiliary variables? Wait, there is a known technique: Maximizing a submodular function that is also a cut function can be reduced to a minimum s-t cut if the function is representable as a cut function? But here we are maximizing cut weight, which is itself a cut function of the graph. So we want max cut. That's not reducible to min cut. However, sometimes we can transform a max cut problem into a min cut problem by considering complement? Not generally.
But maybe because the graph is "perfect" with no odd cycles? Not.
Let's step back. Could it be that the problem is actually easier: maybe the maximum total beauty is always achieved by simply assigning roses and lilies alternating? But the second example disproves that: alternating would be something like 101010 or 010101, let's compute for 101010: positions 1:1,2:0,3:1,4:0,5:1,6:0. Then beauty: segment [5,6]: 1 and 0 => 11=1; [1,4]: 1,0,1,0 => lilies=2, roses=2 => 4; [4,6]: 0,1,0 => lilies=1, roses=2 => 2; total=7 also. So alternating gives 7 as well. Check: 101010 gives S for [4,6] = positions4:0,5:1,6:0 => lilies=1, roses=2 => product=2; same as output? Output gave 110010: lilies at 1,2,5 => product for [4,6] = 12=2. So both get 7. So alternating is also optimal. In first example, alternating pattern? 01010? That would be positions: 1:0,2:1,3:0,4:1,5:0. Then segments: [1,3]: 0,1,0 => lilies=1, roses=2 => product=2; [2,4]: 1,0,1 => lilies=2, roses=1 => product=2; [2,5]: 1,0,1,0 => lilies=2, roses=2 => product=4; total=8 also. So alternating yields same optimum. But output was 01100, which is not strictly alternating but gives same total 8. So alternating might always achieve maximum? Let's test a simple case: n=2, m=1, interval [1,2]. Then beauty = a*b. To maximize, we want one rose and one lily, product=1. Alternating (01 or 10) gives that. So yes.
Another test: n=3, m=2: intervals [1,2] and [2,3]. We want maximize sum: For [1,2]: product; for [2,3]: product. Let's try assignment 010 (rose,lily,rose): [1,2]:1 rose,1 lily =>1; [2,3]:1 lily,1 rose=>1 total=2. Assignment 001 (rose,rose,lily): [1,2]:2 roses,0 lily =>0; [2,3]:1 rose,1 lily=>1 total=1. So 010 gives 2, same as alternating 010? Actually alternating could be 010 or 101: 101 gives [1,2]:1 lily,1 rose =>1; [2,3]:0 lily,2? Wait 101: positions 1 lily,2 rose,3 lily: [1,2]:1 lily,1 rose =>1; [2,3]:1 rose,1 lily=>1 total=2. So alternating good.
But is there a case where alternating is not optimal? Let's try n=3, m=1 interval [1,3]. Then we want as balanced as possible: length 3, best product is 1*2=2 (since 1 and 2). Achievable by having 2 of one type, 1 of other. Alternating patterns: 010 gives 1 lily,2 roses? Actually 010: rose,lily,rose -> 1 lily,2 roses -> product 2. 101: lily,rose,lily -> 2 lilies,1 rose -> product 2. So works.
Another test: n=4, m=2: intervals [1,3] and [2,4]. We can try to see if alternating (0101) yields maximum. Compute alternating 0101: positions: 1:0,2:1,3:0,4:1. [1,3]: 0,1,0 -> 1 lily,2 roses -> product=2. [2,4]: 1,0,1 -> 2 lilies,1 rose? Actually 1,0,1: lilies=2, roses=1 -> product=2. Total=4. Could there be a better? Try 0011: 0,0,1,1. [1,3]: 0,0,1 -> lilies=1, roses=2 ->2. [2,4]: 0,1,1 -> lilies=2, roses=1 ->2 total=4. Try 0110: 0,1,1,0. [1,3]: 0,1,1 -> lilies=2, roses=1 ->2. [2,4]: 1,1,0 -> lilies=2, roses=1 ->2 total=4. Try 0001: [1,3]:0,0,0 ->0; [2,4]:0,0,1 ->1*2? Actually roses=3, lilies=1? Wait [2,4] length3: 0,0,1 => lilies=1, roses=2 -> product=2? But [1,3] product=0 so total=2. So 4 seems max. So alternating works.
But is there a scenario where alternating is not optimal? Consider n=4, m=1 interval [1,4]. Length 4, optimal product is 2*2=4 (balanced). Alternating 0101 gives 2 lilies,2 roses ->4. So ok.
Consider n=5, m=2 intervals: [1,4] and [2,5]. Lengths 4 each. Want both intervals balanced: each ideally 2 and 2. Can we achieve both? Let's try alternating 01010: 0,1,0,1,0. For [1,4]: 0,1,0,1 => 2 lilies,2 roses ->4. [2,5]: 1,0,1,0 => 2 lilies,2 roses ->4. Total=8. Try different: 00110: 0,0,1,1,0. [1,4]: 0,0,1,1 => lilies=2, roses=2 ->4. [2,5]: 0,1,1,0 => lilies=2, roses=2 ->4. Total=8. So many.
Maybe alternating always gives maximum? But then why the problem? Possibly the answer is indeed to output any alternating pattern? But the examples show non-alternating patterns. However, the problem statement says "If there are multiple answers, print any." So alternating would be acceptable if it's optimal. But is it always optimal? Let's try to find a counterexample.
Consider n=4, m=2 intervals: [1,2] and [3,4]. These are disjoint intervals. Each interval of length 2, optimal product is 1*1=1 each, total=2. Alternating 0101 gives assignments: [1,2]=0,1 -> product=1; [3,4]=0,1 -> product=1 total=2. Works. 0011 gives [1,2]=0,0->0; [3,4]=1,1->0 total=0. So alternating good.
Consider n=4, m=2 intervals: [1,3] and [1,4]. We can test. Lengths: 3 and 4. We want both to be balanced. Is alternating 0101 optimal? Compute: 0101: [1,3]:0,1,0 -> 1 lily,2 roses -> product=2. [1,4]:0,1,0,1 -> 2 lilies,2 roses ->4 total=6. Could we do better? Try 0110: [1,3]:0,1,1 -> 2 lilies,1 rose ->2; [1,4]:0,1,1,0 -> 2 lilies,2 roses? Actually 0,1,1,0 -> lilies=2, roses=2 ->4 total=6. Try 0011: [1,3]:0,0,1 -> 1 lily,2 roses ->2; [1,4]:0,0,1,1 -> 2 lilies,2 roses ->4 total=6. Try 1010: [1,3]:1,0,1 ->2 lilies,1 rose->2; [1,4]:1,0,1,0 ->2 lilies,2 roses->4 total=6. So seems 6 is max. Could there be 7? Possibly if [1,3] product=2 and [1,4] product=6? But max for length4 is 4, so total max 6. So alternating works.
Consider n=5, m=3 intervals: [1,5], [2,4], [3,3]? Actually length1 interval gives product 0 always. So not interesting.
Maybe the problem is that for any set of intervals, the maximum cut value of this graph is exactly half the total weight? But that's not true for the first example: total weight W_total = 12, half is 6, but max cut we found is 8, which is >6. So it's not half. The max cut can be larger than half because the graph is not necessarily regular; edges with high weight can be placed across the cut.
But is alternating always optimal? Let's try a more complex example with overlapping intervals that force some imbalance. We need to test with small n,m manually maybe with brute force to see if alternating ever fails.
We'll write a quick mental brute for small n=3, m=2 with intervals [1,2], [1,3]. Compute all 8 assignments? Actually n=3, 2^3=8.
Define intervals:
I1: [1,2]
I2: [1,3]
Compute beauty: B = (rose1*lily1?) Let's compute for each assignment (x1,x2,x3) where 1=lily, 0=rose.
We'll enumerate:
- 000: S1=0, L1=2 =>0; S2=0 =>0 total0.
- 001: S1=0=>0; S2=1 =>1*(3-1)=1*2=2 total2.
- 010: S1=1=>11=1; S2=1 =>12=2 total3.
- 011: S1=1 (since positions2,3? Actually S1 sum over [1,2]: x1=0? Wait 011 means x1=0, x2=1, x3=1? Actually 011: pos1=0, pos2=1, pos3=1. Then S1 = x1+x2 = 0+1=1 => beauty1 = 11=1; S2 = 0+1+1=2 => beauty2 = 21=2 total3.
- 100: x1=1,x2=0,x3=0: S1=1 =>1; S2=1 =>1*2=2 total3.
- 101: 1,0,1: S1=1+0=1 =>1; S2=1+0+1=2 =>2 total3.
- 110: 1,1,0: S1=2 =>0? Actually S1=2, L1=2 =>20=0; S2=1+1+0=2 =>21=2 total2.
- 111: S1=2 =>0; S2=3 =>0 total0.
Maximum total is 3. Alternating patterns: 010 gives 3, 101 gives 3. So optimal.
Now test n=3, m=2 intervals [1,2], [2,3] we already did max=2, alternating gives 2.
Test n=3, m=3 intervals: [1,2],[2,3],[1,3]. Compute total beauty: For each assignment, sum of products. Brute:
000: all 0.
001: intervals: [1,2]:0; [2,3]: x2=0,x3=1 => 0*? Actually S2=0+1=1 =>11=1? Length2 => product 1; [1,3]: S=1 =>12=2 total=3? Let's compute: Actually [2,3] length2: if S=1, product=1; [1,3] length3: S=1 product=12=2; [1,2] product=0. So total=3.
010: [1,2]: S=1 =>1; [2,3]: S=1 =>1; [1,3]: S=1 =>2 total=4.
011: [1,2]: x1=0,x2=1 =>1 =>1; [2,3]: x2=1,x3=1 =>2 =>0; [1,3]: S=2 =>21=2 total=3.
100: symmetric to 001? 100: [1,2]:1=>1; [2,3]:0=>0; [1,3]:1=>2 total=3.
101: [1,2]:1=>1; [2,3]:0+1? Actually x2=0, x3=1 =>1 =>1; [1,3]:2=>2 total=4? Check: 101: x1=1,x2=0,x3=1. [1,2]: S=1 =>1; [2,3]: S=1 =>1; [1,3]: S=2 =>2 total=4.
110: [1,2]:2=>0; [2,3]:1=>1; [1,3]:2=>2 total=3.
111: all 0.
So max=4. Alternating patterns: 010 gives 4, 101 gives 4. So optimal.
Now try n=4, m=3 intervals: maybe [1,2],[2,3],[3,4]. Compute optimal? Possibly alternating yields max. But let's test a tricky case: intervals that heavily overlap in a way that forces some positions to be same? For instance, n=4, intervals: [1,4], [2,3]. The goal: maximize product for [1,4] (length4) -> want 2 and 2; for [2,3] (length2) -> want 1 and 1. So ideally we want positions 2 and 3 opposite, and overall counts 2 and 2. That suggests assignments like: position 2=1,3=0; then we need total lilies=2. So if 2=1,3=0, then we need one more lily among {1,4}. So possible assignments: 1=1,4=0 gives lilies at {1,2} => total lilies=2, roses {3,4}. Check: [2,3] has 1 and 0 => product=1; [1,4] has 1,1,0,0? Actually positions:1=1,2=1,3=0,4=0 => lilies=2, roses=2 => product=4; total=5. Alternating pattern 0101: positions:1=0,2=1,3=0,4=1 gives [2,3]=1,0 =>1; [1,4]=0,1,0,1 => lilies=2, roses=2 =>4 total=5. So 5 is achievable. Can we get more? Max for [1,4] is 4, for [2,3] is 1, sum max 5. So alternating works.
Now try intervals: [1,3], [2,4], [1,4]. This is like first example plus the full interval. Let's compute n=4, intervals: [1,3], [2,4], [1,4]. Compute optimal? Let's brute conceptually. Alternating 0101: positions: 0,1,0,1. Then:
[1,3]: 0,1,0 => product=2
[2,4]: 1,0,1 => product=2
[1,4]: 0,1,0,1 => product=4 (2*2) total=8.
Can we get more? [1,4] max is 4, [1,3] max is 2 (len3 => 2), [2,4] max is 2, sum max possible =8 if each reaches max simultaneously. So 8 is max. So alternating yields max.
Now try a more complex: n=5, intervals: [1,3], [3,5], [1,5]. Maybe alternating 01010: 0,1,0,1,0. Then:
[1,3]:0,1,0 =>2
[3,5]:0,1,0 =>2? Actually positions3=0,4=1,5=0 => lilies=1, roses=2 => product=2? Wait compute: [3,5] length3: if S=1, product=12=2. Good.
[1,5]: 0,1,0,1,0 => lilies=2, roses=3? Actually lilies at 2,4 =>2, roses at 1,3,5 =>3, product=6? But length5 max product is floor(5^2/4)=6.25 => max integer is 6 (23). So total = 2+2+6=10.
Is it possible to get >10? Max sum of individual maxima: [1,3] max=2, [3,5] max=2, [1,5] max=6, total=10, so that's max if achievable. Alternating achieves it. So works.
Maybe alternating always works because intervals are on a line and we can achieve perfect balance for each interval simultaneously by alternating? Is that always possible? Let's think: For any set of intervals, does there exist a binary assignment such that for every interval, the number of 1's is either floor(L/2) or ceil(L/2)? That's a strong condition. This is like constructing a binary sequence with discrepancy at most 1 for all intervals. Is it always possible? That is the problem of "balanced coloring" for intervals. For intervals, it is known that there exists a 2-coloring (with values +1/-1) such that each interval sum is within the length parity? Actually, it's known that the discrepancy of intervals is 1: you can color a sequence of integers with ±1 so that the sum over any interval is at most 1 in absolute value? I recall the "discrepancy theory" result: For intervals on a line, the discrepancy is 1. Indeed, the interval discrepancy for 2 colors is 1: there exists a coloring such that for any interval, the difference between number of red and blue is at most 1. This is a classic result: the discrepancy of the set of intervals is 1. Actually, I'm not entirely sure: For the family of all intervals in {1..n}, the discrepancy is 1. But here we have a specific subset of intervals (m intervals). The classic result says that for the family of all intervals, there exists a 2-coloring with discrepancy ≤ 1. This is achieved by the sequence +1, -1, +1, -1,... (alternating) gives each interval sum either 0 or ±1 depending on parity. Actually, alternating yields that any interval of even length has sum 0, odd length has sum ±1. So discrepancy 1. So for the complete set of all intervals, alternating works. But we only have a subset m intervals. So if alternating works for all intervals, it certainly works for any subset. Therefore, alternating assignment (0101... or 1010...) yields for each interval j: beauty = floor(L_j^2/4) because product = (ceil(L/2) * floor(L/2)). Because if L even, L/2 each, product = L^2/4; if L odd, (L+1)/2 * (L-1)/2 = (L^2-1)/4. That's the maximum possible for that interval alone. So alternating achieves the maximum possible for each individual interval simultaneously. Thus the total beauty becomes sum_j max_beauty(j). That sum is an upper bound for the total beauty, because each interval's beauty cannot exceed its individual maximum. Therefore, if we can achieve that bound simultaneously, it's optimal. And alternating does achieve it, because for any interval, the number of lilies is either floor(L/2) or ceil(L/2), which gives the maximum product for that interval. So indeed, alternating yields the maximum possible total beauty, since it achieves the maximum for each segment individually.
But is it always true that alternating gives the maximum for each interval? Let's verify: For any interval of length L, the product is maximized when the counts are as balanced as possible: if L even, exactly L/2 each; if L odd, either (L+1)/2 and (L-1)/2. The alternating pattern (starting with either 0 or 1) yields exactly that: For any contiguous block of an alternating sequence, the numbers differ by at most 1. Because in an alternating sequence 010101..., any contiguous substring will have either equal numbers of 0 and 1 if length even, or differ by 1 if length odd, with the majority being the first element if length odd? Let's check: If the alternating sequence starts with 0, then for an interval, the count of 0's is either floor(L/2) or ceil(L/2) depending on start. Actually, if start is 0, then the pattern is 0,1,0,1,... So for an interval, the number of zeros = ceil(L/2) if the first element is 0 and interval length odd? Let's compute: interval length L, number of zeros = floor(L/2) if the interval starts with 1? But whichever, the difference is at most 1. So the product is floor(L^2/4). Indeed, the product for an alternating sequence is always the maximum possible for that interval length. Because the product = ab where a+b=L and |a-b|≤1, which gives ab = floor(L^2/4). So yes, alternating achieves the maximum per interval individually.
Therefore, the sum of individual maxima is achievable by simply alternating. Hence, alternating is always optimal! But wait, is there any catch? The intervals are arbitrary, but the alternating assignment works for every interval because it's a property of any substring of an alternating sequence. Indeed, if we have a perfectly alternating sequence (alternating 0 and 1), any contiguous segment of it will have numbers of each type that differ by at most 1. This holds regardless of which alternating sequence we pick (starting with 0 or starting with 1). So both 0101... and 1010... work.
Thus the answer is trivial: output any alternating string of length n, e.g., "010101..." or "101010..." depending on n. But the examples given are not strictly alternating: first example output "01100", which is not alternating. That means alternating is also correct, but they gave a different example. However, in the second example, they gave "110010", which is also not alternating. So maybe alternating is optimal but not the only optimal. So indeed, the problem likely expects any answer that achieves maximum, and the maximum is sum of floor(L_j^2/4). Since alternating achieves that, we can output alternating. But is it always true that alternating achieves floor(L^2/4) for every interval? Let's test: Take n=5, alternating starting with 0: 01010. Interval [1,3] is 0,1,0 -> lilies=1, roses=2, product=2. For length 3, floor(9/4)=2, correct. Interval [2,5] is 1,0,1,0 -> lilies=2, roses=2, product=4, length 4 floor(16/4)=4 correct. So yes.
Now, what about interval [1,1] length 1: product=0, floor(1/4)=0 correct. So works.
Thus alternating always yields the maximum per interval, so sum of maxima is achieved. That's it.
But wait, is there any scenario where the maximum per interval cannot be simultaneously achieved? For instance, consider intervals that have conflicting requirements? However, we just argued that alternating satisfies all intervals maximally because it makes each interval balanced. So there is no conflict. Could there be a scenario where the maximum per interval is not floor(L^2/4) because of the binary nature? That is the maximum for each interval individually. And alternating achieves that for every interval because it's a global pattern. So it's always achievable.
Thus the problem reduces to: output any alternating pattern. But is there a catch when n=1? Then alternating is just "0" or "1". For a single flower, any interval covering it will have length 1, beauty=0, so both fine.
So answer: print a string of length n where characters alternate between '0' and '1'. Starting with either is fine.
But wait, let's double-check with a more complex set of intervals: Suppose intervals are [1,2] and [2,3] we already did. Alternating works. But is there any scenario where an alternating pattern does NOT achieve the per-interval maximum? Let's test a tricky case: n=3, pattern 010. Interval [1,2]: 0,1 -> product=1 (max for length2 is 1) ok. [2,3]: 1,0 -> product=1 ok. [1,3]: 0,1,0 -> product=2, max for length3 is 2 ok. So fine.
What about pattern 101? similar.
Now consider n=4, alternating 0101. Interval [1,4]: 0,1,0,1 -> product=4 (max for length4 is 4). Good.
But wait, if the alternating pattern is 0101..., then for an interval of even length, the numbers are exactly equal. For odd length, they differ by exactly 1. So product = floor(L^2/4). That's exactly the maximum possible for that interval. So yes.
Thus the maximum total beauty is simply sum over intervals of floor(L_j^2/4). And we can achieve it with any alternating assignment.
Now, is there any nuance that the problem expects us to output a specific answer? No, any answer works. So we can output either "010101..." or "101010..." truncated to length n.
But the examples show non-alternating answers. That's okay because they are also optimal (since they also achieve the same total). Possibly they used some other algorithm.
Thus the solution is extremely simple: generate an alternating string of length n.
But wait: Is it always possible that alternating yields the per-interval maximum for every interval? Yes, because the substring of an alternating sequence is balanced. So indeed.
But what if n is very large? That's fine.
I will now double-check with some random small cases by brute force to be absolutely sure. Let's mentally try n=2, intervals: [1,1] and [2,2] (two length-1 intervals). Their max each 0, total 0. Alternating yields 01 or 10; each length-1 interval product=0. Good.
n=3, intervals: [1,2] and [2,3] and [1,3]. We already computed max total =? We computed max total 4? Actually we computed earlier with all three intervals gave max total 4? Wait earlier we had n=3, m=3 intervals: [1,2],[2,3],[1,3] max total 4? Let's recompute: For each interval, max: [1,2] length2 max=1; [2,3] max=1; [1,3] length3 max=2; sum=4. Does alternating achieve 4? 010 gives [1,2]=1, [2,3]=1, [1,3]=2 sum=4. Yes.
Now consider n=4, intervals: [1,1], [2,2], [3,3], [4,4] (all singletons). Max each 0, alternating any, fine.
Consider n=4, intervals: [1,3] and [2,4]. Both length3? Actually [1,3] length3, max=2; [2,4] length3, max=2; sum=4. Alternating 0101: [1,3]=0,1,0 ->2; [2,4]=1,0,1 ->2; total=4.
All good.
Thus the answer is simply to output an alternating string. But wait, is there any scenario where alternating fails because intervals have overlapping constraints that force a specific parity? For alternating, the pattern is fixed: positions alternate. Could there be an interval that requires, say, exactly 2 lilies and 1 rose, but alternating gives 2 lilies and 1 rose? That's fine. But maybe an interval of length 3 where the maximum product is 2, but to achieve that we need either (2,1) lilies/roses. Both alternating patterns will give either (2,1) depending on start. So it's okay.
Now, consider if there are intervals of even length: they need exactly L/2 of each. Does alternating guarantee that? For an even-length interval in an alternating sequence, the counts are equal only if the interval starts with the opposite type to the pattern? Actually, check: pattern 0101..., consider interval starting at position 1 length4: 0,1,0,1 -> 2 each. Starting at position2 length4: 1,0,1,0 -> 2 each. So any even-length interval in an alternating sequence will have equal numbers. Because the alternating sequence is periodic with period 2, and any contiguous block of even length contains equal numbers of each. So yes.
Thus alternating satisfies all intervals perfectly.
Therefore, the problem is trivial: just output any alternating sequence.
But wait, is there any hidden nuance? The problem statement says "Sonya can put either a rose or a lily in the i-th position." She wants to maximize sum of beauties. As argued, the maximum possible sum is sum of floor(L_i^2/4). And this maximum is achievable by an alternating arrangement. So answer is any alternating string.
However, why did the examples give non-alternating strings? Because they are also correct, but they wanted to show some other assignment. The first example output "01100". Let's check if that is alternating? Starting: 0,1,1,0,0 -> not alternating because positions 2 and 3 are both 1. But does it achieve maximum? Yes, total 8. But does it achieve the per-interval maximum for each interval? Let's compute: For interval [1,3] length3 max=2, actual 2 ok. [2,4] length3 max=2, actual 2 ok. [2,5] length4 max=4, actual 4 ok. So it's also optimal. So there are many optimal assignments; alternating is just one.
Thus the solution is indeed to output an alternating pattern.
But before finalizing, we should confirm that the maximum per interval is indeed floor(L^2/4) and that it's always achievable simultaneously. Since we've argued alternating works, it's fine.
However, I want to be 100% certain: Could there be a scenario where alternating does NOT achieve floor(L^2/4) for some interval because of the specific alternating pattern? Let's compute with pattern 0101... for interval starting at 1 of length 3: positions 1=0,2=1,3=0 -> counts: zeros=2, ones=1 -> product=2 = floor(9/4)=2. For pattern 1010... interval length 3 starting at1: 1,0,1 -> ones=2, zeros=1 -> product=2. So fine.
For interval starting at 2 length 3 in pattern 0101: positions 2=1,3=0,4=1 -> ones=2, zeros=1 -> product=2. So always.
For even length: always equal.
Thus it's universal.
So the problem is essentially solved.
But wait, the constraints n,m up to 1000. Could there be a trick where m might be zero? Not, m≥1. But fine.
Thus the answer: print "01" repeated or "10" repeated, truncated to n.
Implementation: for i from 0 to n-1, output '0' if i%2==0 else '1' (or the opposite). That's O(n).
But let's think: Is it always true that the sum of maximums is achievable? Could there be a conflict if intervals force that for some interval the counts must be exactly (L/2, L/2) but for another overlapping interval, due to parity, it might be impossible to have both exactly balanced if they share positions? But alternating gives exactly balanced for even lengths and off by 1 for odd lengths, which is still maximum. So no conflict.
Thus the solution is trivial.
But I recall a Codeforces problem "Exhibition" maybe? I search memory: There is a problem "Sonya and Exhibition" (CF 1004B?) Actually, I think it's Codeforces Round #495 (Div. 2) problem B? Title: "Sonya and Exhibition". I recall a problem where you have to place roses and lilies to maximize sum of products over given intervals, and the solution is to output alternating string. Yes, I remember now: Codeforces problem "Sonya and Exhibition". The solution is indeed to output an alternating pattern. Many people solved it that way. The insight is that the maximum for each segment is achieved when numbers are as balanced as possible, and an alternating pattern ensures that for any segment, the numbers are balanced. So it's optimal.
Thus final answer: For i from 1 to n, if i%2==1 print '0', else '1' (or vice versa). We'll just output "01" repeated.
Let's produce Python code:
def solve():
import sys
input = sys.stdin.readline
n,m = map(int, input().split())
for _ in range(m):
input() # read intervals, not needed
result = ''.join('0' if i%2==0 else '1' for i in range(n))
print(result)
Or we can ignore intervals entirely.
Edge cases: n=1, output "0" or "1" both fine. We'll output "0".
Thus done.
But wait: The problem statement example outputs "01100" for n=5. If we output alternating "01010", would it be accepted? Yes, because any optimal answer is acceptable. The examples are just illustrations.
Thus finalize.
Now, I'll produce final answer with explanation. The maximum beauty of a segment of length L is achieved when the numbers of roses and lilies are as balanced as possible, i.e., the product equals (\left\lfloor \frac{L^2}{4} \right\rfloor).
An alternating pattern (e.g., 0101... or 1010...) guarantees that every contiguous subsegment has counts differing by at most one, thus each segment attains its individual maximum. Hence the total beauty is the sum of the individual maxima, which is optimal.
Since any alternating string works, we can output a simple alternating sequence of length n, such as '0' if the index is even and '1' otherwise (or the opposite).
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it))
# intervals are not needed for the construction
for _ in range(m):
next(it); next(it)
# build alternating string: 0101...
ans = ''.join('0' if i % 2 == 0 else '1' for i in range(n))
print(ans)
if __name__ == "__main__":
solve()