| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 8 초 (추가 시간 없음) | 1024 MB | 8 | 7 | 6 | 85.714% |
While looking through the Grand Archive of Problematic Code, you found this horrendous mistake in one of the code snippets. In this code, they tried to calculate the sum of an array, but got indices and values mixed up. The code of this sum function is shown in Figure H.1.
long long sum(vector<int> a) {
long long ans = 0;
for (int x : a)
ans += a[x];
return ans;
}
def sum(a: list[int]) -> int: ans = 0 for x in a: ans += a[x] return ans
long sum(int[] a) {
long ans = 0;
for (int x : a)
ans += a[x];
return ans;
}
fun sum(a: List<Int>): Long {
var ans = 0L
for (x in a)
ans += a[x]
return ans
}
Figure H.1: The function sum intends to calculate the sum of an array, but x refers to a value in the array, instead of an index. The code is shown in C++ and Python in the top row, and Java and Kotlin in the bottom row.
You wonder how this function behaves exactly, and decide to thoroughly test it. Starting with some initial array, you apply a sequence of updates. For each update, you change one of the values of the array. You wonder what the value of the function is after each update.
The input consists of:
Note that the array uses 0ドル$-based indexing.
For each update, output the return value of the function sum applied to the array after applying the update.
5 0 0 3 2 0 4 1 1 0 4 3 4 1 4
6 10 9 8
5 4 2 2 4 2 3 0 1 4 3 2 3
10 13 16
University > Delft University of Technology > Freshmen Programming Contest 2024 H번