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

#FT0000066. Đảo ngược chuỗi bằng đổi chỗ kề (String Reversal)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đảo ngược chuỗi bằng đổi chỗ kề (String Reversal)

String Reversal

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a string ss of nn lowercase English letters. Transform it into the reversal of the original string, so the first character becomes the last, the second becomes the second-to-last, and so forth.

In one operation you may swap characters at adjacent positions. You may perform any number of operations, including zero for an already palindromic string.

Find the minimum number of adjacent swaps needed to obtain the exact reversed string. Equal letters may occur multiple times and need not be distinguished in the final string.

Input

The first line contains nn, the length of the string.

The second line contains ss, exactly nn lowercase letters a–z.

Output

Print one integer: the minimum number of adjacent character swaps.

Subtasks

  • Subtask 1 (20 points): 2≤n≤602\le n\le 60; ∣s∣=n|s|=n; each character of ss is in a–z.

  • Subtask 2 (30 points): 2≤n≤30002\le n\le 3000; ∣s∣=n|s|=n; each character of ss is in a–z.

  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000; ∣s∣=n|s|=n; each character of ss is in a–z.

Examples

Input

5
aaaza

Output

2

Explanation

The target is the reverse of aaaza, namely azaaa.

  • Swap positions 33 and 44: aaaza becomes aazaa.
  • Swap positions 22 and 33: aazaa becomes azaaa.

Two swaps are sufficient. The only z must move from position 44 to position 22, and an adjacent swap moves it by at most one position; therefore fewer than two swaps cannot work. The answer is 2.