# 题目

∑ x ∈ S a x − ∑ ⟨ a , b ⟩ ∈ E a , b ∈ S v e \sum_{x\in S}a_x-\sum_{\langle a,b\rangle\in E}^{a,b\in S}v_e

2 m ⩽ n ⩽ 1 0 5 2m\leqslant n\leqslant 10^5 0 ⩽ x 0 , y 0 , a 0 , z 0 < p 0\leqslant x_0,y_0,a_0,z_0<p

# 代码

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long llong;

int a = 0, c = getchar(), f = 1;
for(; !isdigit(c); c=getchar())
if(c == '-') f = -f;
for(; isdigit(c); c=getchar())
a = (a<<3)+(a<<1)+(c^48);
return a*f;
}

const int __q = 101, __b = 137, __p = 1e9+7;
const int MAXN = 100005;
struct Edge{

int to, nxt, val;
Edge() = default;
Edge(int _to,int _nxt,int _val):
to(_to),nxt(_nxt),val(_val){

}
};
Edge e[MAXN];

}

llong sum[MAXN], a[MAXN];
bool dead[MAXN]; ///< if it's decided

int sta[MAXN], top, dfn[MAXN];
void bfs(int x){

sta[++ top] = x, dfn[x] = top;

if(!dfn[e[i].to]) bfs(e[i].to);
if(dfn[e[i].to] > dfn[x])
sum[e[i].to] -= e[i].val;
}
}

llong best_val;
void dfs(int x,const llong &now_val){

if(x == top+1){

if(best_val < now_val)
best_val = now_val;
return ; // the end
}
// if this one not chosen
if(sum[sta[x]] > a[sta[x]])
dfs(x+1,now_val);
// if this one is chosen
if(a[sta[x]] > 0){

a[e[i].to] -= e[i].val;
dfs(x+1,now_val+a[sta[x]]);
a[e[i].to] += e[i].val;
}
}

int deg[MAXN];
int main(){

{

// input
set< std::pair<int,int> > s;
rep(i,1,n) a[i] = (__q*a[i-1]+__b)%__p;
for(int i=1,x,y; i<=m; ++i){

x0 = int((llong(__q)*x0+__b)%__p);
y0 = int((llong(__q)*y0+__b)%__p);
z0 = int((llong(__q)*z0+__b)%__p);
x = x0%n+1, y = y0%n+1;
if(x == y || s.count(make_pair(x,y))
|| s.count(make_pair(y,x))) continue;
s.insert(std::make_pair(x,y));
sum[x] += z0, sum[y] += z0;
++ deg[x], ++ deg[y]; // to delete leaf
}
}
llong ans = 0;
{

// cut branches
std::queue<int> q;
rep(i,1,n) q.push(i);
while(!q.empty()){

int x = q.front(); q.pop();
if(sum[x] <= a[x]){

// always chosen
dead[x] = true; ans += a[x];

sum[e[i].to] -= e[i].val;
a[e[i].to] -= e[i].val; // both chosen
-- deg[e[i].to];
q.push(e[i].to); // re-check
}
}
else if(a[x] <= 0){

// never chosen

sum[e[i].to] -= e[i].val;
-- deg[e[i].to];
q.push(e[i].to); // re-check
}
}
// deg = 0 meaning sum = 0, determined
else if(deg[x] == 1){

ans += a[x]; dead[x] = true;

sum[e[i].to] -= e[i].val;
a[e[i].to] -= a[x];
-- deg[e[i].to];
q.push(e[i].to); break;
}
}
}
}
for(int i=1; i<=n; ++i)

top = 0, bfs(i), best_val = 0;
dfs(1,0); ans += best_val;
}
printf("%lld\n",ans);
return 0;
}