ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules

2217. Binary Sort

Time limit: 1.0 second
Memory limit: 256 MB
Recently, Timofey started studying algorithms and is currently learning about sorting. To reinforce the material, Timofey decided to practice on a string S of length N, consisting of zeros and ones. Sorting the entire string would be too simple, so Timofey decided to sort substrings of S, meaning he can perform the following operation:
  • Choose two indices l and r (1 ≤ lrN) and sort the substring Sl Sl+1Sr in non-decreasing order.
To train well, Timofey set himself a goal. He wants to be able to transform the string S into a palindrome by applying the substring sorting operation an unlimited number of times (possibly zero). Can Timofey achieve his goal?
A palindrome is a string that reads the same forwards and backwards.

Input

The first line contains a single integer N — the length of the string S (1 ≤ N ≤ 105).
The second line contains the string S, consisting only of the characters 0 and 1.

Output

Print “YES” if there exists a sequence of operations that transforms the string S into a palindrome, and “NO” otherwise.

Samples

inputoutput
12
010110110100
YES
1
1
YES
2
10
NO

Notes

In the first example, the string 010011110010 can be obtained:
  • First, sort l = 4, r = 6, resulting in 010011110100.
  • Then, sort l = 10, r = 11, resulting in 010011110010.
Problem Author: Timofey Imaev
Problem Source: ICPC Ural Regional Contest Qualifier 2025