PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#TP0000074. Sinh nhật Sage (bản khó) (Sage’s Birthday (Hard Version))

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sinh nhật Sage (bản khó) (Sage’s Birthday (Hard Version))

Sage’s Birthday (Hard Version)

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Rearrange nn prices (duplicates allowed) to maximize the number of internal elements strictly smaller than both neighbors. Output both the optimal number and one optimal rearrangement.

Input

Line 1: nn. Line 2: nn prices.

Output

Line 1: optimal count. Line 2: any permutation of the given multiset realizing that count.

Subtasks

  • Subtask 1 (20 points): Exactly one dataset; 1≤n≤81\le n\le8; 1≤ai≤1091\le a_i\le10^9; equal prices are allowed.
  • Subtask 2 (30 points): Exactly one dataset; 1≤n≤20001\le n\le2000; 1≤ai≤1091\le a_i\le10^9; equal prices are allowed.
  • Subtask 3 (50 points): Exactly one dataset; 1≤n≤1000001\le n\le100000; 1≤ai≤1091\le a_i\le10^9; equal prices are allowed.

Example

Input

7
1 3 2 2 4 5 4

Output

3
3 1 4 2 4 2 5

Explanation

The displayed order has three strict local minima at positions 2, 4, and 6; no order of seven elements can have more.