Official

B - Can you buy them all? Editorial by en_translator


Prepare a variable to store the sum of the prices, use a for loop to repeat \(N\) times, and in each iteration add the price of each item just as the problem statement describes. Finally compare the sum of the prices with \(X\).

Note that the index is flipped when the program is \(0\)-indexed.

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
    int N, X; cin >> N >> X;
    int sum = 0;
    for(int i = 1; i <= N; i++){
        int A; cin >> A;
        if(i%2 == 0)A--;
        sum += A;
    }
    if(X >= sum) cout << "Yes" << endl;
    else  cout << "No" << endl;
}

Note that one may also write as follows.

Sample code (Python)

N, X = map(int, input().split())
A = list(map(int, input().split()))
if sum(A) - N//2 <= X :
    print("Yes")
else:
    print("No")

posted:
last update: