Official

A - Shift Editorial by en_translator


If you are new to AtCoder and do not know where to start, first try Problem A “Welcome to AtCoder” in the practice contest. There, you can find a sample for basic input and output.
If you are not used to problems in competitive programming enough, we recommend you to solve some of the problems in AtCoder Beginners Selection.


This problem asks you to handle a for loop properly.

First, let us consider how we can perform the operation against \(A = (A_1, A_2, \dots, A_N)\). Among various approaches, we think that updating \(A\) in the following way is the naivest one.

  • For \(i = 1, 2, \dots, N-1\) in this order, “update \(A_i\) by assigning \(A_{i + 1}\).” (This can be implemented with a for loop with a variable i.)
  • Update \(A_N\) by assigning \(0\).

All that left is to repeat the steps above \(K\) times, which can also be implemented with a for loop to let the same process be executed \(K\) times.

A sample code in C++ follows.

#include <iostream>
using namespace std;

int N, K, A[101];

int main() {
  cin >> N >> K;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }
  for (int loop = 1; loop <= K; loop++) {
    for (int i = 1; i <= N - 1; i++) {
      A[i] = A[i + 1];
    }
    A[N] = 0;
  }
  for (int i = 1; i <= N; i++) {
    cout << A[i] << (i == N ? "\n" : " ");
  }
}

A sample code in Python follows. In Python, you may use a feature called slice of a sequence to make the code for updating a little bit simpler.

N, K = map(int, input().split())
A = list(map(int, input().split()))
for i in range(K):
  A = A[1:] + [0]
print(*A)

posted:
last update: