随一个顺序然后卡时跑暴力即可。

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
#include<bits/stdc++.h>
using namespace std;
struct node{
int x,y,id;
}a[100005];
vector<node> b[100005],c[100005];
bool cmpx(node x,node y){
return x.x<y.y;
}
bool cmpy(node x,node y){
return x.y<y.y;
}
mt19937 rd(43678902);
int n,m,len[105],cnt=1,id[100005];
long long calc(int banid){
long long res=0;
for(int i=1;i<=cnt;++i)c[i].clear();
long long t=1,mx=0,now=m;
for(int i=1;i<=n;++i){
if(i==banid)continue;
if(a[i].x<=now){
c[t].push_back(a[i]),mx=max(mx,1ll*a[i].y),now-=a[i].x;
if(!now)res+=mx,mx=0,++t,now=m;
}
else{
int le=a[i].x-now;
auto tmp=a[i];
tmp.x=le;tmp.y=(int)ceil(1.0*tmp.y*now/a[i].x);
mx=max(mx,1ll*tmp.y);
c[t].push_back(tmp);++t;res+=mx;mx=0;now=m;
}
}
if(mx)res+=mx;
return res;
}
int main(){
ios::sync_with_stdio(0);
cin>>m>>n;
for(int i=1;i<=n;++i)cin>>a[i].x>>a[i].y,a[i].id=i;
int now=m;
for(int i=1;i<=n;++i){
if(a[i].x<=now){
b[cnt].push_back(a[i]);now-=a[i].x;
if(!now)++cnt,now=m;
}
else{
int le=a[i].x-now;
auto tmp=a[i];
tmp.x=le;tmp.y=(int)ceil(1.0*tmp.y*now/a[i].x);
b[cnt].push_back(tmp);
++cnt;now=m;
}
}
if(!b[cnt].size())--cnt;
long long ans=1e17;
for(int i=1;i<=n;++i)id[i]=i;
if(n>=50000)shuffle(id+1,id+n+1,rd);

for(int i=1;i<=n;++i){
if(1.0*clock()/CLOCKS_PER_SEC>=1.98)break;
ans=min(ans,calc(id[i]));
}
cout<<ans<<'\n';
return 0;
}