We need to solve: given numbers 1..n, partition into two non-empty subsets such that the absolute difference of their sums is minimized. This is essentially a partition problem, but with the set {1,2,...,n}. The total sum S = n(n+1)/2. We want to find a subset with sum as close as possible to S/2, but both subsets must be non-empty. Since the total is S, if we find subset with sum = floor(S/2) or ceil(S/2) and the complement is non-empty automatically if subset is non-empty and proper? Actually both subsets must be non-empty, so we must avoid taking all elements. But if we take a subset that is not all elements, its complement is non-empty. So any proper subset is allowed. The empty set is not allowed because first group must be non-empty. Also we cannot take all elements because second group would be empty. So we need a subset of {1..n}, not empty and not all, with sum as close to S/2 as possible. Then the difference = |S - 2*sum|.
So the problem reduces to: find subset of {1..n} (non-empty, not all) with sum as close to S/2 as possible.
We need to output the minimum absolute difference, then the size and elements of one group (say the first group). The group we output can be either the subset we found or its complement? Both are non-empty. But we must output a group. Usually we can output the smaller subset or the first subset, but it says "the size of the first group, followed by the integers in that group." So we need to output the group we choose as first group. It can be either. Usually we output the subset we find (or its complement) whichever is convenient, as long as both are non-empty.
Now n up to 60000, S up to 6000060001/2 ≈ 1.8e9, which is about 1.8 billion. That's large, but we can't do DP of O(nS). We need a constructive solution. Since the numbers are consecutive integers, there is known approach: For any n, we can achieve difference either 0 or 1? Actually the parity matters: S = n(n+1)/2. If S is even, can we partition into two equal sum subsets? That is possible if and only if n mod 4 = 0 or 3. Known fact: The set {1,...,n} can be partitioned into two subsets of equal sum iff n(n+1)/2 is even and n mod 4 != 1,2? Actually condition: n mod 4 = 0 or 3. Because sum S even when n mod 4 = 0 or 3? Let's check: n=4 => S=10 even, possible. n=3 => S=6 even, possible? {1,2,3} can be partitioned? Sum=6, half=3, subsets: {1,2} sum 3, {3} sum3, works. n=2 => S=3 odd, cannot equal. n=1 trivial. n=5 => S=15 odd, cannot equal. n=6 => S=21 odd. n=7 => S=28 even, can it? n=7 mod4=3, yes possible. So indeed when S even (i.e., n mod 4 = 0 or 3) we can get difference 0. When S odd, the minimum difference is 1 because two integer sums differ by at least 1.
But is it always possible to achieve difference 0 when n mod 4 = 0 or 3? Yes, there is known constructive algorithm. Also, if n mod 4 = 1 or 2, then difference is 1, and we can achieve that. So the answer is known: minimal difference is 0 if n mod 4 = 0 or 3, else 1. But is it always possible to achieve difference 1 for n mod 4 = 1 or 2? For n=2, we have 1. For n=1 (but n>=2). n=5: S=15, half=7.5, min diff is 1. Can we get a subset sum =7 or 8? 7? possible? {1,2,4} sum=7, complement sum=8 => diff=1. Yes. n=6: S=21, half=10.5, we need subset sum=10 or 11. Can we get 10? {1,2,3,4} sum=10, but that's all except 5,6? Actually {1,2,3,4}=10, complement {5,6}=11 diff=1. So yes. n=9: S=45 odd, diff=1? {1,2,3,4,5,6,7,8,9} sum=45, half=22.5. Can we get 22? {1,2,4,5,10? no 10 not exist. But we can find? Known result: the set {1,...,n} can always be partitioned into two subsets with difference at most 1, and the exact difference as above. In fact, there are constructive algorithms.
Thus we can output the minimal difference easily: d = 0 if n%4==0 or n%4==3 else d=1.
Now we also need to output one group (size and elements). So we need to actually construct a subset achieving that difference. So we need to output a subset of {1..n} (non-empty, not all) with sum = floor(S/2) or ceil(S/2). Specifically, if d=0, we need a subset sum = S/2. If d=1, we need a subset sum = floor(S/2) (or ceil). Either is fine; we can output that subset or its complement. But we must ensure both groups are non-empty; the subset we output must be non-empty and not all.
We need constructive algorithm for n up to 60000. There are known patterns:
Case 1: n mod 4 == 0
Then we can partition as: take indices i such that i mod 4 = 0 or 1? Actually there is standard approach: For n divisible by 4, we can pair numbers (1,n), (2,n-1), ... Each pair sums to n+1. In each pair, we assign one number to set A and the other to set B such that each set gets half the sum. One method: For each k from 1 to n/4, put k and n-k+1? Let's recall: For n=4: pairs (1,4) sum5, (2,3) sum5. One set takes {1,4}, other {2,3}. For n=8: pairs (1,8), (2,7), (3,6), (4,5). I think the rule: In each pair (i, n+1-i), put i if i is odd? Not exactly. Let's derive: For n divisible by 4, we can assign as: For i from 1 to n/2, if i is odd, put i in set A? But need to consider that total sum of A equals S/2. Known algorithm: If n % 4 == 0, then one partition is: {1,4,5,8,9,12,...}? Actually pattern: For n=4: {1,4} works. For n=8: need subset sum = 36/2=18? Wait S=8*9/2=36, half=18. Which subset sum 18? Could be {1,2,7,8}? sum=18? 1+2+7+8=18. That is not simple pattern. But known constructive algorithm from "Partition problem for consecutive integers" uses: If n % 4 == 0, then take numbers: 1,2,...,n/4, and also numbers from 3n/4+1 to n? Not sure.
Alternatively, there is a simple greedy/backtracking? But n=60000, we need O(n) construction.
Better to recall known approach: We can generate a subset by starting from the largest number and decide whether to include it based on the remaining target. Since the numbers are consecutive, we can use a greedy algorithm to achieve exact half sum when possible: Starting from n down to 1, if current number <= target, include it and subtract from target. Since total sum S is exactly sum of all numbers, and we are trying to reach S/2, this greedy algorithm (taking largest available) often works? Let's test: For n=4, S/2=5, target=5: start at 4: 4<=5 include, target=1; next 3: 3>1 skip; next 2: 2>1 skip; next 1: 1<=1 include, target=0 -> subset {4,1} sum=5. Works. n=3: S/2=3, start 3: 3<=3 include, target=0, subset {3} sum=3, complement {1,2} sum=3. Works. n=7: S=28, half=14, target=14: start 7: 7<=14 include, target=7; 6:6<=7 include, target=1; 5:5>1 skip; 4:4>1 skip; 3:3>1 skip; 2:2>1 skip; 1:1<=1 include, target=0 => subset {7,6,1} sum=14. Works. n=8: half=18, target=18: 8 include, target=10; 7 include (7<=10), target=3; 6>3 skip;5>3 skip;4>3 skip;3<=3 include, target=0 => subset {8,7,3} sum=18? 8+7+3=18. That works. So greedy from largest down to smallest selecting if number <= target seems to produce a subset that sums exactly to target? Does it always work when a subset sum equal to target exists? In general, for any set of numbers (especially powers of two?), greedy may fail. But for the set {1,2,...,n} and target = floor(S/2) or S/2 when S even, is this greedy always successful? Let's test n=5: S=15, half floor =7 (or 8). Suppose we try to get 7: target=7: start 5<=7 include, target=2; 4>2 skip; 3>2 skip;2<=2 include, target=0 => subset {5,2} sum=7 works. For 8: target=8: start 5 include, target=3; 4>3 skip;3<=3 include, target=0 => {5,3}=8 works. n=6: target floor=10? Actually S=21, half floor=10: target=10: start 6 include, target=4;5>4 skip;4<=4 include, target=0 => {6,4}=10 works. For 11: target=11: start 6 include target=5;5<=5 include target=0 => {6,5}=11 works. n=9: S=45, half floor=22: target=22: start 9 include target=13; 8 include target=5; 7>5 skip;6>5 skip;5<=5 include target=0 => {9,8,5}=22 works. n=10: S=55, half floor=27? 55/2=27.5 so floor=27, target=27: 10 include target=17;9 include target=8;8<=8 include target=0 => {10,9,8}=27? 10+9+8=27 works. For 28: target=28: 10 include target=18;9 include target=9;8 include? 8<=9 include target=1;7>1 skip;...1 include => {10,9,8,1}=28? 10+9+8+1=28 works. So seems greedy works for these consecutive numbers for the half-sum (or floor/ceil) when subset exists? Let's test n=12: S=78, half=39 (even), target=39: start 12 include target=27;11 include target=16;10 include target=6;9>6 skip;8>6 skip;7>6 skip;6<=6 include target=0 => {12,11,10,6}=39? 12+11+10+6=39 works. n=13: S=91 odd, floor=45 (or 46). target 45: 13 include target=32;12 include target=20;11 include target=9;10>9 skip;9<=9 include target=0 => {13,12,11,9}=45? sum=45 works. n=14: S=105, floor=52, target=52: 14 include target=38;13 include target=25;12 include target=13;11 include? 11<=13 include target=2;10>2 skip;...2<=2 include target=0 => {14,13,12,11,2}=52? 14+13+12+11+2=52, yes. n=15: S=120 even, half=60, target=60: 15 include target=45;14 include target=31;13 include target=18;12 include target=6;11>6 skip;10>6 skip;9>6 skip;8>6 skip;7>6 skip;6<=6 include target=0 => {15,14,13,12,6}=60? 15+14+13+12+6 = 60 indeed. n=16: S=136 even? 16*17/2=136, half=68, target=68: 16 include target=52;15 include target=37;14 include target=23;13 include target=10;12>10 skip;11>10 skip;10<=10 include target=0 => {16,15,14,13,10}=68? 16+15+14+13+10=68. Works. n=17: S=153 odd, floor=76, target=76: 17 include target=59;16 include target=43;15 include target=28;14 include target=14;13 include? 13<=14 include target=1;12>1 skip;...1 include => {17,16,15,14,13,1}=76? sum=17+16+15+14+13+1=76, yes.
So the greedy algorithm of picking the largest number not exceeding the remaining target seems to always yield a subset with sum exactly equal to target, where target = floor(S/2) or S/2? It appears to work for all n up to tested values. But is it proven? Since the numbers are 1..n, this is essentially the "canonical coin system" where greedy works for any amount if coins are canonical? But not all coin systems have greedy property for all amounts. However, with denominations 1,2,...,n, it's known that the greedy algorithm for making change works for any target up to total sum? Actually this is the problem of representing a number as sum of distinct integers from 1..n (each at most once). The greedy algorithm: always take the largest number ≤ remaining target. This is essentially constructing a representation using the "binary" like? But it's not binary; however, note that if we have numbers 1..n, then any integer from 0 to S can be represented as sum of distinct numbers from 1..n? Not exactly; but many can. For the specific target floor(S/2), we need to ensure it can be represented. It is known that the set {1,2,...,n} can represent any integer from 0 to S? Actually it can represent any integer from 0 to S because it's the set of all numbers; but is that true? Consider S=2: numbers {1,2} can represent 0,1,2,3? Actually total is 3, but we can't represent 3? With subset sum, we can represent 3 by {1,2} yes. For S=3: numbers {1,2,3} total=6. Can we represent all numbers 0..6? 0 yes,1,2,3,4? 4 can be 1+3,5=2+3,6=1+2+3. So yes all. In general, since we have 1, we can build any integer from 0 to S by a simple greedy algorithm? Actually the set of numbers 1..n is complete in the sense that it's the set of all integers from 1 to n. This set can represent any integer from 0 to S: this is known as the "subset sum of first n integers" being all integers from 0 to S? Is that true? Let's test n=4: S=10. Can we represent 8? Yes {4,3,1}=8, {4,2,?}. 8 possible. 9? {4,3,2}=9, yes. 7? {4,2,1}=7, yes. 6? {4,2}=6 etc. So yes. n=5: S=15. Can we represent 14? {5,4,3,2}=14, yes. 13? {5,4,3,1}=13, yes. 12? {5,4,3}=12, yes. 11? {5,4,2}=11, yes. 10? {5,3,2}=10, yes. 9? {5,3,1}=9, yes. 8? {5,2,1}=8, yes. 7? {5,2}=7? Actually 5+2=7, yes. 6? {5,1}=6, yes. So seems all numbers up to 15 can be represented. But is there any number that cannot? For n=1: S=1, can represent 0,1. For n=2: S=3, can represent 0,1,2,3. For n=3: 0..6 all possible. In general, by induction, adding a new number n+1 to the set {1..n} which can represent all numbers from 0 to S_n, then with new number n+1, we can represent all numbers from n+1 to n+1+S_n = S_{n+1}, and also all numbers from 0 to S_n (by not using n+1). Since n+1 <= S_n+1? Actually S_n = n(n+1)/2. For n>=1, n+1 <= S_n+1? Let's check: n=1, S1=1, n+1=2, 2 <= 2? Yes. n=2, S2=3, n+1=3 <= 4? 3 <= 4, yes. In general, S_n = n(n+1)/2. Compare n+1 and S_n+1: S_n+1 = n(n+1)/2 +1. For n>=1, n(n+1)/2 >= n+1 when n>=2? Actually for n=2: 3>=3 yes. For n>=2, n(n+1)/2 >= n+1 because dividing: (n/2) >=1 for n>=2. So indeed n+1 <= S_n+1? Actually we need the condition for the intervals to cover all numbers from 0 to S_{n+1} without gaps: We have representation for numbers 0..S_n from old set. Adding number n+1, we can represent numbers n+1.. S_n + n+1 = S_{n+1}. The gap would be if there is a number between S_n+1 and n+1-1? Actually we need that every integer between 0 and S_{n+1} is representable. The new numbers we can represent are those of form (n+1)+x where x representable by old set (0..S_n). So these are numbers from n+1 to n+1+S_n = S_{n+1}. The old set gives numbers 0..S_n. To have all numbers from 0..S_{n+1} covered, we need that S_n >= n (or more precisely that there is no gap between S_n and n+1). The gap would be if there is an integer > S_n and < n+1 that is not covered. That would require n+1 > S_n+1? Actually if n+1 > S_n+1, then numbers S_n+1,..., n would be missing. But is that possible? Let's compute n+1 - (S_n+1) = n+1 - (n(n+1)/2 +1) = n+1 - n(n+1)/2 -1 = n - n(n+1)/2 = n(1 - (n+1)/2) = n*( (2 - n -1)/2) = n*( (1-n)/2 ) = -n(n-1)/2. That is negative for n>1. So n+1 <= S_n+1 for n>=1? Actually equality at n=1: n+1=2, S_n+1=2, yes. For n=2: n+1=3, S_n+1=4, so 3<=4. So indeed n+1 <= S_n+1. Hence the intervals [0, S_n] and [n+1, S_{n+1}] cover all numbers from 0 to S_{n+1} because S_n >= n+1-1? Actually the union covers 0..S_n and n+1..S_{n+1}. Since n+1 <= S_n+1, there is possibly a gap at S_n+1 if n+1 > S_n+1? But we have n+1 <= S_n+1, so the two intervals overlap or touch? For n=2: [0,3] and [3,6] overlap at 3. For n=3: S3=6, n+1=4, so intervals [0,6] and [4,10] overlap from 4 to 6. So indeed all numbers are covered. By induction, all integers from 0 to S_n are representable. So the greedy algorithm that picks the largest number not exceeding the target will always find a subset because the target is within [0, S_n] and such a representation exists. But does the greedy algorithm always succeed? For the coin change problem with canonical coin systems, greedy works for all amounts if the coin system is "canonical" and sorted decreasingly. However, the coin system 1,2,3,... is not a canonical coin system in the usual sense because coins are not multiples of each other, but there is a known property: For the set of numbers {1,2,...,n}, the greedy algorithm that always picks the largest number not exceeding the remaining sum yields a representation of any integer up to S_n. This can be proven by induction as well. Because if we pick the largest k ≤ target, then we need to represent target - k with numbers from 1..k-1. Since target ≤ S_n, we have target - k ≤ S_{k-1}? Not exactly. But consider that the maximum sum we can form from numbers less than k is S_{k-1} = (k-1)k/2. Since k is the largest ≤ target, we have target ≤ (S_n)? But we need to ensure target - k ≤ S_{k-1}. Since we know that for any target, there exists a representation. But does greedy always find it? I think yes: because the set of numbers are all integers, and we have a "complete" set. Let's attempt to prove: Suppose we want to represent T, 0≤T≤S_n. Let k = min(n, T) but actually we choose the largest number m such that m ≤ T. Then we claim that T - m ≤ S_{m-1}. Because if T - m > S_{m-1}, then T > m + S_{m-1} = m + (m-1)m/2 = m(m+1)/2? Actually S_{m-1} = (m-1)m/2, so m + S_{m-1} = m + (m-1)m/2 = m(1 + (m-1)/2) = m( (2 + m -1)/2 ) = m(m+1)/2 = S_m. So T > S_m. But since T ≤ S_n and m ≤ n, we have S_m ≤ S_n, but it's possible that T > S_m? Yes, if T is larger than S_m, then T could be > S_m. But if T > S_m, then m is not the largest number ≤ T? Because we could have taken a larger number? Actually we defined m as the largest number ≤ T. But if T > S_m, then there might be a number larger than m that is ≤ T? Since numbers are consecutive up to n, the next number is m+1. If m+1 ≤ T, then we would have chosen m+1 as the largest ≤ T. So m must satisfy that m+1 > T, i.e., m+1 > T. So T < m+1. Then T ≤ m. So actually m = T? Because the largest integer ≤ T is floor(T). If T is not integer? But T is integer. So the largest number ≤ T is T if T ≤ n, else n. So m = min(T, n). Actually careful: we have numbers 1,2,...,n. For an integer target T, the largest number in the set that is ≤ T is min(n, T). Because if T ≤ n, then T itself is in the set, so the largest is T. If T > n, then n is the largest. So m = min(n, T). But we also consider that we might not want to pick T if T > n? Actually in the greedy algorithm we start from n downwards and pick if n ≤ target. So we pick the largest number not exceeding target, which is indeed min(n, target) if target>=1. But is that always optimal? Let's test: T=4, n=3. Then the largest number ≤ 4 is 3. So m=3. Then T-m=1, and S_{m-1}=S_2=3. 1≤3, okay. But if T=5, n=3: m=3, T-m=2≤S_2=3. So it's okay. In general, if T ≤ n, then m=T, T-m=0, trivial. So the only interesting case is when T > n. Then m=n. Then we need to represent T - n using numbers 1..n-1. For the greedy to succeed, we need T - n ≤ S_{n-1}. But T ≤ S_n, so T - n ≤ S_n - n = S_{n-1}. Yes because S_n = n + S_{n-1}. So T - n ≤ S_{n-1}. So by induction, greedy works. Thus greedy algorithm always yields a subset with sum exactly equal to any target between 0 and S_n inclusive. But we also need to ensure that the subset we get does not include all numbers? It could possibly be the whole set if target = S_n? But we are targeting floor(S/2) or S/2, which is less than S_n unless n=0 or 1? For n>=2, S/2 < S_n. So the subset will be a proper subset, non-empty? Could it be empty? target=0 would produce empty set, but we never target 0 because S/2>0 for n>=2. So we will get a non-empty subset, and its complement is also non-empty because the total sum is S and subset sum < S. So both non-empty. So greedy works for any n.
Thus we can simply compute target = S // 2 if we only care about difference? Actually we want minimal absolute difference. So we compute d = S % 2 (i.e., 0 if S even else 1). But we also need to output the group. We can compute target = S // 2 (floor). Then we use greedy to find a subset with sum = target. This subset will have sum = floor(S/2). Then the other subset sum = S - floor(S/2) = ceil(S/2). So the absolute difference = S - 2*target = S%2 = 0 or 1. Indeed if S even, target = S/2 exactly. If S odd, target = floor(S/2) and difference =1.
But note: In case S odd, we could also aim for ceil(S/2) as target, that would also give difference 1. Either works. Greedy will also work for ceil? For S odd, ceil = floor+1. We need to find subset sum = ceil? Greedy may find floor, but could it find ceil? Possibly by slight modification? But we can just use floor; that's fine because the other group sum will be ceil, so the difference is still 1. However, which group do we output? We can output the subset we constructed (sum floor) or its complement (sum ceil). Both give difference 1. The problem statement: "print the smallest possible absolute difference in the first line." So we output 0 or 1 accordingly. Then "In the second line print the size of the first group, followed by the integers in that group." So we need to output some first group that achieves that difference. So we can output the subset we constructed (which has sum floor) if we want. But is that valid? The absolute difference of sums of the two groups is |S - 2*sum|. If S even, sum = S/2, diff=0. If S odd, sum = floor(S/2), diff=1. Yes. So that's valid.
But we must ensure that both groups are non-empty. In case of odd S, could it happen that the subset we get by greedy for target floor(S/2) is the whole set except one element? That would be non-empty and complement non-empty. If target floor(S/2) leads to the subset being all elements? That would be sum = S, but target = floor(S/2) < S, so no. Could it be empty? No, target >0 for n≥2. So fine.
Thus algorithm:
- Compute S = n*(n+1)//2.
- Compute target = S // 2.
- Run greedy: start from i = n down to 1: if i <= target: take i, target -= i.
- The taken numbers form a subset A.
- The complement B = rest.
- Output diff = S - 2* (sum of A) = (S % 2).
Actually sumA = original S//2 - final target? But we can compute sumA as we build list, keep running sum.
- Output size |A| and the elements in A.
But wait: In case S even and n mod 4 = 0 or 3, we already know diff=0. But does greedy always produce subset sum = S/2? According to above, yes for any target ≤ S, so yes. So it's fine.
Check edge cases: n=2, S=3, target=1. Greedy: start i=2: 2 <=1? No. i=1: 1<=1 take, target=0. A={1}. sum=1, diff=3-2=1, correct. Output group: size 1, element 1. That matches example? Example output for input 2: "1\n1 1". Actually they output "1" as diff and "1 1" meaning first group of size 1 and integer 1. So yes matches.
n=4, S=10, target=5. Greedy: i=4<=5 take, target=1; i=3>1 skip; i=2>1 skip; i=1<=1 take, target=0. A={4,1}. Output size 2 and elements 1 4 (order arbitrary). Example output: "0\n2 1 4". Yes matches.
Thus greedy works for examples.
But is greedy always guaranteed to produce a subset that does not contain all numbers? For n=1 (not allowed). For n=2, ok. For n=3, S=6, target=3: greedy: i=3 take, target=0, A={3}. Complement {1,2} diff=0. works. For n=5, target=7: greedy: i=5 take target=2; i=4>2 skip; i=3>2 skip; i=2<=2 take target=0; A={5,2}. Sum=7 diff=1. Complement {1,3,4} sum=8 diff=1. Both non-empty.
Thus solution is straightforward.
Time complexity O(n). Memory O(n) to store subset list, but we can output directly? We need to output size first, then the numbers. So we need to store the chosen numbers, or we can compute on the fly but need to output size first. Could compute the subset list and then print size and numbers. n up to 60000, so storing list of up to n numbers (max about 30000 maybe) is fine.
Now we need to be careful: if the subset we construct is of size 0? Not possible as target>0. Could it be size n? That would happen only if target = S, which is not. So fine.
But there is nuance: The problem statement: "He wants to split these integers in two non-empty groups". So the groups must both be non-empty. Our greedy yields a subset A with sum target. It's possible that A is the entire set? Only if target = S, but target is S//2, for n>=2, S//2 < S, so not the whole set. Could A be empty? Only if target = 0, which does not happen for n>=2 because S//2 >=1. So both groups non-empty. However, what if the greedy algorithm for some n and target yields A = complement? Actually both are non-empty. So fine.
Thus the solution is simple.
But wait: The greedy algorithm we described picks the largest numbers first. For n large (60000), it's O(n). That's acceptable.
Now confirm that for n mod 4 = 0 or 3, diff=0, else diff=1. But we don't even need to compute that separately; we can just compute diff = S % 2, as we get from the greedy result. However, we must ensure that for S even, greedy produces sum exactly S/2. According to our reasoning, it should. But is there any case where greedy fails to produce exactly S/2? Since S/2 is an integer and is between 0 and S, and we argued that any integer up to S can be represented as sum of distinct numbers from 1..n, greedy should find it. But is greedy guaranteed to find a representation for every integer? While the set can represent all numbers, greedy is a specific algorithm. For coin systems, greedy may fail to find a representation even if one exists. Example: coins {1,3,4} and target 6. Greedy picks 4, then 2 left, cannot pick 3 or 1? Actually after 4, target=2, largest <=2 is 1, then target=1, pick 1, target=0 => {4,1,1} but cannot reuse 1? In subset sum each number can be used at most once. So with coins {1,3,4} and each coin can be used at most once, the representation {4,1,?} we need to pick distinct numbers. After picking 4, target=2, we have remaining numbers {1,3}. 3 is >2, so only 1 can be taken, target=1. Then we have remaining numbers {? after using 1, we have 3 left, but 3>1}. So we end with target=1 not 0. So greedy fails to find {3,? Actually 3+? 3+? need 3+?=6 -> 3+?=6, 3+3 not allowed, 3+1+?=6? 3+1+?=6 -> need 2, not available. Actually set {1,3,4} can represent 6? 1+? 1+?=6, 1+? 5 not possible. 3+4=7>6. 4+1=5. So 6 not representable, so not relevant. For a set that can represent all numbers, does greedy always succeed? Consider the set {1,2,5}. Can it represent all numbers up to total 8? Total =8, but can we represent 4? Greedy: target=4, largest <=4 is 2? Actually numbers sorted descending: 5,2,1. 5 >4 skip, 2 <=4 take, target=2, then next 2? Actually after taking 2, we consider next numbers: 1? but we need to consider remaining numbers {1}. So take 1, target=1, then no number left? Actually we still have 1? Already used? No we take 1, target=0? Wait we took 2, then target=2; next largest is 1, 1 <=2 take, target=1; next? no more numbers left? Actually set had 5,2,1. We used 2 and 1, leaving 5 not used. Target now 1, but no number <=1 left (1 already used). So greedy fails to find {??} But 4 is representable? {?} Can we make 4 from {1,2,5}? 2+1+?1? need 4, 2+1=3, need 1 more but only one 1 available. Could use 5? no. So 4 is not representable. So it's okay that greedy fails because no representation exists. But if representation exists, does greedy always find it? For some coin systems, greedy can fail even when a representation exists if coins are not canonical. For example, with unlimited coins, US coins are canonical, but some are not. For subset sum with distinct coins, is there a set where some amount can be formed but greedy fails? Consider set {1,6,7}. Total =14. Can we represent 8? Greedy: start 7<=8 take, target=1; next 6>1 skip; then 1 take, target=0 => {7,1}=8 works. That works. Consider set {1,4,5}. Total=10. Represent 8? Greedy: 5 take, target=3; 4>3 skip; 1 take, target=2; then next 1? But only one 1, so target=2 fails. But is there representation? 4+? 4+?=8 -> 4+4 not allowed, 4+1+?=8 -> need 3, not available. 5+?=8 need 3, not available. 1+4+5=10 >8. So 8 not representable. Okay. Consider set {1,3,4,5}. Total=13. Can we represent 2? Greedy: 5>2 skip;4>2 skip;3>2 skip;1 take target=1; then 1? but only one 1, so fails. But 2 is not representable? Actually 1+? need another 1. So not. So seems greedy might be safe for sets that are "complete" like 1..n? I recall a known result: For the set {1,2,...,n}, the greedy algorithm (taking the largest possible element not exceeding remaining sum) always produces a subset with sum exactly the given target, for any target from 0 to S_n. Because the set has the property that the sum of all numbers less than the largest chosen is at least the largest chosen minus 1? Actually we need to prove that if we pick m (largest ≤ target), then the remainder target - m ≤ S_{m-1}. As argued earlier, if m = min(n, target). When target > n, m=n, remainder ≤ S_n - n = S_{n-1}. When target ≤ n, m = target, remainder=0. So that's the proof. So greedy works. Great.
Thus we can implement.
Implementation steps:
- Read n.
- Compute total = n*(n+1)//2.
- target = total // 2.
- Initialize empty list chosen.
- For i from n down to 1:
if i <= target:
chosen.append(i)
target -= i
(else skip)
- After loop, target should be 0.
- Compute sum_chosen = total//2 - target? Actually target becomes 0, so sum is total//2 (original target) if we started with target = total//2.
- diff = total - 2*sum_chosen = total % 2.
- Output diff.
- Output len(chosen) and then the elements in chosen (order arbitrary; we have descending, but we can output as is or ascending. They said arbitrary order.
- Ensure not to output trailing spaces? Usually okay.
Check n=60000: total = 6000060001/2 = 1800030000? Actually compute: 6000060001 = 3,600,060,000? Let's compute: 60,000 * 60,001 = 60,000*60,000 + 60,000 = 3,600,000,000 + 60,000 = 3,600,060,000. Divide by 2 = 1,800,030,000. So fits in 32-bit signed? 1.8e9 < 2^31-1 (2.147e9). So int32 okay. But in C++ one might use long long, but Python fine.
Now, verify greedy for large n: target ~0.9e9, loop from 60000 down to 1, each step check if i <= target. Since target decreases, it will include many numbers. Complexity O(n) fine.
But is it guaranteed that after loop target becomes 0? As argued yes. But we should verify with code maybe mentally for some n.
Edge Cases:
- n=2: target=1; loop: i=2: 2<=1? false; i=1: 1<=1 true, target=0; chosen=[1]; works.
- n=3: target=3; i=3 <=3 -> target=0; chosen=[3]; works.
- n=4: target=5; i=4 <=5 -> target=1; i=3 skip; i=2 skip; i=1 <=1 -> target=0; chosen=[4,1]; works.
- n=5: target=7; i=5 <=7 -> target=2; i=4>2; i=3>2; i=2<=2 -> target=0; i=1 skip; chosen=[5,2]; works.
- n=6: target=10; i=6 <=10 -> target=4; i=5>4; i=4<=4 -> target=0; chosen=[6,4]; works.
- n=7: target=14; i=7 <=14 -> target=7; i=6<=7 -> target=1; i=5>1; i=4>1; i=3>1; i=2>1; i=1<=1 -> target=0; chosen=[7,6,1]; works.
- n=8: target=18; i=8 <=18 -> target=10; i=7<=10 -> target=3; i=6>3; i=5>3; i=4>3; i=3<=3 -> target=0; chosen=[8,7,3]; works.
- n=9: target=22; i=9<=22 -> target=13; i=8<=13 -> target=5; i=7>5; i=6>5; i=5<=5 -> target=0; chosen=[9,8,5]; works.
- n=10: target=27; i=10<=27 -> target=17; i=9<=17 -> target=8; i=8<=8 -> target=0; chosen=[10,9,8]; works.
So all good.
Now what about outputting the group? The problem statement example for n=4 output "2 1 4". So they printed the size first, then the numbers separated by spaces. That matches.
Thus the solution is simple.
But wait: Could there be a scenario where the greedy algorithm yields a subset that, when we output it, its complement might be the one that is smaller or something? But that's irrelevant.
Now, we need to output the smallest possible absolute difference. Our construction yields diff = total%2. Is that indeed the smallest? Known results: yes. But we should double-check for n mod 4=0 or 3: diff=0. For n mod 4=1 or 2: diff=1. Since total%2 = 1 when n(n+1)/2 odd. When is total odd? n(n+1)/2 odd => n(n+1) even but product even always; we need to check parity: n mod 4 = 1 or 2 => total odd? Let's test: n=1: total=1 odd, n%4=1; n=2: total=3 odd, n%4=2; n=3: total=6 even, n%4=3; n=4: total=10 even, n%4=0; n=5: total=15 odd, n%4=1; n=6: total=21 odd, n%4=2; n=7: total=28 even, n%4=3; n=8: total=36 even, n%4=0. So indeed total odd iff n mod 4 = 1 or 2. So diff = total%2 matches the known result. So minimal difference is either 0 or 1.
Thus solution is correct.
But we must be careful: The problem expects to print the group. Could there be multiple optimal groups? Yes, any is fine.
Now potential pitfalls: For n large, the group size might be large; we need to output numbers separated by spaces. In Python, printing a list with join is fine.
Now we need to write code accordingly.
But what about the case n=0? Not allowed. n>=2.
One more check: For n=1 (if allowed) would be diff=1? But n>=2, fine.
Thus implementation in Python:
def solve():
import sys
n = int(sys.stdin.readline().strip())
total = n * (n + 1) // 2
target = total // 2
chosen = []
for i in range(n, 0, -1):
if i <= target:
chosen.append(i)
target -= i
# After loop, target should be 0.
# But due to integer arithmetic, it's possible that target not zero if something wrong? But theoretically correct.
# Compute actual sum of chosen (optional)
sum_chosen = total // 2 - target # since we subtracted.
diff = total - 2 * sum_chosen # equivalently total % 2.
# Actually diff = total % 2 if target started as total//2, but we can compute directly.
print(diff)
print(len(chosen), end='')
if chosen:
print(' ' + ' '.join(map(str, chosen)))
else:
print() # but not possible.
But note: The output format: second line: first the size of the first group, then the integers. So we need to output size and then numbers separated by spaces. So we can print as above: print(len(chosen), *chosen). That's simpler: print(len(chosen), *chosen). However, careful: If chosen list is empty, we would print "0" then nothing? But not possible. So fine.
Now one nuance: In the example for n=2, they printed "1 1". That is exactly print(len(chosen), *chosen) gives "1 1". Good.
Now we should consider that the greedy algorithm always yields a subset that may not be the one they expect, but it's acceptable.
But is there any scenario where the greedy algorithm yields a subset that is all numbers? Only if target = total, but target = total//2 < total for n>1. For n=1, target=0? Actually not needed.
Thus final answer.
But wait: we need to output the smallest possible absolute difference, but we computed diff from our constructed subset. It will be either 0 or 1. However, is it possible that the minimal absolute difference is even smaller than 1? No, because sums are integers, difference is integer, smallest possible positive difference is 1. So 0 is the only possible smaller than 1.
Thus all set.
Now I'll craft a thorough explanation for the solution.
But the problem might be from Codeforces: "Petya and Inequiations"? Actually no, it's "Split the set". The constraints 60000 suggest O(n) solution. The greedy method is fine.
Double-check for n=60000: total even or odd? n=60000 mod 4 = 0, so total even, diff=0, we can partition into equal sums. The greedy algorithm will produce a subset with sum exactly 1.8e9/2 = 900,015,000? Actually total/2 = 900,015,000? Let's compute: total = 1,800,030,000, half = 900,015,000. So it will pick many numbers. It will loop from 60000 down to 1. Since half is roughly 0.75 * total? Actually total = n(n+1)/2 ≈ (n^2)/2 = 1.8e9; half ≈ 0.9e9. The sum of numbers from 1 to n is about n^2/2, half is about n^2/4. The greedy picks large numbers first, likely picks numbers from n down to about n/2? Let's approximate: sum of numbers from k to n is about (n(n+1)/2 - (k-1)k/2). To reach half, we might pick about n/√2? Actually not needed.
Time complexity O(n) fine.
Now ensure memory: storing chosen list could be up to ~n/2 elements, which is at most 30,000 for n=60,000. That's fine.
But maybe we can further optimize by not storing but just generating and printing later? Need size first. So we need to store or compute size first. We could compute the subset and then print. Storing is fine.
Alternatively, we could compute the complement and maybe output the smaller group to reduce output size. But not necessary.
Now check correctness for n=3: our greedy yields subset {3}. Output: diff=0? Because total=6 even, target=3, subset sum=3, diff=0. So output "0\n1 3"? But is {3} and {1,2} acceptable? Yes, both non-empty. However, does the example provide any? Not for n=3 but it's fine.
Now finalize solution.
But before finalizing, we should double-check the condition that both groups must be non-empty. Could our greedy ever produce the whole set? Only if target = total, which is not. Could it produce empty? target>0 ensures at least one number taken because the loop will eventually hit i=1 when target>0? But is it possible that target never becomes zero because we skip all numbers? Suppose target=1, loop from n downwards: if n>1, we skip all numbers >1, then when i=1, we take it. So yes, at least one taken. So both groups non-empty.
Thus the solution is correct.
Now I'll produce final answer: code in Python.
But note: There is a known alternative constructive method for partition of 1..n into two equal sums when n%4==0 or 3. But our greedy method is simpler.
However, there is a potential catch: The greedy algorithm as described (starting from n and picking if i<=target) will always include n if target >= n. For many n, target is >= n (since total/2 is roughly n^2/4, which for n>3 is > n). So it will include n. That's fine. But does it ever fail to reach exact target? As argued, it should not.
Let's attempt to find a counterexample maybe for n=11? n=11, total=66, half=33. Greedy: i=11 <=33 take, target=22; i=10 <=22 take, target=12; i=9 <=12 take, target=3; i=8>3; i=7>3; i=6>3; i=5>3; i=4>3; i=3<=3 take, target=0; chosen {11,10,9,3} sum=33. Works.
n=12, we did.
n=13, we did.
n=14: total=105, half floor=52. Greedy: 14 take target=38;13 take target=25;12 take target=13;11 take? 11<=13 take target=2;10>2;9>2;...2 take? Actually after 11, target=2, then i=10,9,...,3>2, i=2<=2 take target=0. So {14,13,12,11,2} sum=52 works.
n=15: we did.
n=20: total=210, half=105. Greedy: 20 take target=85;19 take target=66;18 take target=48;17 take target=31;16 take target=15;15 take? 15<=15 take target=0; => {20,19,18,17,16,15} sum=105? 20+19+18+17+16+15=105, yes works.
n=21: total=231, half floor=115? Actually 231/2=115.5, floor=115. Greedy: 21 take target=94;20 take target=74;19 take target=55;18 take target=37;17 take target=20;16 take target=4;15>4 skip;14>4... down to 4 take? i=4<=4 take target=0; So chosen {21,20,19,18,17,16,4} sum? 21+20=41, +19=60, +18=78, +17=95, +16=111, +4=115, works.
Seems solid.
Now about time: O(n) is fine.
Thus final solution: read n, compute total, target = total // 2, gather chosen numbers by iterating from n down to 1, output diff = total % 2, output size and chosen.
But wait: In the output, they printed a space after the size? In example: "2 1 4". That is exactly "size" space then numbers. So using print(len(chosen), *chosen) will produce that.
Now, one more nuance: The problem statement: "Print the smallest possible absolute difference in the first line. In the second line print the size of the first group, followed by the integers in that group." It says "followed by the integers" meaning after the size, separated by spaces. So yes.
Now I'll produce final answer.
But before final, I should consider: Could there be a situation where we output a group that is larger than its complement? That's fine.
Thus final code:
def main():
import sys
n = int(sys.stdin.readline())
total = n * (n + 1) // 2
target = total // 2
chosen = []
for i in range(n, 0, -1):
if i <= target:
chosen.append(i)
target -= i
# Now compute diff
# The sum of chosen is total // 2 - target (target now 0)
# diff = abs(total - 2sum_chosen) = total - 2sum_chosen (since sum_chosen <= total/2)
# But we know it's total % 2.
diff = total % 2
# Actually if we want to be safe compute sum_chosen:
sum_chosen = (total // 2) - target
diff2 = total - 2 * sum_chosen
# They should match.
print(diff2)
print(len(chosen), *chosen)
if name == "main":
main()
That's it.
But double-check: In odd total case, sum_chosen = floor(total/2), diff2 = total - 2*floor(total/2) = 1. So good.
Now test with examples:
Input: 4 -> total=10 target=5 -> loop: i=4 take target=1, i=3 skip,i=2 skip,i=1 take target=0 => chosen [4,1] -> diff=0 -> output "0\n2 4 1"? Our print order: [4,1] -> prints "2 4 1". That's acceptable, but example printed "2 1 4". Order arbitrary.
Input: 2 -> total=3 target=1 -> loop: i=2 skip,i=1 take => chosen [1] -> diff=1 -> output "1\n1 1". Works.
Now test n=3: total=6 target=3 -> loop: i=3 take target=0 => chosen [3] -> diff=0 -> output "0\n1 3". That is valid.
Now test n=5: total=15 target=7 -> loop: i=5 take target=2, i=4 skip,i=3 skip,i=2 take target=0,i=1 skip -> chosen [5,2] -> diff=1 -> output "1\n2 5 2". Good.
Now test n=6: total=21 target=10 -> loop: i=6 take target=4,i=5 skip,i=4 take target=0 -> chosen [6,4] -> diff=1 -> output "1\n2 6 4". Good.
Now test n=7: total=28 target=14 -> loop: i=7 take target=7,i=6 take target=1,i=5 skip,i=4 skip,i=3 skip,i=2 skip,i=1 take target=0 -> chosen [7,6,1] -> diff=0 -> output "0\n3 7 6 1". Good.
Thus it's correct.
Now consider if the problem expects output to have the first group as the one we constructed; but it's okay.
One potential issue: In some languages, printing many numbers could be slow, but Python's print with * expansion may be okay for up to 30000 numbers. However, using join might be more efficient. But we can use sys.stdout.write to avoid overhead. But for 60000, printing up to 30000 numbers is okay in Codeforces Python if we use fast output. But typical Codeforces Python time limit maybe 2 seconds, printing 30000 numbers should be fine with print(*list). Alternatively, we could use write.
But note: The worst-case group size could be about n/2 = 30000, which is moderate. Printing 30000 integers as string with spaces, the output size could be ~30000*5 = 150k bytes, which is fine.
Thus solution is fine.
Now I'll write the final answer with explanation and code.
But before final, consider if there is a more optimal solution for outputting the group without storing list? We can compute the chosen numbers and also compute the complement. But we need to print size first. We could compute the set of chosen numbers as we go, and then count them. That's what we do. So fine.
Now I'll produce final message. ```python
import sys
def main() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
total = n * (n + 1) // 2 # S
target = total // 2 # floor(S/2)
chosen = []
# Greedy from n down to 1
for i in range(n, 0, -1):
if i <= target:
chosen.append(i)
target -= i
# The sum of the chosen group is exactly total//2 (target became 0)
diff = total - 2 * (total // 2 - target) # = total % 2
print(diff)
print(len(chosen), *chosen)
if name == "main":
main()