#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3RhsN1z ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
signed main(){
_3RhsN1z;
int m,n;
cin>>m>>n;
using T=pair<int,pii>;
vec(T) a(n);
vec(pii) ina(n);
rep(i,n){
cin>>a[i].fi>>a[i].se.fi;
a[i].se.se=i;
ina[i]={a[i].fi,a[i].se.fi};
}
sort(a.begin(),a.end());
const int si=6;
int k=0;
vec(vec(pii)) rbts(si);
vi went_on(n);
while(k<si){
int now=0;
while(now<m){
assert(sz(a));
T u=a.back();
a.pop_back();
int need=min(u.se.fi,m-now);
assert(need>0);
rbts[k].pb({need,u.se.se});
u.se.fi-=need;
now+=need;
if(u.se.fi>0){
went_on[u.se.se]=1;
a.pb(u);
}
}
k+=1;
}
while(1){
bool fnd=0;
rep(i,si){
if(sz(rbts[i])>1){
pii p=rbts[i].back();
if(went_on[p.se]){
fnd=1;
// rbts[i].pop_back();
int ni=i+1;
vec(pii) tmp=rbts[ni];
rbts[i].pop_back();
rng(j,1,sz(tmp)){
rbts[i].pb(tmp[j]);
}
rbts.erase(rbts.begin()+ni);
tmp.clear();
tmp.pb({m,p.se});
rbts.pb(tmp);
}
}
}
if(!fnd) break;
}
int res=0;
rep(i,si){
for(auto p:rbts[i]){
res+=p.fi*ina[p.se].fi;
}
}
cout<<res<<"\n";
rep(i,si){
cout<<rbts[i][0].se+1<<" ";
}
cout<<"\n";
vec(T) pns;
rep(i,si){
int tm=0;
rep(j,sz(rbts[i])-1){
tm+=rbts[i][j].fi;
pns.pb({tm,{rbts[i][j].se,rbts[i][j+1].se}});
}
}
sort(pns.begin(),pns.end());
cout<<sz(pns)<<"\n";
for(auto tp:pns){
cout<<tp.fi<<" "<<tp.se.fi+1<<" "<<tp.se.se+1<<"\n";
}
//checker
// {
// vec(vi) tq(m);
// rep(i,si){
// int tm=0;
// rep(j,sz(rbts[i])){
// rng(k,tm,tm+rbts[i][j].fi){
// tq[k].pb(rbts[i][j].se);
// }
// tm+=rbts[i][j].fi;
// }
// }
// vi cnt(n);
// for(int i=0;i<m;i++){
// assert(sz(set<int>(tq[i].begin(),tq[i].end()))==si);
// for(auto v:tq[i]){
// cnt[v]+=1;
// }
// }
// map<int,int> in,out;
// for(auto tp:pns){
// in[tp.se.se]=tp.fi;
// if(out.find(tp.se.se)!=out.end() and abs(out[tp.se.se]-tp.fi)<1){
// print(tp.se.se);
// assert(0);
// }
// out[tp.se.fi]=tp.fi;
// if(in.find(tp.se.fi)!=in.end() and abs(in[tp.se.fi]-tp.fi)<1){
// assert(0);
// }
// }
// }
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Integer 53 violates the range [1, 49] |
2 |
Incorrect |
3 ms |
596 KB |
Integer 2835 violates the range [1, 2799] |
3 |
Incorrect |
7 ms |
1440 KB |
Integer 539738 violates the range [1, 499999] |
4 |
Incorrect |
0 ms |
340 KB |
Integer 2093 violates the range [1, 1999] |
5 |
Incorrect |
3 ms |
852 KB |
Integer 200078 violates the range [1, 199999] |
6 |
Incorrect |
1 ms |
468 KB |
Integer 351 violates the range [1, 349] |
7 |
Incorrect |
2 ms |
724 KB |
Integer 1501 violates the range [1, 1499] |
8 |
Incorrect |
24 ms |
5652 KB |
Integer 50024 violates the range [1, 49999] |
9 |
Incorrect |
122 ms |
24332 KB |
Integer 500169 violates the range [1, 499999] |
10 |
Incorrect |
139 ms |
24548 KB |
Integer 500096 violates the range [1, 499999] |