#include #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; template struct edge{ int to; T wt; edge(int to,const T& wt):to(to),wt(wt){} }; template using weighted_graph=vector>>; template void add_directed_edge(weighted_graph& G,int u,int v,const T& wt){ G[u].emplace_back(v,wt); } random_device seed_gen; mt19937 rng(seed_gen()); int n,m; weighted_graph G; int calc_score(const vector& p){ int res=0; vector vis(n); rep(i,n){ int u=p[n-1-i]; vis[u]=true; for(auto e:G[u]) if(vis[e.to]) res+=e.wt; } return res; } int main(){ auto start=std::chrono::system_clock::now(); scanf("%d%d",&n,&m); G.resize(n); rep(i,m){ int u,v,c; scanf("%d%d%d",&u,&v,&c); add_directed_edge(G,u,v,c); } vector p(n); iota(p.begin(),p.end(),0); int ans=calc_score(p); auto p_ans=p; for(int t=0;;t++){ if(t%20==0){ auto now=std::chrono::system_clock::now(); if(std::chrono::duration_cast(now-start).count()>4990) break; } shuffle(p.begin(),p.end(),rng); int tmp=calc_score(p); if(tmp>ans){ ans=tmp; p_ans=p; } } while(1){ bool updated=false; rep(i,n) for(int j=i+1;jans){ ans=tmp; p_ans=p; updated=true; } else{ swap(p_ans[i],p_ans[j]); } } if(!updated) break; } printf("%d\n",ans); rep(i,n) printf("%d%c",p_ans[i],i