We need to parse the problem carefully. The problem description is a bit ambiguous, but we need to understand it properly.
We are given n words, each word length L_i. We have a banner with r lines, each line can contain at most c characters. We need to select a consecutive subsequence of words (i..j) such that they can be placed on the banner according to the rules:
- Words are placed on lines, in order, left to right, top to bottom.
- Within a line, words are separated by spaces. But the problem says: "Single words in one line of the banner should be separated by spaces. You are allowed to insert more than one space at once." So we can insert arbitrary number of spaces between words? Typically in such problems, we have to fit words into lines such that the total length of words plus spaces (minimum one space between words) does not exceed c. But here they say "You are allowed to insert more than one space at once." That suggests we can add extra spaces to fill the line, but we cannot exceed c characters total. So we need to place words such that the sum of lengths of words on a line plus at least (k-1) spaces (if k words) is ≤ c. Actually they say "Single words in one line of the banner should be separated by spaces. You are allowed to insert more than one space at once." That means that between words we must have at least one space, but we can add extra spaces. However, we also must not exceed c characters. So essentially, for a line that contains k words, the total length of those words plus some number of spaces between them that is at least (k-1) must be ≤ c. Since we can add extra spaces, we could always fill up to exactly c if we want, but the requirement is that we can write them on the banner. That means we need to check if there exists some arrangement of spaces (at least one between words) such that the total characters per line ≤ c. Since we can always make spaces minimal, the condition reduces to: sum of lengths of words in a line + (number of words in line - 1) ≤ c, because we can set exactly one space between consecutive words. If that holds, we can fit the words; we can also add extra spaces, but not needed. So the existence condition is that the minimal total length (words + 1 space between) ≤ c. Because we can always adjust spacing to be at least 1. So the condition for a line is: total width = sum(L_i) + (k-1) ≤ c.
Alternatively, if we want to maximize something, we might use extra spaces to better align? But the only requirement is to be able to write them; we can decide the exact spacing later.
But wait, the problem statement also says: "Note that you are not allowed to break the words, that is, each word in the text must occupy exactly one line in the banner." That means each word must be placed entirely in one line, not split across lines. Actually "each word in the text must occupy exactly one line in the banner" could be misinterpreted. It might mean that each word takes exactly one line? That would be weird. Actually reading: "Note that you are not allowed to break the words, that is, each word in the text must occupy exactly one line in the banner." Possibly it means each word must be entirely on a line (cannot be split), but the phrase "exactly one line" might be ambiguous. Usually "each word must occupy exactly one line" would mean each word is on a single line, not that each word gets its own line. But they want to say "each word must be on exactly one line (not split across lines)". I think that's the intended meaning: words cannot be broken across lines; each word appears in exactly one line (i.e., it belongs to a single line, not multiple). So the usual line-wrapping constraint.
Thus we have to place a sequence of words into at most r lines, each line width at most c (with at least one space between words), and we can choose to add extra spaces. So the existence condition is as described: we can pack the words into lines respecting the width limit with at least one space between words.
We need to find the maximum number of consecutive words (from i to j) that can be placed on the banner (i.e., there exists a layout with at most r lines, each line ≤ c, using exactly that sequence of words in order). Then output the banner (any valid layout) that uses that maximal subsequence. If there are multiple, any is fine.
But there's a twist: "Note that some lines of the banner can be empty. You are allowed not to print such lines." So we don't have to output empty lines.
We have constraints: n, r, c up to 1e6, but r*c ≤ 1e6. Also total characters in words ≤ 5e6.
We need to output the actual banner: at most r lines, each line at most c characters, with words separated by spaces. Since we can choose spacing arbitrarily, we should produce some valid arrangement. Typically we would want to output the banner with minimal spacing (single spaces) for simplicity, but we could also add extra spaces if we want. However, we must ensure that each line length does not exceed c. If we use single spaces, then we must have sum(L) + (k-1) ≤ c. That is the condition for the line to be valid. So if we constructed the subsequence based on that condition, we can output with single spaces; that will satisfy ≤ c. So outputting with single spaces is fine.
But careful: We might have lines that are shorter than c, that's allowed.
Thus the main problem: Given n words with lengths L_i, and parameters r, c, find the longest contiguous subsequence (i..j) such that the words can be packed into at most r lines, each line with width constraint (sum of word lengths + number of words - 1 ≤ c). Then output the packing (any) of that subsequence.
We need to produce the actual banner lines.
Observation: Since rc ≤ 1e6, and n can be up to 1e6, but the maximum number of words we can possibly place is bounded by total characters? Actually we can place many words if they are short. But the banner has at most r lines, each line at most c characters, so total characters printed on banner (including spaces) ≤ rc ≤ 1e6. The sum of lengths of words we place plus spaces ≤ rc. Since each word length at least 1, and at least one space between words except maybe at line start/end? Actually spaces are between words within lines. So total characters used = sum(L_i) + (#words placed - #lines) because each line has (#words_in_line - 1) spaces. Since each line at least one word, total spaces = total_words_placed - #lines. So total characters = sum(L_i) + total_words_placed - #lines. This must be ≤ rc. Since #lines ≤ r, we have sum(L_i) + total_words_placed ≤ rc + #lines ≤ rc + r. But rc ≤ 1e6, so total_words_placed is at most something like 1e6 (since each word length at least 1, sum(L_i) ≥ total_words_placed). So the number of words in the maximal subsequence is also bounded by around 1e6, but could be as high as 1e6 if words are length 1? But rc ≤ 1e6, and if each word is length 1, then total characters = total_words_placed + (total_words_placed - #lines) = 2total_words_placed - #lines ≤ rc. So total_words_placed ≤ (rc + #lines)/2 ≤ (1e6 + r)/2, so up to about 5e5. However we also have r up to 1e6, but rc ≤ 1e6, so r could be large but then c small. So number of words placed could be up to r (if each line contains one word, word length ≤ c). Since r could be 1e6, c=1, but then word lengths must be 1. So number of words placed could be up to 1e6 as well. So it's plausible we need to handle up to 1e6 words.
Thus we need an efficient algorithm to find the longest segment that can be packed into at most r lines.
This is reminiscent of the problem of fitting words into lines with given width, and we want to know for each starting index i, what is the farthest j such that the words i..j can be placed in ≤ r lines? Then we find the maximum length segment. Since n up to 1e6, we need O(n log n) or O(n) algorithm.
We could treat it as a sliding window with a greedy line breaking? For a given segment of words, we need to check if they can be packed into r lines. This is similar to checking if the number of lines needed by the greedy algorithm (put as many words as possible on each line, given width c) is ≤ r. The greedy algorithm (always take as many words as possible on a line, respecting width) is optimal in terms of minimizing number of lines because there is no penalty for line breaks; we just need to pack into as few lines as possible. Indeed, to minimize number of lines used for a given sequence, we can greedily pack each line with as many words as possible (the standard "minimum number of lines" given line width). This yields the minimal number of lines required to fit the sequence. So for a segment, we can compute the minimal lines needed via greedy. Then check if ≤ r.
Thus problem reduces to: For each i, find the maximal j such that the greedy line count for segment i..j is ≤ r. Then find the segment with maximal j-i+1.
We need to compute efficiently. Could use two pointers/sliding window and maintain greedy line counts dynamically. However, greedy packing is not easily maintained when moving start index forward. Alternatively, we can precompute next break positions: For each word index i, define next[i] = the smallest index > i such that the words from i to next[i]-1 fit in one line but adding word next[i] would exceed line width (if we try to pack as many as possible starting at i). This is the standard "next word after a line" when packing greedily. Then the number of lines needed for segment [i, j] can be computed by iterating through these pointers: start at i, then repeatedly go to next[ current start ], counting lines, until we exceed j. This is like jumping. Then we can find, for each i, the farthest j such that line count ≤ r, using binary lifting (doubling) because n up to 1e6 and we need to query many i's? Possibly we can compute for each i the end index if we use at most r lines by climbing the next pointers r times. That would give j as the index after r lines (i.e., the first index after r lines). However careful: If we start at i, after k lines we cover words up to some index. The number of lines used to cover words from i to j is the minimum number of lines needed. For a fixed i, the set of j that can be covered in ≤ r lines is a contiguous range from i up to some maximum j_max(i) (since adding more words can only increase lines needed). And j_max(i) is the last index that can be covered in exactly r lines? Actually if we have r lines, we can cover up to the start of the (r+1)th line. Specifically, if we compute the greedy "next" jumps: let f^t(i) be the index after t lines (i.e., after packing t lines greedily starting at i). More precisely, define nextLineStart(i) = the smallest index > i such that words from i to nextLineStart(i)-1 fit in one line (maximal), i.e., the index of the first word on the next line. So after one line, we are at index p1 = nextLineStart(i). After two lines, p2 = nextLineStart(p1). After r lines, we get p_r (which could be > n if we exceed). Then the segment i..(p_r - 1) can be packed in exactly r lines? Actually if p_r <= n+1, then words from i to p_r-1 can be packed in at most r lines (specifically, r lines exactly if we stop at p_r-1). But if p_r == n+1 (past end), then all words from i to end can be packed in ≤ r lines. Also if we use fewer lines than r, that also works. However, the minimal number of lines for segment i..j is ≤ r if and only if j is less than p_r? Let's think.
Let L(i) denote the minimal number of lines needed to pack words from i to some j. Actually we can think of the greedy packing: Starting at i, we fill a line as much as possible, get next start s1 = next[i]. Then from s1, fill line, get s2 = next[s1], etc. This yields a sequence of line endpoints. For a given j, the number of lines needed is the smallest t such that s_t > j (where s_0 = i). That is, we need the smallest t where the t-th line's start after packing exceeds j. So t = number of lines used to cover up to j. So condition t ≤ r is equivalent to: The start of line (r+1) (i.e., s_r) > j. Because if after r lines we have covered up to s_r - 1; if j ≤ s_r - 1, then we can pack in ≤ r lines. So j_max(i) = s_r - 1, where s_r is the index after r lines (i.e., the start of the (r+1)-th line) computed via greedy starting at i. However, note that if we stop early, we could possibly use fewer lines, but the condition is ≤ r, so the maximum j is exactly s_r - 1. Because if we try to include s_r (the first word of line r+1), then we would need at least r+1 lines. But maybe we could use a different packing (not greedy) to fit more words into r lines? However, greedy yields the maximum number of words per line, so it minimizes number of lines. So if the greedy packing needs r+1 lines for a segment, no other packing can do it with r lines. Because greedy gives the minimal lines; if minimal lines > r, then impossible. So yes, j_max(i) = (index after r greedy lines) - 1.
Thus if we precompute next[i] (the start index of next line when packing greedily from i), then for each i, we can compute s_r = next^r(i) (apply next r times) and then j_max = s_r - 1. However, note that next[i] can be defined for i from 1 to n+1? Typically next[i] gives the index of first word of next line if we start at i. So next[i] > i. And if we start at i and the line cannot even hold the first word? But each word length ≤ c? The problem might have words longer than c. That is possible: a word length may exceed c. In that case, can it be placed on a line? The constraint says each line can contain at most c characters. If a word is longer than c, it cannot be placed on any line because you cannot break it. So such word cannot be included in any banner. That means the subsequence we select must consist only of words with length ≤ c; otherwise it's impossible to place. But the problem does not explicitly say word lengths are ≤ c. So we need to handle that: If a word length > c, then any segment containing it cannot be placed. So essentially, we should treat such words as impossible. However, there is a nuance: The banner has exactly r lines, but we are allowed to have empty lines? The subsequence must be placed entirely; if a word > c, it cannot be placed in any line because the line has at most c characters and the word alone would exceed c. So it's impossible. So such a word cannot be part of any valid segment. So effectively, we need to consider segments that consist only of words with length <= c. And the greedy packing: For a starting index i, we need to compute next[i] as the first index after i such that the cumulative sum of lengths + spaces from i to next[i]-1 fits in ≤ c, but adding word next[i] would exceed. But if word i itself > c, then next[i] = i+1? Actually, we cannot even place word i alone on a line because it's too long. So segment containing i is invalid. So in the DP, we should treat such indices as impossible; j_max(i) would be i-1 (empty segment). But we want to find the longest valid segment. So we need to skip over words with length > c.
Thus, we can first filter or note that any segment containing a word longer than c is invalid. So we can split the text into groups separated by such words. Within each group, all words have length ≤ c. Then we can apply the greedy next pointers.
Now we need to compute for each i (within valid groups) the furthest j reachable with ≤ r lines. The length is j-i+1. We need the maximum over all i.
A direct approach: For each i, we could walk r steps via next pointers. But r can be up to 1e6, and n up to 1e6, so O(n*r) is too big.
But we can precompute the "jump" using binary lifting: precompute up[k][i] = index after 2^k lines starting at i. Then for each i we can compute s_r by iterating bits of r. That would be O(n log r) time, which with n=1e6, log2(1e6)~20, feasible. However memory: nlog r = 1e620 = 20 million integers, each 4 bytes = 80 MB, which might be okay? But we need to be careful about memory constraints; typical Codeforces limit is 256 MB, so 80 MB is okay. But we also have to store words and lengths. Also total characters up to 5e6. So memory maybe okay.
But we can also use two-pointer sliding window with a data structure that maintains line count as we expand/contract? That might be more complex.
Given the constraints, binary lifting is straightforward. However, we need to compute next[i] efficiently for all i. That is, for each i, we need to find the maximal segment starting at i that fits in one line (sum of lengths + (k-1) ≤ c). This is a classic "two-pointer" scanning to compute the furthest index reachable from i in one line. Since we need next[i] for all i, we can compute using a sliding window that moves start pointer. Specifically, we can maintain a window [i, j] such that it fits (cumulative sum + spaces ≤ c) and try to extend j until it fails, then next[i] = j+1 (since adding j+1 would exceed). Then when moving i to i+1, we adjust sum. Complexity O(n). But we must consider words with length > c: if a word length > c, then next[i] for i at that word? Actually, if word i itself > c, then the window from i cannot even include i alone? But the condition: sum of lengths + spaces: if only word i, sum = L_i, spaces = 0, so need L_i ≤ c. If L_i > c, then no valid line can start at i (i.e., you cannot place word i at all). So for such i, we can set next[i] = i (or some sentinel) indicating invalid. But we will treat i as not a valid start. However, for our binary lifting, we only care about indices where L_i ≤ c. But when we have a word > c, it effectively breaks the chain; we cannot jump over it because a segment containing it is invalid. So we can split the text into segments of words with length ≤ c. Within each segment, we can compute next pointers for each index, where next[i] will be within the same segment, and maybe next[i] can go beyond the segment? Actually if the segment is contiguous words all ≤ c, then the line may end within the segment. next[i] will be the first index after the maximal line from i. It will be > i and could be at most n+1. Since all words are ≤ c, it's possible that the line may hold many words, but if we encounter a word > c within the segment, it doesn't happen because we are only in segment with all ≤ c. So it's fine.
But careful: If there is a word with length > c, that word cannot be placed, but the line starting at a previous index might try to include it? Actually, if we start at i before the long word, the greedy algorithm would try to pack as many as possible. Since the long word itself > c, the line cannot include it because even alone it would exceed? Wait, if a word length > c, it cannot fit even by itself. So when building a line starting at i, as we scan forward, if we encounter a word with length > c, then the condition sum+L_i + spaces? But we are summing lengths; if that word is encountered, it will definitely cause overflow because even alone (if it's the first word of the line) would exceed c. But if it's not the first word, the line already has some words, and adding it would definitely exceed because its length > c, but the sum before may be 0? Actually, the line's cumulative sum must remain ≤ c. If we have L_j > c, then even if sum before is 0, adding that word alone would exceed c. So the line cannot include it. So the maximal line starting at i will stop before that word. So next[i] will be the index of that long word (since that word cannot be included). Then any segment containing that long word is invalid, and our j_max(i) computed by r jumps may cross over that long word? But we said we should restrict to segments where all words have length ≤ c. The condition for a segment to be valid is that each word length ≤ c and also the minimal number of lines ≤ r. If we compute next[i] ignoring the long word (i.e., if we treat it as a barrier), then when we jump r lines, we may land on a long word or beyond. If we land on a long word, then the segment i..(j) would include that long word? Actually, if next[i] points to a long word (i.e., the first index that cannot be included), then the line from i ends right before that long word. So the segment covered by that line excludes the long word. Then the next line starts at that long word. But if the long word is >c, then starting a line at that long word is impossible (since it can't fit even alone). So the greedy algorithm would then have a line that cannot contain any word? Actually, if we attempt to start a line at a word with length > c, it's impossible to place that word. So such a segment cannot be packed. Therefore, in a valid segment, we cannot have any word > c. So when we compute j_max(i), we must ensure that all words in [i, j_max(i)] have length ≤ c. So if there is any long word in that range, it's invalid. So we need to treat such words as barriers that break the continuity; essentially we can only consider segments that lie entirely between long words. So we can split the text into "good" blocks where all words have length ≤ c. Then within each block, compute next pointers and binary lifting to find max segment. The answer will be the longest segment among blocks.
Thus the algorithm:
- Read n, r, c.
- Read the text line (maybe using getline). The words are separated by spaces. We need to store each word (as string) and its length L_i. Total characters ≤ 5e6, so we can store the strings in an array.
- For each i from 1..n, compute len[i] = length(word[i]).
- Determine if len[i] > c. For such i, they are "bad".
- Partition into blocks: intervals of consecutive indices where all len ≤ c.
For each block, we need to compute for each index in the block the "next" pointer: next[i] = smallest j > i such that the words from i to j-1 can fit in one line, but adding word j would exceed (or j = n+1 if all remaining fit). In other words, we want the first word that does NOT fit in the line starting at i. So if we define a line's capacity: we can include words from i up to p-1, where p is the smallest index > i such that sum_{k=i}^{p-1} L_k + (p-i-1) > c. Since we need at least one space between words, the total width if we include words i..p-1 is (sum of lengths) + (p-i-1). We require that ≤ c. So p is the maximal index such that the condition holds, plus 1.
We can compute next[i] using two pointers (sliding window) because the condition is monotonic. For a given i, we want the maximal p such that the condition holds. As i increases, p will not decrease. So we can do:
Initialize j = 1? Actually we need to process from 1 to n. We'll maintain a sum of lengths of window [i, j] inclusive? Let's define end pointer e = i (the end of line candidate). We'll increment e while we can add word e (with spaces). But careful: When adding a new word e (starting from i), we need to check if current sum + (number of words currently - 1?) Actually simpler: We'll maintain the window [i, e] inclusive, where we have words i..e. The total width if we include i..e is (prefix sum of lengths from i to e) + (e-i). Because there are (e-i) spaces between i and e. The condition is sum_len(i,e) + (e-i) ≤ c. So we can compute prefix sums of lengths to compute sum_len quickly, or slide.
But sliding window with O(n) total works: keep a variable cur_len = 0, and count = 0. For i from 1 to n (within good block), we start e at previous e (since e is non-decreasing). For each i, we want to find the maximum e such that condition holds. While e+1 ≤ n and len[e+1] ≤ c? Actually we are within good block, so all words have len ≤ c. But we need to ensure we don't cross block boundary; but we can treat block boundaries by stopping at block end. But while e+1 ≤ n and condition with adding word e+1 holds: cur_len + len[e+1] + (count) ≤ c? Because currently we have count words (from i to e). If we add a new word, we need to add a space before it if count>0. Actually simpler: we maintain the window [i, e] inclusive, and we compute total width as cur_len + (e-i) where cur_len is sum of lengths. Alternatively, we can precompute prefix sums of lengths and prefix sum of (len+1) maybe.
Given n up to 1e6 and total chars 5e6, we can compute prefix sums array of length n+1: ps[i] = sum_{k=1}^{i} len[k]. Then sum_len(i, e) = ps[e] - ps[i-1]. Condition: (ps[e] - ps[i-1]) + (e-i) ≤ c => (ps[e] + e) - (ps[i-1] + i-1) ≤ c? Actually (e-i) = e - i. So inequality: ps[e] - ps[i-1] + e - i ≤ c => (ps[e] + e) - (ps[i-1] + i) ≤ c? Let's compute: ps[e] - ps[i-1] + e - i = (ps[e] + e) - (ps[i-1] + i). Because ps[i-1] + i includes an extra 1? Let's check: (ps[e] + e) - (ps[i-1] + i) = ps[e] - ps[i-1] + e - i. Yes, correct. So we can define q[i] = ps[i] + i. Then condition becomes q[e] - q[i-1] ≤ c. Actually q[i-1] + c? Wait: q[e] - q[i-1] ≤ c. That's a nice monotonic condition.
But careful: q[i] = ps[i] + i. Then for i=1, q[0] = ps[0] + 0 = 0. Then condition: q[e] - q[i-1] ≤ c. So given i, we need the maximum e such that q[e] ≤ q[i-1] + c. Since q is non-decreasing (len positive), we can binary search for each i. That would be O(n log n), which is acceptable (1e6 * log 1e6 ~ 20 million). But we might do O(n) with two pointers because it's also monotonic. Since we need also to compute binary lifting, O(n log n) might be okay. However we also need to handle bad words; we can set next[i] = i for bad i? Actually for indices i that are bad (len>c), we will not consider them as starting points. However, next[i] for those i would be something like i+1? But we won't use them. Better to split into blocks.
But note: Within a block, all words have len ≤ c. But the condition for next[i] also must respect block boundaries: if the maximal e reaches beyond the block end, that's fine; but if the next word after e is beyond block? Actually block ends at the last good word; after that there might be a bad word. The line cannot include a bad word because its len > c. So the maximal e for i within block cannot include the bad word because including it would violate condition (since its len > c, and even if sum+spaces was ≤ c before, adding that word would exceed because its len > c, but maybe previous sum was 0? Actually if the bad word is at position p, its len > c, then condition with that word would always fail because q[p] - q[i-1] ≥ len[p] (since q[p] - q[i-1] = sum_len + (p-i+1?) Actually careful: For e = p, the condition includes the bad word. Since len[p] > c, and other terms non-negative, q[p] - q[i-1] ≥ len[p] > c, so fails. So the maximal e will be < p. So the block boundary is naturally enforced by the condition. So we can just compute next[i] using the inequality without worrying about blocks, but we must be careful that for a bad i, we should not compute next[i] as it's not a valid start. However, for binary lifting we need next values for indices that are within a block; we can compute for all i, but if we compute for bad i, the jumping may cross bad indices? But we can simply not start from bad indices, and for indices within a block, their next[i] will point to an index > i that is either within block or possibly to a bad index? Let's examine: If i is in a block and the line we build ends before a bad word (if any), then next[i] will be the first index that cannot be included, which could be the bad word. So next[i] could point to a bad index. Then if we jump from i using binary lifting, we might land on a bad index. But if we ever land on a bad index, then the segment from i to the position before that bad index might be covered, but the line starting at that bad index would be impossible. However, the greedy algorithm for lines would try to start a line at that bad index, but since that word cannot fit, the line cannot be formed, so the packing would fail. So for a segment to be packable, all words must be good. Therefore, if any line starts at a bad index, that would mean we are trying to include that bad word, which is impossible. So when we compute j_max(i) as (next^r(i) - 1), we must ensure that all intermediate indices (the starts of lines) are good? Actually the greedy packing process: Starting at i (good), we fill line 1, ending before next[i]. Then line 2 starts at next[i]. For the packing to be feasible, line 2 must be able to be formed, meaning the words from next[i] onward must be packable into a line (with at most c width). But if next[i] is a bad index (len > c), then line 2 cannot even start because the first word cannot fit. Therefore, the segment i..(next[i]-1) can be packed in 1 line, but if we need more lines, we need the subsequent lines to be valid. So when we say "segment i..j can be packed in ≤ r lines", the greedy algorithm must be able to form the required number of lines without hitting an invalid start. So we need that for all steps t from 0 to r-1, the start of line t+1 is an index that is within the segment and the line from that start to its end (which is next[start] - 1) is valid. So all those start indices must be good (len ≤ c) and the lines must fit. The greedy algorithm's next pointers already encode the lines: if a start index s has len[s] > c, then next[s] is not defined? Actually we could define next[s] as s+1? But then the line from s would be empty? Not correct. So we need to ensure that in the segment we consider, all words are good. So if any next pointer leads to a bad index, then after that point the packing fails because the next line cannot be formed. But does that matter for j_max(i)? Consider we want to pack exactly r lines. Starting from i, we apply next r times to get s_r. If any intermediate start (including the first?) is bad? i is good by assumption. If next[i] is bad, then after 1 line, the next start is bad. To pack a second line, we need to include words starting at that bad index, but that's impossible. So if r >= 2, then the segment i..(s_r-1) would include that bad word? Actually, s_1 = next[i] is the start of second line. If that is a bad index, then the words from s_1 onward cannot be placed on a line. So the greedy packing would not be able to form the second line; thus the minimal number of lines needed to cover words beyond the first line would be infinite? Or we could consider that we cannot cover any word from that bad index. So the segment i..j that we can pack must end before that bad index if we need more lines? Let's clarify: Suppose we have i good, next[i] = p where len[p] > c. Then the first line covers i..p-1. To pack a segment that includes words beyond p-1, we must also include p (since it's the next word). But p cannot be placed. So any segment that includes p is invalid. So the maximum j we can cover with r lines, for r >= 2, is still p-1, because including p is impossible. But can we cover more words by not using the greedy packing? Could we pack differently, like not packing the first line as greedily, to possibly include p later? If p > c, it's impossible to place p on any line, so any segment containing p is invalid. So indeed, j_max for r>=2 is still p-1, because we cannot include p. So effectively, if there's a bad word within the block, it splits the text. So we should treat only contiguous good words as blocks. So if we compute next[i] within a block of only good words, then next[i] will always point to an index within the block or to n+1, because the line will stop before any bad word, as the condition fails at the bad word. But if there is a bad word within the block? By definition block is only good words, so no bad words inside. So next[i] will point to an index that is also within the block or the block's end+1. Good.
Thus we can just split into blocks of good words and process each block independently. For each block, we compute next[i] for i in block (including the last index, but for the last index, next[i] will be block_end+1? Possibly n+1 if block goes to end). Then we can compute jumps.
Now, we need to compute for each i in block the farthest j such that segment i..j can be packed in ≤ r lines. That is j_max(i) = (next^r(i) - 1). However, we need to consider that the number of lines used might be less than r if we reach the end before using r lines. But that's fine; j_max(i) would be block_end if next^r(i) exceeds block_end? Actually if applying next r times goes beyond n (or block end), then j_max(i) = block_end (since we can cover up to block_end). But careful: If we apply next r times and the last application lands at an index > block_end+1? Actually next pointers are defined only for indices within block; but if we start at i and after k lines we reach an index s, which is the start of next line. If s > block_end, that means we have covered all words up to block_end. So j_max(i) = min( block_end, next^r(i)-1 ). But since next^r(i) could be > block_end+1? Possibly it could be block_end+1 (if exactly at the end after covering all words) or larger if we overshoot. But next^r(i)-1 could be >= block_end. In any case, the valid segment from i can go at most to block_end. So we define j_max(i) = min(block_end, f(i) - 1), where f(i) = the index after applying next r times starting from i, but if we reach an invalid state (like a start that is not good) we stop? But within a block, all starts are good until we go beyond block end. However, next[i] for the last index in block: If the last word can fit in a line alone, next[last] = block_end+1 (since after covering it, the next line would start at block_end+1). That's fine. So f(i) is computed by repeatedly applying next, but if at some point we try to apply next to an index outside the block (i.e., > block_end), we should treat next[x] as something? Actually we only need up to r steps; we can precompute next for all indices from 1 to n+1 perhaps. For indices beyond n, we can define next[n+1] = n+1 (or some sentinel). But we can limit to block.
Simpler: For each i in block, we want to know the furthest reachable index using at most r lines. Since the block consists only of good words, we can use binary lifting on next pointers defined for all positions in the block, and also define next for the position after the block as n+1 (or block_end+1) to indicate end.
But careful: The greedy next pointer for a position i is defined as the smallest index > i such that words i..next[i]-1 fit in one line (maximally). That definition works for any i within the block, as long as len[i] ≤ c. If len[i] > c, then next[i] is undefined because we cannot even start a line; but we won't use those.
Now, the binary lifting: We need to compute up[k][i] where up[0][i] = next[i] (or maybe we want to jump lines, not words). But note that next[i] is the start of the next line after packing a line starting at i. So applying next once corresponds to moving to the start of the next line (i.e., after consuming one line). So after t lines, the start index is next^t(i). Then the segment covered is i .. next^t(i)-1. So for a given i, we want the smallest t such that next^t(i) > block_end? Actually we want the maximum j such that number of lines needed ≤ r. That is j = next^r(i) - 1, but if next^r(i) > block_end+1, then j = block_end. However, note that if we apply next r times, but we might exceed block_end before r steps, then next^r(i) might be > block_end+1? Let's consider: Suppose block_end = n. If after k < r steps we have next^k(i) = n+1 (i.e., we have covered all words), then for any further steps, we need to define next[n+1] = n+1 (since there are no words left). Then next^r(i) would be n+1 as well. So j = n+1-1 = n, which is correct. So we can define next[n+1] = n+1. Similarly for any index beyond n, we can set next = itself.
Thus we can extend next to indices up to n+1 (or maybe up to n+1 + some log). For binary lifting, we need up to n+1.
Now, for each i in block (i from block_start to block_end), we can compute f = apply next r times (using binary lifting) and then j = f - 1. But careful: f could be n+1 (if we reach end) or some index within block. Then segment length = min(j, block_end) - i + 1? Actually if f is inside block, then j = f-1 is the last word covered. So length = (f-1) - i + 1 = f - i. But we need to ensure that we haven't included any word beyond block_end? f is computed from next which might go beyond block_end only if we step outside. But if f is inside block, then the segment ends at f-1 which is within block. So length = f - i.
But we need to ensure that the segment i..f-1 is actually packable within r lines. Since we used the greedy next jumps, the number of lines used is exactly the number of jumps we made, which is r, but if we reached the end before using r jumps, we may have used fewer lines, but that's ≤ r, so it's fine. So j = f-1 is the farthest index we can cover with at most r lines, because if we try to include the next word (f), we would need more lines (or it's impossible). But is it always the case that we cannot extend by even one more word with the same number of lines? According to greedy minimal lines, if the minimal lines needed for i..j is ≤ r, then j ≤ next^r(i)-1. And indeed, next^r(i)-1 is the maximum j such that min_lines(i,j) ≤ r. So it's correct.
Thus, for each i, we can compute j_max(i) quickly using binary lifting.
Now we need to find the i that maximizes (j_max(i) - i + 1). Then we need to output the banner using the words from i_opt to j_opt. Also we need to output the banner: at most r lines, each line up to c characters, with words separated by spaces (single space by default). We can output the greedy packing for that segment: starting at i_opt, repeatedly take as many words as possible per line (maximizing words per line) until we cover all words up to j_opt. That will use the minimal number of lines (which is ≤ r). But we must output at most r lines. If the number of lines used is less than r, we just output those lines; the remaining lines can be empty and we can skip printing them (or just don't print). The problem says: "Print at most r lines, in each line print at most c characters — the optimal advertisement banner. If there are multiple advertisement banners, print any of them. Note that some lines of the banner can be empty. You are allowed not to print such lines." So we can output exactly the lines we need, not necessarily r lines.
We need to output the actual words with spaces. Since we used the greedy packing (which gives exactly the next pointers), we can output lines accordingly.
Edge Cases: If r=0? But r ≥ 1 per constraints (1 ≤ n, r, c ≤ 10^6). So r≥1.
If there are no valid segments (i.e., all words are longer than c), then the maximum number of consecutive words we can place is 0? But we need to output an advertisement banner. The problem says "Print at most r lines, in each line print at most c characters — the optimal advertisement banner." If the maximum number is 0, meaning we can't place any word? That would be a segment of length 0. But the problem likely expects at least one word? Possibly if no word fits (all >c), then the best we can do is an empty banner (maybe print nothing). But the problem states "you should determine which maximum number of consecutive words from the text can be written on the banner." If no word can be written, then maximum number is 0, and we can output nothing (or empty lines). But the output might be ambiguous; maybe the problem guarantees that there is at least one word with length ≤ c? Not necessarily. However, constraints: r*c ≤ 10^6, c could be very small (1). Words length can be > c, indeed. But we need to handle case where no segment exists. In that case, we can output nothing (maybe just no lines). The problem says "Print at most r lines" so we can print 0 lines. That should be acceptable.
But we need to check sample: Example 3: n=6, r=2, c=3, words: "croc a a a croc a". The lengths: "croc" length 4 >3, "a" length 1, etc. So there are words longer than c. The output is:
a a
a
So they selected words from position 3? Actually words: 1:croc(4),2:a(1),3:a(1),4:a(1),5:croc(4),6:a(1). They picked a segment of words 2-4? Wait output: "a a" and "a". That's two lines: first line "a a" has two words, second line "a" has one word. That corresponds to words: positions 2,3,4? But "a a" could be positions 2 and 3? and "a" could be position 4? That would be three words. However, they have only two lines, r=2, c=3. Word "a" length 1. For three words: "a a a" would require first line: could fit "a a" (1+1+1 space=3) exactly, second line "a" fits. So they could have taken 3 words. But they output only 2 words? Let's check: Output: "a a" then "a". That's three "a"s? Actually "a a" is two words. Then "a" is one word, total 3 words. So they took three words. Which three? Possibly indices 2,3,4? That would be "a a a". But the output shows "a a" and "a". That could be either (2,3) on first line, (4) on second; or (2) and (3,4) but that would be "a" and "a a"? They printed "a a" then "a", so first line two words, second line one word. That's consistent with indices 2,3,4. But why didn't they take indices 3,4,5? 5 is "croc" length 4 >c, can't. So max segment is probably words 2-4, length 3. But could we take words 2-6? No because 5 is too long. So max length is 3. So output matches.
Now we need to ensure our algorithm yields that segment.
Now the complexity: O(n log r) for binary lifting, plus O(n) for computing next pointers.
But we must be careful about memory: storing up to log2(r) (max 20) arrays of size n+2 (since r up to 1e6, log2(1e6)≈20). That's okay.
Alternatively, we could use a two-pointer + sparse table for range maximum? Another approach: For each i, we could compute the furthest j such that number of lines ≤ r by using a sliding window with a data structure that tracks lines needed. Could be done with two pointers and a pointer that moves based on next jumps? But binary lifting is simpler.
Let's design details:
- Input: n, r, c.
- Read the entire line of text. We'll parse words. We'll store them in an array of strings words[1..n] (or we can store lengths and the actual strings). Since total characters ≤ 5e6, storing strings is fine.
- Compute lengths len[i].
- Determine good[i] = (len[i] <= c). If good[i] is false, then word cannot be used.
We need to compute next[i] for all i from 1 to n+1? Actually we need next for indices i that are good, but for binary lifting we need a value for all i (including bad) to avoid segmentation faults, but we can treat bad indices as having next[i] = i+1? But they are not used as start, but they might be reached via jumps from a good start? As argued, if we start from a good index, after some jumps we might land on a bad index if there is a bad word in the block? But we split into blocks, so within a block all are good, and next pointers from positions in the block will point to positions within the block or to block_end+1. If block_end+1 is a bad word or n+1, that's fine. But what about jumps that go beyond block_end? For binary lifting we need next for indices beyond block? We'll compute next for all indices 1..n+1, setting for indices with len[i] > c we can set next[i] = i+1? But that would be incorrect for line packing. However, we will only use starting indices that are good, and we will only consider segments within good blocks. When we compute j_max(i) using binary lifting, if we ever jump to a bad index, then the segment would include that bad word? Actually not; the jump result f = next^r(i) is the index after covering r lines. If at some intermediate step we land on a bad index, that means that after some line we started a line at a bad word, which is invalid. But would the greedy algorithm ever start a line at a bad word? It would if the previous line ended right before the bad word, and the bad word is the next word. In that case, the greedy algorithm would attempt to start a line with that bad word, but since it can't fit, the line cannot be formed, so the packing fails. However, our definition of next for a bad word: if we try to compute next[bad] using the condition, it's not defined because we cannot even place the word alone. But in the greedy packing process, if we ever encounter a start that is bad, then the packing cannot continue; thus the segment i..j that we are trying to cover cannot include that bad word, because the moment we need to start a line at a bad word, it means the previous line ended before the bad word, and the bad word is not yet covered. So to cover a segment that goes beyond the bad word, we would need to include the bad word, which is impossible. So the maximum j for any starting i before a bad word is up to the index before the bad word. That aligns with block splitting: the block ends right before the bad word. So we should treat the bad word as a barrier and not consider crossing it. Therefore, we can simply split into blocks of good words, and within each block, compute next and jumps.
Thus our algorithm:
- Compute prefix sum array ps[0]=0, ps[i]=ps[i-1]+len[i].
- Compute q[i] = ps[i] + i. Actually we derived condition: for start at i, the maximum end e satisfies q[e] - q[i-1] ≤ c. Because total width = (ps[e]-ps[i-1]) + (e-i) = (ps[e]+e) - (ps[i-1]+i). So condition: q[e] ≤ q[i-1] + c.
So for each i, we can binary search for the largest e such that q[e] ≤ q[i-1] + c. However, we also need to ensure that each word length ≤ c individually? The condition will automatically fail if a word > c because q[e] - q[i-1] includes the word's length plus spaces. But if we start at i, and len[i] > c, then q[i] - q[i-1] = len[i] + 1? Actually q[i] = ps[i] + i, so q[i] - q[i-1] = len[i] + 1. The condition for e = i would be q[i] ≤ q[i-1] + c => len[i] + 1 ≤ c? That's stricter than needed because we need only one word: total width = len[i] (no spaces) not len[i]+1. Wait, check: For a line starting at i and ending at e, total width = sum_{k=i}^{e} len[k] + (e-i). For e = i, width = len[i] + 0 = len[i]. In our derived formula: total width = q[e] - q[i-1] - (???). Let's recalc carefully.
We have:
sum_len(i,e) = ps[e] - ps[i-1].
Spaces: (e - i) spaces (since there are e-i spaces between e-i+1 words).
Total = (ps[e] - ps[i-1]) + (e - i).
Now q[i] = ps[i] + i? Let's compute q[e] - q[i-1] = (ps[e] + e) - (ps[i-1] + (i-1)) = (ps[e]-ps[i-1]) + (e - i + 1). That's total + 1. So total = (q[e] - q[i-1]) - 1.
Thus condition total ≤ c becomes (q[e] - q[i-1] - 1) ≤ c => q[e] - q[i-1] ≤ c + 1.
Better: q[e] - q[i-1] ≤ c + 1.
Check with e=i: q[i] - q[i-1] = len[i] + 1. Condition: len[i] + 1 ≤ c + 1 => len[i] ≤ c. That's correct. So the correct inequality is: q[e] ≤ q[i-1] + c + 1.
Thus we can compute next[i] as the smallest index > i such that the line cannot include it? Actually we want the maximal e such that total ≤ c. That is the largest e satisfying q[e] ≤ q[i-1] + c + 1. Then next[i] = e + 1 (the first index not included). So we can compute next[i] = that e + 1.
Now we can compute next[i] for all i (including those with len[i] > c). For i where len[i] > c, the condition for e = i fails because q[i] = q[i-1] + len[i] + 1 > q[i-1] + c + 1 (since len[i] > c). So the largest e satisfying is i-1, i.e., no words can be placed. In that case, we could set next[i] = i? But that would indicate a line of zero words? Not valid. To avoid complications, we will only compute next for good i. For bad i, we won't use them as start. However, when computing jumps, we may land on a bad index via next from a good i? Let's see: For a good i, we find e such that q[e] ≤ q[i-1] + c + 1. If the next word after e (i.e., e+1) is bad, then e+1 will be a bad index. And next[i] = e+1. So next[i] could point to a bad index. Then if we apply next again from that bad index, we need to define next[bad] appropriately. But as argued, if we ever try to start a line at a bad word, the packing fails. So for the purpose of computing j_max(i) (the furthest reachable index with ≤ r lines), we should not consider segments that require starting a line at a bad word. In the greedy packing, if we have a line starting at a bad word, that line cannot be formed, so we cannot cover any word from that bad word onward. Therefore, if at any point we encounter a bad start, the number of lines needed to cover further words becomes infinite (or impossible). So for a segment i..j to be packable, all starts encountered during greedy must be good. This means that for the packing to be valid for j up to some point, the starts (i, next[i], next[next[i]], ...) up to the line before covering j must be good. So when we compute j_max(i) as next^r(i)-1, we must ensure that all intermediate indices are good. If next[i] points to a bad index, then for r>=2, the segment i..next^2(i)-1 would include that bad word? Actually next^2(i) = next[next[i]]. If next[i] is bad, then next[next[i]] might be defined but the line from next[i] is invalid, so the segment i..(next^2(i)-1) would include words up to some point after the bad word? That wouldn't be valid because the line starting at next[i] cannot be formed. So we must ensure that the segment we consider does not include any bad words and also that the greedy packing can actually place the lines. The condition for packability is: all words in the segment are good, and the greedy packing (or any packing) uses lines that each fit. The greedy packing from i will produce a sequence of line starts s0=i, s1=next[s0], s2=next[s1], ... until we exceed j. For the packing to be valid, each line from s_k must be able to fit words from s_k to s_{k+1}-1. If any s_k is bad, then the line from s_k cannot be formed, because the first word alone fails. So we need that all s_k for k from 0 to (number of lines used - 1) are good. Since s0 = i is good by assumption, we need s1, s2, ... also good. But if next[i] points to a bad index, then s1 is bad, meaning that after the first line, we cannot start a second line, so we cannot cover any word beyond s1-1. So the maximum j we can cover with at most r lines for r>=1 is at most s1-1. That is exactly j = next[i]-1. For r=1, it's fine because we don't need a second line. For r>=2, we cannot cover more than that. So our formula j_max(i) = next^r(i)-1 would be incorrect if next[i] is bad and r>=2, because next^r(i) might be computed assuming we can continue from bad start (if we define next[bad] incorrectly). So we must handle this.
Thus the clean approach: Split the text into blocks of consecutive good words (len ≤ c). For each block, we ignore bad words entirely; they act as barriers. Within a block, all words are good, and the next pointers will always point to an index within the block or to block_end+1 (which could be a bad word or n+1). But note: next[i] for i in the block may point to block_end+1 (i.e., the first index after the block). That's fine because that index is not a good word, but it's not used as a start if we only consider lines within the block. However, if we have r >= number of lines used to cover the block, we may need to consider if we can cover the entire block. That's fine; if next[i] points to block_end+1, that means after covering some lines, we have exhausted the block, and the next line would start at a bad word or end of text. For packing the block, we don't need to start a line at that index. So for r lines, if we apply next repeatedly, we might eventually land on an index > block_end. That's okay because we don't need to form a line from that index; we just stop. So it's valid.
Thus, we can process each block independently, assuming we only consider segments fully inside the block. Then j_max(i) computed as min(block_end, f(i)-1) where f(i) = result after r jumps, but if f(i) exceeds block_end+1, we cap at block_end. Actually if f(i) > block_end+1, that means after r jumps we are beyond the block, but the covered segment would be up to block_end. So we can set j_max(i) = block_end if f(i) > block_end+1 else f(i)-1.
But careful: When we compute jumps using binary lifting, we need next values for indices within the block and for the sentinel at block_end+1? Actually our next array is defined for indices 1..n+1. For indices beyond n, we set next[n+1] = n+1. For indices that are at block_end+1 (which may be a bad word or n+1), we also have a next value. But if block_end+1 is a bad word (len > c), then next[block_end+1] would be something? But we won't use it as a start from within the block because we only start from good indices. However, when we apply jumps, if we jump from a good index and after some steps we land on block_end+1, the next jump from there would be defined. But if block_end+1 is a bad word, then is next[block_end+1] valid? In our definition, if len[block_end+1] > c, then the line starting at block_end+1 cannot fit even one word, so the greedy algorithm would not be able to proceed. But if we are computing f(i) as the index after r lines, and we land on a bad index before completing r lines, that would correspond to the situation where we cannot form the next line, so we cannot cover more words. So for the purpose of covering words up to block_end, we should not consider that we can continue beyond block_end. However, if we land on a bad index after k lines (k < r), that means that we cannot form the (k+1)th line, so the maximal segment we can cover is only up to the previous line's end. So if we compute f(i) by applying next r times ignoring the fact that some next values might be invalid (i.e., starting a line at a bad word is impossible), we might overestimate the reachable index. For example, suppose we have a block of good words, and after the block there is a bad word. For a starting i near the end of the block, after one line we might land on next[i] = block_end+1 (the bad word). If r>=2, then applying next again from that bad word would yield something else (maybe block_end+2 if we defined next for bad words). But in reality, we cannot form the second line because we cannot start at the bad word. So the reachable j with ≤2 lines is still block_end (since after the first line we have covered up to block_end, and we cannot cover any more because next line can't start). But if we apply next twice, we might get an index beyond block_end, say next[block_end+1] = block_end+2? Then f(i) = block_end+2, and j = f(i)-1 = block_end+1, which includes the bad word, which is invalid. So we need to ensure that our jump computation stops when we hit an index where a line cannot be started (i.e., a bad index). To handle this correctly, we can define next[i] for bad i as i (or some sentinel) to indicate that you cannot progress, but then next[i] = i would cause infinite loops? Or we can define next[i] = i (so applying next doesn't advance). That would make f(i) possibly i, causing j = i-1, which is too pessimistic. Actually, if we are at a bad index, we cannot start a line, so we cannot cover any words from that index onward. So for the purpose of covering words, the greedy process cannot continue. So when we compute the maximum j reachable from i with ≤ r lines, we should consider that if at any point we encounter a bad start, we cannot cover any more words beyond the previous line's end. Therefore, the effective reach after t lines is the minimum over steps of something.
Simpler: Instead of trying to handle bad indices globally, we can split into blocks and only consider starts within a block, and we limit our computation of next jumps to stay within the block. That is, when we apply binary lifting, we consider that the next operation from an index inside the block yields the next line start, which may be inside the block or may be exactly block_end+1 (the first index after the block). But if it's block_end+1, then that index is not in the block and we should not continue further because any subsequent line would start outside the block, meaning no more words to cover. So for r lines, if we ever reach block_end+1 before using all r jumps, then further jumps do not change the position (since there are no words left). So we can treat next[block_end+1] = block_end+1. So we need to define next for the sentinel at block_end+1 as itself. But we don't know block boundaries in advance? We can define next for all indices, but we can modify the binary lifting to stop when we exceed n or when we hit an index with no words? Actually we can define next for all i as the computed next line start using the inequality, but for i where len[i] > c, the maximal e from i is i-1 (since condition fails for e=i). Then next[i] would be i? That doesn't make sense. Better to compute next only for good indices, and for others we treat them as barriers. For binary lifting, we can precompute up[k][i] for i from 1 to n+1, but we need to define next[i] for all i. A clean approach: We will compute "good" array, and we will compute next[i] for i=1..n as follows:
- If len[i] > c, set next[i] = i (or some special value indicating dead end). But then when we apply jumps from a good i, we might jump to a bad i. For further jumps, we need to know that from a bad i, no progress can be made; i.e., next[i] = i (so applying next stays at i). Then next^r(i) could be i if we ever hit a bad i. That would yield j = i-1, which is not correct because we might have covered some words before the bad i. Actually, consider we start at good i, we compute next[i] = j (which may be bad). The greedy packing's first line covers words i..j-1. Then we attempt to start second line at j (bad). Since we cannot form a line starting at j, we cannot cover any more words. So the maximum reachable index with ≥2 lines is still j-1. So if we apply next twice, the result should be something representing that we cannot go beyond j. If we set next[bad] = bad, then next^2(i) = next[bad] = bad. Then j = bad-1. That is actually correct: j-1 = (bad)-1, which is exactly the last word we could cover (i.e., up to before the bad word). And if we have more lines, we still can't go further, so j stays bad-1. So defining next[bad] = bad seems to work: because from a bad index, you cannot start a line, so you remain stuck there. Then applying next multiple times yields bad. Then j = bad-1 is the last word covered before the bad word. But careful: If we start at good i, next[i] = bad, then next^2(i) = bad. So j = bad-1. That's correct: with 2 lines we cannot go beyond bad-1 because the second line cannot start. With 3 lines also j = bad-1. So that matches the behavior: For r >= 2, the max j is bad-1.
But we must also consider the case where next[i] = n+1 (end). That's fine: next[n+1] = n+1 (self-loop). Then j = n+1-1 = n.
Thus we can define next[i] for all i from 1 to n+1 as:
- For i from 1 to n: if len[i] > c, then next[i] = i (or maybe i? but then line would cover zero words? Actually, if we start at bad i, we cannot even cover word i. So the greedy packing would not be able to cover word i. For our purposes, if we ever start at a bad i, the covered segment from that point onward is empty. So next[i] should be i (meaning the "start of next line" is still i? That would cause infinite loop). But maybe better: define next[i] = i+1? That would incorrectly suggest we can skip the bad word? No, we cannot include the bad word. So we must not allow starting at bad i. In the binary lifting, we should never start from a bad i. However, we might land on a bad i via jumps from good i. When we land on a bad i, that means we are trying to start a line there, which is impossible; thus we cannot cover more words. So from that point, the process stops; the position remains at that bad index? Actually, after we have covered up to bad-1, the next line would start at bad, but cannot cover any word. So the "start of next line" is still bad, because we haven't advanced. If we try to form a line from bad, we cannot place any word, so the line would be empty? But the problem requires each line to contain words? It doesn't explicitly forbid empty lines? The banner lines can be empty, but in the context of packing words, we are trying to place words; if we have an empty line, we could just not use it. But can we skip the bad word and start later? The greedy algorithm, as defined, when starting at bad, since the word cannot fit, you cannot include it. Could you skip it and start at the next word? That would mean you omit the bad word, but our segment includes the bad word? No, we are only considering segments that consist of consecutive words from the original text. If we land on a bad word, that means the previous line ended right before it, and the segment we are trying to cover includes that bad word? Actually, when we compute j_max(i) for a segment starting at i, we consider segments that include