#if defined(__GNUC__) && !defined(__clang__) #pragma GCC optimize("O3") #if defined(__x86_64__) || defined(__i386__) #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #endif #endif #include #include #define each(x, v) for (auto&& x : v) #define each2(x, y, v) for (auto&& [x, y] : v) #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--) #define all(x) begin(x),end(x) #define sz(x) (int)(x).size() using namespace std; using namespace atcoder; using ll=long long; using ull=unsigned long long; using ld=long double; using vll=vector; using vvll=vector; using vi=vector; using vvi=vector; using pll=pair; using pi=pair; // using mint=modint998244353; using mint=modint1000000007; // using mint2=static_modint<2>; // using mint=LazyMontgomeryModInt<998244353>; using vm=vector; using vvm=vector; #line 2 "graph/static-graph.hpp" namespace StaticGraphImpl { template ::value> struct E; template struct E { int to; T cost; E() {} E(const int& v, const T& c) : to(v), cost(c) {} operator int() const { return to; } }; template struct E { int to; E() {} E(const int& v) : to(v) {} operator int() const { return to; } }; template struct StaticGraph { private: template struct Es { It b, e; It begin() const { return b; } It end() const { return e; } int size() const { return int(e - b); } auto&& operator[](int i) const { return b[i]; } }; int N, M, ec; vector head; vector>> buf; vector> es; void build() { partial_sum(begin(head), end(head), begin(head)); es.resize(M); for (auto&& [u, e] : buf) es[--head[u]] = e; } public: StaticGraph(int _n, int _m) : N(_n), M(_m), ec(0), head(N + 1, 0) { buf.reserve(M); } template void add_edge(int u, Args&&... args) { #pragma GCC diagnostic ignored "-Wnarrowing" buf.emplace_back(u, E{std::forward(args)...}); #pragma GCC diagnostic warning "-Wnarrowing" ++head[u]; if ((int)buf.size() == M) build(); } Es>::iterator> operator[](int u) { return {begin(es) + head[u], begin(es) + head[u + 1]}; } const Es>::const_iterator> operator[](int u) const { return {begin(es) + head[u], begin(es) + head[u + 1]}; } int size() const { return N; } }; } // namespace StaticGraphImpl using StaticGraphImpl::StaticGraph; /** * @brief Static Graph * @docs docs/graph/static-graph.md */ int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin>>N>>M; if(M==0){ cout<g(N,2*M); rep(i,0,M){ int u,v,w; cin>>u>>v>>w; --u,--v; g.add_edge(u,v,w); g.add_edge(v,u,w); } vll D(N); rep(u,0,N){ each2(v,w,g[u])D[u]+=w; } sort(all(D),greater()); ll ans=0; rep(i,0,N){ if(i&1)ans-=D[i]; else ans+=D[i]; } cout<