-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy path1276.cpp
More file actions
71 lines (71 loc) · 1.46 KB
/
1276.cpp
File metadata and controls
71 lines (71 loc) · 1.46 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
using namespace std;
int times=0,limit,m,n[20]={0},d[20]={0},cashmax=0,sum=0,cashnum[20]={0},cashtotal[20]={0};
void change(int t,int a)
{
sum+=t*d[a];
cashnum[a]+=t;
if(sum>cashmax)cashmax=sum;
}
void f(int a,int p)
{
int i;
times++;
if(times>10000000)return;
change(1,a);
for(i=a;i<=p;i++)
{
if((sum+d[p]>limit)||((sum+cashtotal[i+1]+d[i]*(n[i]-cashnum[i]))<=cashmax))break;
if(cashnum[i]<n[i]&&((sum+d[i])<=limit)&&((sum+cashtotal[i+1]+d[i]*(n[i]-cashnum[i]))>cashmax))f(i,p);
}
change(-1,a);
return;
}
int main()
{
int i,j,k,p,t;
while(cin>>limit>>m)
{
cashmax=sum=times=0;
for(i=0;i<=19;i++)
{
n[i]=d[i]=cashnum[i]=cashtotal[i]=0;
if(i>0&&i<=m)cin>>n[i]>>d[i];
}
if(limit*m==0)cout<<"0\n";
else
{
for(i=1;i<=m;i++)
for(j=i+1;j<=m;j++)
if(n[i]*d[i]<n[j]*d[j])
{
n[i]^=n[j];
n[j]^=n[i];
n[i]^=n[j];
d[i]^=d[j];
d[j]^=d[i];
d[i]^=d[j];
}
t=p=0;
for(i=m;i>=1;i--)
{
if(t==0&&n[i]*d[i]>0)
{
p=i;
t=1;
}
if(t==1)
{
cashtotal[i]=cashtotal[i+1]+n[i]*d[i];
}
}
for(i=1;i<=p;i++)
{
if(sum+d[p]>limit||sum+cashtotal[i+1]+d[i]*(n[i]-cashnum[i])<=cashmax)break;
if(cashnum[i]<n[i]&&sum+d[i]<=limit&&sum+cashtotal[i+1]+d[i]*(n[i]-cashnum[i])>cashmax)f(i,p);
}
cout<<cashmax<<endl;
}
}
return 0;
}