Cut Ribbon

Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the following two conditions:

  • After the cutting each ribbon piece should have length ab or c.
  • After the cutting the number of ribbon pieces should be maximum.

Help Polycarpus and find the number of ribbon pieces after the required cutting.

Input

The first line contains four space-separated integers nab and c (1 ≤ n, a, b, c ≤ 4000) — the length of the original ribbon and the acceptable lengths of the ribbon pieces after the cutting, correspondingly. The numbers ab and c can coincide.

Output

Print a single number — the maximum possible number of ribbon pieces. It is guaranteed that at least one correct ribbon cutting exists.

Examples
input
Copy
5 5 3 2
output
Copy
2
input
Copy
7 5 5 2
output
Copy
2
Note

In the first example Polycarpus can cut the ribbon in such way: the first piece has length 2, the second piece has length 3.

In the second example Polycarpus can cut the ribbon in such way: the first piece has length 5, the second piece has length 2.


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define mod 998244353
using namespace std;
int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };


int main() 
{
    int n;
    cin >> n;
    vector<int> a(3),dp(n+1);
    for (int i = 0; i < 3; i++)
        cin >> a[i];
    sort(a.begin(), a.end());
    if (a[0] <= n) dp[a[0]] = 1;
    if (a[1] <= n) dp[a[1]] = 1;
    if (a[2] <= n) dp[a[2]] = 1;
    for (int i = 1; i <= n; i++)
    {
        int ans = dp[i];
        if (i > a[0] && dp[i - a[0]] != 0) ans = max(ans, dp[i - a[0]] + 1);
        if (i > a[1] && dp[i - a[1]] != 0) ans = max(ans, dp[i - a[1]] + 1);
        if (i > a[2] && dp[i - a[2]] != 0) ans = max(ans, dp[i - a[2]] + 1);
        dp[i] = ans;
    }
    cout << dp[n] << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/dealer/p/12325505.html