#C9334. Longest Balanced Subsequence

    ID: 53416 Type: Default 1000ms 256MiB

Longest Balanced Subsequence

Longest Balanced Subsequence

You are given an array of n integers. Your task is to find the longest balanced subsequence from the array and output it in a sorted order.

A balanced subsequence in this problem is defined as the entire sorted array derived from the given input, which minimizes the difference between the sums of its two halves. In mathematical terms, if you split the sorted array into two halves (if the length is odd, one half will have one extra element), the absolute difference between the sum of the left half and the sum of the right half is minimized. Since sorting achieves this balance naturally for this problem, the solution is to sort the array.

The program should print the length of the balanced subsequence in the first line, followed by the elements of the subsequence in the second line, separated by spaces.

The only formula involved is the minimization condition expressed as: \(\min \left| \sum_{i=1}^{k} a_i - \sum_{i=k+1}^{n} a_i \right|\), where the sequence \(\{a_1, a_2, \ldots, a_n\}\) is in sorted order and \(k\) is the split point.

inputFormat

The first line of input consists of a single integer n (1 ≤ n ≤ 105), representing the number of elements in the array.

The second line contains n space-separated integers, representing the elements of the array. Each integer is in the range of \(-10^9\) to \(10^9\).

outputFormat

Output two lines:

  • The first line contains a single integer denoting the length of the balanced subsequence.
  • The second line contains the elements of the balanced subsequence (which is the sorted order of the array), separated by a single space.
## sample
6
1 7 3 4 5 6
6

1 3 4 5 6 7

</p>