問題一覧 > 通常問題

No.927 Second Permutation

レベル : / AI推定難易度 : 525 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 332
作問者 : leaf_1415 / テスター : tempura_pp
お気に入りにしたユーザー ProblemId : 2795 / 自分の提出
問題文最終更新日: 2019-05-02 04:01:33
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 231 (順位表) の他の問題:

Problem Statement

You are given a positive integer $X$.
Rearrange the digits that make up $X$ in any way you like to form a positive integer $Y$.
However, the most significant digit of $Y$ must not be '0'.

Output the $2$nd largest number among the numbers that can be formed as $Y$.
If no such number exists, output $-1$.

Input

$X$

The input satisfies the following constraints.
$10 \leq X < 10^{100000}$
$X$ is an integer

Output

Output the 2nd largest number among the numbers that can be formed as $Y$. If no such number exists, output −1. Print a newline at the end.

Sample

Sample 1
Input
213
Output
312

The values of $Y$ that can be formed from $X=213$ are,
in descending order, the six numbers $321, 312, 231, 213, 132, 123$,
so output $312$, the $2$nd largest among them.

Sample 2
Input
999
Output
-1

No matter how the digits that make up $X$ are rearranged, the resulting number is $999$.
The only number that can be formed as $Y$ is $999$, so "the $2$nd largest number among the numbers that can be formed as $Y$" does not exist.
Therefore, output $-1$.

Sample 3
Input
50
Output
-1

The only number that can be formed as $Y$ is $50$,
so "the $2$nd largest number among the numbers that can be formed as $Y$" does not exist.
Therefore, output $-1$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。