#C2366. Lexicographically Smallest Permutation with ASCII Sum Multiple Condition
Lexicographically Smallest Permutation with ASCII Sum Multiple Condition
Lexicographically Smallest Permutation with ASCII Sum Multiple Condition
Given a positive integer y and a string s consisting of lowercase alphabets, your task is to find the lexicographically smallest permutation of s such that the sum of the ASCII values of its characters is a multiple of y.
In other words, let \(S\) be the sum of the ASCII values of the characters in s, i.e.,
$$ S = \sum_{i=1}^{|s|} \text{ord}(s_i), $$
then you should output the lexicographically smallest permutation of s (which is equivalent to sorting the string in non-decreasing order) if \(S \equiv 0 \pmod{y}\), otherwise output -1
.
Example:
Input: 5 abcde Output: abcde</p>Input: 10 abcdef Output: -1
inputFormat
The first line of input contains a single integer T (the number of test cases).
Each of the following T lines contains an integer y and a string s separated by a space.
Note: The string s consists only of lowercase English letters.
outputFormat
For each test case, output in a separate line the lexicographically smallest permutation of s if the sum of its ASCII values is divisible by y, otherwise output -1
.
1
5 abcde
abcde
</p>