[ABC429]B - N - 1题解

Time Limit: 2 sec / Memory Limit: 1024 MiB
时间限制:2 秒 / 内存限制:1024MiB

Score : 200 points  分数: 200 分

Problem Statement  题目描述

You are given an integer sequence of length NA=(A1​,A2​,…,AN​), and an integer M.
给定一个长度为 N 的整数序列 A=(A1​,A2​,…,AN​) 和一个整数 M 。

Determine whether it is possible to remove one of the N elements of A so that the sum of the remaining (N−1) elements is exactly M.
判断是否可以移除 A 中的一个 N 元素,使得剩余的 (N−1) 元素的和正好是 M 。

Constraints  约束条件

  • 2≤N≤100
  • 0≤M≤10000
  • 0≤Ai​≤100
  • All input values are integers.
    所有输入值都是整数。

Input  输入

The input is given from Standard Input in the following format:
标准输入中给出的输入格式如下:

N M
A1​ A2​ … AN

Output  输出

If it is possible to remove one of the N elements of A so that the sum of the remaining (N−1) elements is exactly M, print Yes; otherwise, print No.
如果可以移除 A 中的 N 元素,使得剩余的 (N−1) 元素之和正好为 M ,则打印 Yes ;否则,打印 No 。


Sample Input 1  示例输入 1 复制

Copy  复制
4 10
3 2 3 4

Sample Output 1  样本输出 1 复制

Copy  复制
Yes

If you choose A1​,A3​,A4​, the sum is 3+3+4=10. Therefore, print Yes.
如果选择 A1​,A3​,A4​ ,则总和为 3+3+4=10 。因此,打印 Yes 。


Sample Input 2  示例输入 2 复制

Copy  复制
5 16
3 3 4 2 5

Sample Output 2  样例输出 2 复制

Copy  复制
No

No matter how you choose four elements, their sum is not equal to 16. Therefore, print No.
无论如何选择四个元素,它们的和都不等于 16 。因此,打印 No 。


Sample Input 3  样例输入 3 复制

Copy  复制
6 16
0 8 0 2 6 8

Sample Output 3  样本输出 3 复制

Copy  复制
Yes

思路

直接枚举即可。

代码见下

#include<bits/stdc++.h> 
using namespace std;
long long n,m,a[105],b=0;
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        b+=a[i];
    }
    for(int i=1;i<=n;i++){
        if(b-a[i]==m){
            cout<<"Yes"<<endl;
            return 0;
        }
    }
    cout<<"No"<<endl;
	return 0; 	
}

posted @ 2025-10-25 21:47  bz02_2023f2  阅读(1)  评论(0)    收藏  举报  来源