#K44627. Execute Commands on an Array

    ID: 27574 Type: Default 1000ms 256MiB

Execute Commands on an Array

Execute Commands on an Array

You are given an array of integers and a series of commands to process sequentially. There are three types of commands:

  • sumfirstk k: Calculate the sum of the first \(k\) elements of the array.
  • rotateleft n: Rotate the array to the left by \(n\) positions. When \(n\) exceeds the array's length, perform the rotation modulo the array length.
  • sortfirstm m: Sort the first \(m\) elements of the array in non-decreasing order.

After executing a command, output the result on a new line. For the sum command, output the integer sum. For rotate and sort commands, output the updated array as space-separated integers on a single line.

inputFormat

The input is read from standard input (stdin) in the following format:

  1. The first line contains an integer (n), representing the size of the array.
  2. The second line contains (n) space-separated integers, the elements of the array.
  3. The third line contains an integer (q), denoting the number of commands.
  4. Each of the following (q) lines contains a command in one of these formats:
    • sumfirstk k
    • rotateleft n
    • sortfirstm m

Here, (k), (n), and (m) are positive integers.

outputFormat

For each command, output the result on a separate line to standard output (stdout). If the command is sumfirstk, output the computed sum. If the command is rotateleft or sortfirstm, output the modified array as space-separated integers on one line.## sample

5
1 2 3 4 5
1
sumfirstk 3
6

</p>