- 将所有价格组合模拟进
book数组 - 每一轮循环进行降重,优化时间/空间复杂度
- 数组降重
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#define ll long long
using namespace std;
ll n, x, pi;
int main()
{
cin >> n >> x;
vector<ll>book;
for (size_t i = 0; i < n; i++)
{
cin >> pi;
ll lenth = book.size();
for (size_t j = 0; j < lenth; j++)
{
book.push_back(book[j] + pi);
}
book.push_back(pi);
sort(book.begin(), book.end());
auto last = unique(book.begin(), book.end());
book.erase(last, book.end());
}
for (size_t i = 1; i < book.size(); i++)
{
if(x == book[i - 1])
{
cout << book[i - 1];
return 0;
}
if ((x > book[i - 1] && x < book[i]))
{
cout << book[i];
return 0;
}
}
return 0;
}