Skip to main content
Back to problems
Codeforces
Medium
Strings
Simulation
Number Theory
Anton and currency you all know

Convert a number string to the largest possible value by changing at most one digit according to the allowed currency digit mapping.

Acceptance 0%
Problem Statement

You are given a number written in a special currency system where each digit can be converted to another digit according to a fixed mapping. Your task is to make the resulting number as large as possible by applying the conversion rules to at most one digit, or to all digits if that is required by the exact rule set of the problem.

Read the digits of the number, apply the allowed transformation carefully, and output the resulting number in the same order. The key is to follow the digit-mapping rule exactly and choose the best position to improve the value when multiple choices are possible.

Input Format

Input

  • The first line contains an integer nn — the length of the number.
  • The second line contains a string of nn digits.
  • The next lines describe the digit transformation rules or the relevant mapping for this currency system.

Notes

  • This is an implementation-style problem.
  • Treat the number as a string; leading zeros, if any, should be preserved unless the rules explicitly change them.

Output Format

Output

  • Print the transformed number after applying the best valid conversion.

If there are multiple valid answers, print any one that satisfies the rules.

Constraints

  • 1n1051 \le n \le 10^5
  • Digits are decimal characters.
  • The solution should run in linear time or close to it.
Examples
Sample cases returned by the problem API.

Example 1

Input

5
12345

Output

52345

Explanation

If the rules allow changing one digit to a larger one, the best choice is to improve the earliest possible digit, since that has the biggest impact on the final value.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.