Since each group of data has a maximum of 20 data records, the total length of the data may consist of a maximum of 2 ^ 20 about 10 ^ 6. An array can be put down, and I use a vector. Select the creator of the total time length stored in the vector (and cannot exceed N), and calculate the data contained in the total time length based on the serial number. My problem-solving code is as follows:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #include <algorithm> using namespace std; #define maxm 20 int Duration[maxm]; int main() { int N,M; int Size,Max,Maxk; int tmp; vector<int> v; while(cin >> N >> M) { for(int i=0; i<M; i++) cin >> Duration[i]; Max = 0; v.clear(); v.push_back(0); for(int i=0; i<M; i++) { Size = v.size(); for(int j=0; j<Size; j++) { v.push_back( tmp = v[j]+Duration[i]); if(tmp<=N && Max<tmp) { Max = tmp; Maxk = v.size()-1; } } } int div = 2; for(int i=0; i<M; i++) { if(Maxk%div >= div/2) cout << Duration[i] << ' '; div *= 2; } cout << "sum:" << Max << endl; } return 0; }