-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuns.java
More file actions
46 lines (42 loc) · 1.26 KB
/
buns.java
File metadata and controls
46 lines (42 loc) · 1.26 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.List;
public class buns {
static int mod = (int) 1e9 + 7;
static int[][] dp;
static int c0,d0;
static int[] a,b,c,d;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt() , m = sc.nextInt();
c0=sc.nextInt();d0=sc.nextInt();
a=new int[m+1];b=new int[m+1];c=new int[m+1];d=new int[m+1];
dp=new int[n+1][m+1];
for(int[] i:dp)Arrays.fill(i,-1);
for(int i=1;i<=m;i++){
a[i]=sc.nextInt();
b[i]=sc.nextInt();
c[i]=sc.nextInt();
d[i]=sc.nextInt();
}
System.out.println(sol(n,m));
}
static int sol(int n,int m){
if(n==0)return 0;
if(m==0)return (n/c0)*d0;
if(dp[n][m]!=-1)return dp[n][m];
int ans=sol(n,m-1);
for(int i=1;i<=n/c0;i++){
ans=Math.max(ans,i*d0+sol(n-i*c0,m-1));
}
for(int i=1;i<=a[m]/b[m] && n-i*c[m]>=0;i++){
ans=Math.max(ans,i*d[m]+sol(n-i*c[m],m-1));
}
return dp[n][m]=ans;
}
}