1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int n, m, wynik=0, plecak, a;
vector <int> t, p;

int main()
{
    cin >> n >> m;

    for (int i=0; i<n; i++)
    {cin >> a; t.push_back(a);}

    for (int i=0; i<m; i++)
    {cin >> a; p.push_back(a);}

    sort(t.begin(), t.end());
    sort(p.begin(), p.end());
    for (int i=m-1; i>=0; i--)
    {
        plecak=p[i];
        for (int j=t.size()-1; j>=0; j--)
        if (t[j]<=plecak) {plecak-=t[j]; t.erase(t.begin()+j);}
        wynik++;
        if (t.size()==0) {cout << wynik; break;}
    }
    if (t.size()!=0) cout << "NIE";
}