Official

A - Contest Result Editorial by en_translator


If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).


Implement the following procedure.

  1. Receive the input.
  2. Prepare a variable ans, initializing it with \(0\).
  3. For each \(i\ (1\leq i \leq M)\), add \(A_{B_i}\) to ans.
  4. Print the value of ans.

For more specific implementation, see the sample codes below (in C++ and Python). Note that, depending on implementation, you may need to subtract \(1\) from \(B_i\) that you received from the input, since the indices start with \(0\) in many programming languages.

C++

#include<bits/stdc++.h>

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(m);
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < m; i++) {
        cin >> b[i];
        --b[i];
    }
    int ans = 0;
    for (int i = 0; i < m; i++) ans += a[b[i]];
    cout << ans << endl;
}

Python

n, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
for i in range(0, m):
    ans += a[b[i] - 1]
print(ans)

posted:
last update: