Problem:
Problem H: Yu-kun Likes a Directed Graph
Background
会津大学付属幼稚園はプログラミングが大好きな子供が集まる幼稚園である。園児の一人であるゆう君は、プログラミングと同じくらいお絵描きが大好きだ。
これまでゆう君は、丸と矢印で沢山絵を書いてきた。矢印は必ず丸と丸を結ぶようにして描いてある。ある日ゆう君はこれらの絵がグラフであることを知る。
丸は頂点と呼ばれ、矢印は辺と呼ばれているらしい。更にゆう君は、矢印を描く際に、必ずその上に１つ正の整数を書いていた。このように、辺に重みがあり有向な辺からなるグラフは重み付き有向グラフと呼ばれる。
今日ゆう君は、新たに閉路という言葉を知った。閉路とは、連結した辺の列であり、それに含まれるどの頂点も高々1度しか現れないもので、最初の頂点と最後の頂点が同じであるようなものをいう。閉路の辺の重みの総和が負になる場合、そのような閉路は負の閉路と呼ばれる。
新たな言葉を沢山知ったゆう君は一つの問題を思いついた。
Problem
閉路を持たない重み付き有向グラフが与えられる。
異なる２つの頂点
i
,
j
を選び、
i
から
j
に重み
w
(
w
< 0 ) の有向な辺を1本追加する。
これによってグラフ内に負の閉路ができるような
i
、
j
をみつけ、その負の閉路に属する辺の重みの総和の最大値を求めよ。
そのような
i
,
j
が存在しない場合は"NA"と出力すること。
Input
V
E
w
s
0
t
0
c
0
s
1
t
1
c
1
...
s
(E-1)
t
(E-1)
c
(E-1)
１行目に頂点の数
V
, 辺の数
E
, 追加する辺の重み
w
が空白区切りで与えられる。
続く
E
行に有向な辺の情報が
s
i
t
i
c
i
として与えられる。 ( 0 ≤
i
≤
E-1
)
これは、
s
i
から
t
i
に向けて重み
c
i
の有向な辺が存在することを表す。
Constraints
入力は以下の条件を満たす。
2 ≤
V
≤ 100000
1 ≤
E
≤ 500000
-100 ≤
w
< 0
0 ≤
c
i
≤ 100 ( 0 ≤
i
≤
E-1
)
0 ≤
s
i
,
t
i
≤
V-1
( 0 ≤
i
≤
E-1
)
入力中に同じ
s
i
と
t
i
のペアが現れることはない
s
i
と
t
i
は異なる
Output
重み
w
の有向な辺を追加することで出来る負の閉路に属する辺の重みの総和の最大値を１行に出力せよ。
どこに辺を追加しても負の閉路が作れない場合は"NA"と出力すること。
Sample Input 1
3 2 -3
0 1 1
0 2 5
Sample Output 1
-2
Sample Input 2
3 2 -1
0 1 1
0 2 5
Sample Output 2
NA
Sample Input 3
7 8 -8
0 1 5
0 4 3
1 2 10
1 3 1
3 6 6
4 3 1
4 5 2
4 6 2
Sample Output 3
-1
Sample Input 4
5 4 -30
0 1 1
1 3 15
1 4 4
2 1 3
Sample Output 4
-12

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <queue>
using namespace std;

int N, W;
int M, A[1 << 19], B[1 << 19], C[1 << 19];
bool dp[100009][109];
vector<pair<int, int>> G[100009];
queue<pair<int, int>> Q;

int main() {
	// Step 1. Input
	cin >> N >> M >> W;
	for (int i = 1; i <= M; i++) cin >> A[i] >> B[i] >> C[i];
	for (int i = 1; i <= M; i++) A[i] += 1;
	for (int i = 1; i <= M; i++) B[i] += 1;
	for (int i = 1; i <= M; i++) G[B[i]].push_back(make_pair(A[i], C[i]));

	// Step 2. BFS
	for (int i = 1; i <= M; i++) {
		dp[A[i]][C[i]] = true;
		Q.push(make_pair(A[i], C[i]));
	}
	while (!Q.empty()) {
		int pos1 = Q.front().first;
		int pos2 = Q.front().second;
		Q.pop();
		for (pair<int, int> v : G[pos1]) {
			int to = v.first;
			int cost = v.second;
			if (pos2 + cost > -W) continue;
			if (dp[to][pos2 + cost] == false) {
				dp[to][pos2 + cost] = true;
				Q.push(make_pair(to, pos2 + cost));
			}
		}
	}

	// Step 3. Output
	int Answer = -(1 << 30);
	for (int i = 1; i <= N; i++) {
		for (int j = 0; j < -W; j++) {
			if (dp[i][j] == false) continue;
			Answer = max(Answer, j + W);
		}
	}
	if (Answer == -(1 << 30)) cout << "NA" << endl;
	else cout << Answer << endl;
	return 0;
}
accuracy: 1
time_ms: 870
memory_kb: 33988
final_rank: 8

code_snippet: #include <iostream>
#include <vector>
#include <queue>
using namespace std;

int N, W;
int M, A[1 << 19], B[1 << 19], C[1 << 19];
bool dp[100009][109];
vector<pair<int, int>> G[100009];
queue<pair<int, int>> Q;

int main() {
	// Step 1. Input
	cin >> N >> M >> W;
	for (int i = 1; i <= M; i++) cin >> A[i] >> B[i] >> C[i];
	for (int i = 1; i <= M; i++) A[i] += 1;
	for (int i = 1; i <= M; i++) B[i] += 1;
	for (int i = 1; i <= M; i++) G[B[i]].push_back(make_pair(A[i], C[i]));

	// Step 2. BFS
	for (int i = 1; i <= N; i++) {
		dp[i][0] = true;
		Q.push(make_pair(i, 0));
	}
	while (!Q.empty()) {
		int pos1 = Q.front().first;
		int pos2 = Q.front().second;
		Q.pop();
		for (pair<int, int> v : G[pos1]) {
			int to = v.first;
			int cost = v.second;
			if (pos2 + cost > -W) continue;
			if (dp[to][pos2 + cost] == false) {
				dp[to][pos2 + cost] = true;
				Q.push(make_pair(to, pos2 + cost));
			}
		}
	}

	// Step 3. Output
	int Answer = -(1 << 30);
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j < -W; j++) {
			if (dp[i][j] == false) continue;
			Answer = max(Answer, j + W);
		}
	}
	if (Answer == -(1 << 30)) cout << "NA" << endl;
	else cout << Answer << endl;
	return 0;
}
accuracy: 0.5483870967741935
time_ms: 140
memory_kb: 25748
final_rank: 17

code_snippet: #include <bits/stdc++.h>
#define int long long
#define pt(x) cout << x << endl;
#define Mid ((l + r) / 2)
#define lson (rt << 1)
#define rson (rt << 1 | 1)
using namespace std;
const int N = 2e5 + 1009;
const int M = 2e6 + 1009;
int n, m, w, in[N], f[N][109], ok[N];
int head[N], nxt[M], ver[M], edge[M], tot = 1;
void add(int x, int y, int w) {
	ver[++tot] = y; nxt[tot] = head[x]; head[x] = tot; edge[tot] = w;
}
queue<int> q;
signed main()
{
	ios :: sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m >> w;
	w = -w;
	for(int i = 1; i <= m; i++) {
		int x, y, c;
		cin >> x >> y >> c;
		x += 1; y += 1;
		add(x, y, c);
		assert(c >= 0);
		in[y]++;
	}
	for(int i = 1; i <= n; i++) {
		if(in[i] == 0) {
			q.push(i);
			f[i][0] = 1;
			ok[i] = 1;
		}
	}
	
	while(q.size()) {
		int x = q.front(); q.pop();
		for(int i = head[x]; i; i = nxt[i]) {
			for(int p = 0; p < w; p++) if(p + edge[i] < w && f[x][p] == 1) {
				f[ver[i]][p + edge[i]] |= 1;
			}
			if(edge[i] < w)f[ver[i]][edge[i]] |= 1;
			in[ver[i]]--;
			if(in[ver[i]] == 0) q.push(ver[i]);
		}
	}
	for(int p = w - 1; p >= 0; p--) {
		for(int i = 1; i <= n; i++) {
			if(ok[i] == 1) continue;
			if(f[i][p]) {
				cout << p - w << endl;
				return 0;
			}
		}
	}
	cout << "NA" << endl;
	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 112788
final_rank: 10

code_snippet: #include<iostream>
#include<vector>
#include<cstdio>
#include<queue>
using namespace std;
#define MAXN 100010
#define maxn 500010
int h,e,w,ans;
struct edge{
	int root,w;
};
int out[MAXN],vis[MAXN][100];
vector<edge> in[MAXN];
priority_queue < int,vector<int>,less<int> > val[MAXN];
 
void topo(){
	queue<int> q;
	for(int i=0;i<h;i++){
		if(!out[i]) q.push(i);
	}
	
	while(!q.empty()){
		int f=q.front();
		q.pop();
		
		while(!val[f].empty()){
			for(int i=0;i<in[f].size();i++){
				
				edge x=in[f][i];
				out[x.root]--;
				
				int kk=val[f].top()+x.w;
				if(kk+w<0&&!vis[x.root][kk]){
					vis[x.root][kk]=1;
					ans=max(ans,kk);
					val[x.root].push(kk);
				}
				
				if(!out[x.root]){
					q.push(x.root);
				}
			}
			val[f].pop();
		}
		
//		cout<<f<<endl;
	}
}
int read(){
    int x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}
int main(){
//	freopen("in.txt","r",stdin);
	cin>>h>>e>>w;
	ans=0;
	for(int i=0;i<h;i++){
		val[i].push(0);
	}
	for(int i=0;i<e;i++){
		int s,t,v;
		s=read();
		t=read();
		v=read();
//		scanf("%d%d%d",&s,&t,&v);
		if(v>-w) continue;
		out[s]++;
		edge tmp={s,v};
		in[t].push_back(tmp);
	}
	topo();
	if(ans!=0) cout<<ans+w<<endl;
	else cout<<"NA"<<endl;
	return 0;
}
accuracy: 0.5483870967741935
time_ms: 250
memory_kb: 102500
final_rank: 18

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 100005

struct Edge{
	Edge(int arg_to,int arg_weight){
		to = arg_to;
		weight = arg_weight;
	}
	int to,weight;
};

int V,E,W;
int maximum;
int in_num[SIZE];
bool dp[SIZE][105];
vector<Edge> G[SIZE];

//https://betrue12.hateblo.jp/entry/2020/08/09/150101

int main(){

	scanf("%d %d %d",&V,&E,&W);

	int abs_W = -W;

	for(int i = 0; i < V; i++){

		//dp[終端ノード][重みの総和] := 存在するならtrue
		dp[i][0] = true;
		for(int k = 1; k <= abs_W-1; k++){
			dp[i][k] = false;
		}
	}
	for(int i = 0; i < V; i++){

		in_num[i] = 0;
	}

	int from,to,cost;

	for(int i = 0; i < E; i++){

		scanf("%d %d %d",&from,&to,&cost);
		G[from].push_back(Edge(to,cost));
		in_num[to]++;
	}

	queue<int> Q;
	for(int i = 0; i < V; i++){

		if(in_num[i] == 0){

			Q.push(i);
		}
	}

	int maximum = -BIG_NUM;

	while(!Q.empty()){

		int node_id = Q.front();
		Q.pop();

		for(int i = 0; i <= abs_W-1; i++){

			if(!dp[node_id][i])continue;

			for(int k = 0; k < G[node_id].size(); k++){

				int next_node = G[node_id][k].to;
				int next_cost = i+G[node_id][k].weight;

				if(next_cost >= abs_W)continue;

				maximum = max(maximum,next_cost+W);
				dp[next_node][next_cost] = true;
			}
		}

		for(int i = 0; i < G[node_id].size(); i++){

			int next = G[node_id][i].to;
			in_num[next]--;
			if(in_num[next] == 0){

				Q.push(next);
			}
		}
	}

	if(maximum == -BIG_NUM){

		printf("NA\n");
	}else{

		printf("%d\n",maximum);
	}


	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 23872
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main(){
    int N, M, W;
    cin >> N >> M >> W;
    vector<vector<pair<int, int>>> edges(N);
    vector<int> indeg(N);
    for(int i=0; i<M; i++){
        int a, b, c;
        cin >> a >> b >> c;
        edges[a].emplace_back(b, c);
        indeg[b]++;
    }

    queue<int> que;
    for(int i=0; i<N; i++) if(indeg[i] == 0) que.push(i);
    vector<bitset<101>> dp(N);

    while(que.size()){
        int i = que.front();
        que.pop();
        for(auto& p : edges[i]){
            int j = p.first, c = p.second;
            dp[j] |= dp[i]<<c;
            dp[j][c] = 1;
            indeg[j]--;
            if(indeg[j] == 0) que.push(j);
        }
    }

    int ans = -1000;
    for(int i=0; i<N; i++) for(int j=0; j<abs(W); j++) if(dp[i][j]) ans = max(ans, j+W);
    if(ans == -1000){
        cout << "NA" << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 14980
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main(){
    int N, M, W;
    cin >> N >> M >> W;
    vector<vector<pair<int, int>>> edges(N);
    vector<int> indeg(N);
    for(int i=0; i<M; i++){
        int a, b, c;
        cin >> a >> b >> c;
        edges[a].emplace_back(b, c);
        indeg[b]++;
    }

    queue<int> que;
    for(int i=0; i<N; i++) if(indeg[i] == 0) que.push(i);
    vector<bitset<101>> dp(N);

    while(que.size()){
        int i = que.front();
        que.pop();
        dp[i][0] = 1;
        for(auto& p : edges[i]){
            int j = p.first, c = p.second;
            dp[j] |= dp[i]<<c;
            indeg[j]--;
            if(indeg[j] == 0) que.push(j);
        }
    }

    int ans = -1000;
    for(int i=0; i<N; i++) for(int j=1; j<abs(W); j++) if(dp[i][j]) ans = max(ans, j+W);
    if(ans == -1000){
        cout << "NA" << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}
accuracy: 0.5483870967741935
time_ms: 60
memory_kb: 10104
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

struct graph_scc{
	int V;
	static const int MAX=200000;
	vector<int> G[MAX];
	vector<int> rG[MAX];
	vector<int> vs;
	bool used[MAX];
	int cmp[MAX];
	void init(int n){
		V=n;
		for(int i=0;i<V;i++){
			G[i].clear();
			rG[i].clear();
		}
		vs.clear();
		memset(cmp,0,sizeof(cmp));
	}
	void add_edge(int from,int to){
		G[from].push_back(to);
		rG[to].push_back(from);
	}

	void dfs(int v){
		used[v]=true;
		for(int i=0;i<G[v].size();i++){
			if(!used[G[v][i]])dfs(G[v][i]);
		}
		vs.push_back(v);
	}
	void rdfs(int v,int k){
		used[v]=true;
		cmp[v]=k;
		for(int i=0;i<rG[v].size();i++){
			if(!used[rG[v][i]])rdfs(rG[v][i],k);
		}
	}

	int scc(int n){
		V=n;
		memset(used,0,sizeof(used));
		vs.clear();
		for(int v=0;v<V;v++){
			if(!used[v])dfs(v);
		}
		memset(used,0,sizeof(used));
		int k=0;
		for(int i=vs.size()-1;i>=0;i--){
			if(!used[vs[i]]){
				rdfs(vs[i],k++);
			}
		}
		return k;
	}
};

int n,m,w;
vector<P> G[100005];
graph_scc gr;
int ord[100005];
bool dp[100000][105];

int main(void){
	scanf("%d%d%d",&n,&m,&w);
	bool zero=false;
	for(int i=0;i<m;i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		gr.add_edge(a,b);
		if(c==0)zero=true;
		G[a].push_back(P(b,c));
	}
	gr.scc(n);
	for(int i=0;i<n;i++){
		ord[gr.cmp[i]]=i;
		dp[i][0]=true;
	}
	for(int i=0;i<n;i++){
		int v=ord[i];
		for(int j=0;j<G[v].size();j++){
			int to=G[v][j].first;
			int cost=G[v][j].second;
			for(int k=0;k+cost<=100;k++){
				if(dp[v][k]){
					dp[to][k+cost]=true;
				}
			}
		}
	}
	int ma=-1;
	if(zero)ma=0;
	for(int i=0;i<n;i++){
		for(int j=1;j<-w;j++){
			if(dp[i][j]){
				ma=max(ma,j);
			}
		}
	}
	if(ma==-1){
		printf("NA\n");
	}else{
		printf("%d\n",ma+w);
	}
	return 0;
}
accuracy: 1
time_ms: 320
memory_kb: 46068
final_rank: 4

code_snippet: #include "bits/stdc++.h"
using namespace std;

using Info = bitset<101>;

vector<int> make_dag(vector<vector<int>> edges) {
	const int N = edges.size();
	vector<int> parnum(N);
	for (auto row : edges)for (auto col : row)parnum[col]++;

	queue<int> q;
	vector<int> res;
	for (int i = 0; i < N; i++)if (parnum[i] == 0)q.push(i);

	while (!q.empty()) {
		int v = q.front(); q.pop();
		for (auto u : edges[v]) {
			parnum[u]--;
			if (parnum[u] == 0) {
				q.push(u);
			}
		}
		res.push_back(v);
	}
	return res;
}

int main() {
	int V, E, w;
	cin >> V >> E >> w;
	vector<map<int, int>> edges(V);
	vector<vector<int>> parents(V), fordag(V);

	for (int i = 0; i < E; i++) {
		int s, t, c;
		cin >> s >> t >> c;
		edges[s][t] = c;
		parents[t].push_back(s);
		fordag[s].push_back(t);
	}

	// dag
	vector<int> dag = make_dag(fordag);

	// way
	vector<Info> res(V);
	for (auto& pos : dag) {
		for (int& parent : parents[pos]) {
			int len = edges[parent][pos];
			for (int i = 0; i < 101; i++) if (res[parent][i]) {
				int score = i + len;
				if (score + w < 0)res[pos][score] = true;
			}
			if (len + w < 0)res[pos][len] = true;
		}
	}

	int ans = -1;
	for (const auto& row : res)
		for (int i = 0; i < 101; i++)if (row[i])ans = max(ans, i);

	cout << (ans == -1 ? "NA" : to_string(ans + w)) << endl;
	return 0;
}
accuracy: 1
time_ms: 730
memory_kb: 51888
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

using pi = pair<int,int>;

const int N = 100000;
const int W = 100;
bool vis[N][W];

struct edge{ int to,cost; };
vector<edge> G[N];

const int INF = 19191919;

int main(){
    int n,m,w;
    scanf(" %d %d %d", &n, &m, &w);

    rep(i,m){
        int s,t,c;
        scanf(" %d %d %d", &s, &t, &c);
        G[s].pb({t,c});
    }

    queue<pi> que;
    rep(i,n) que.push({i,0});
    while(!que.empty()){
        pi now = que.front();
        que.pop();

        int v = now.fi, c = now.se;
        for(const auto &e:G[v]){
            int nv = e.to;
            int nc = c+e.cost;
            if(nc<W && !vis[nv][nc]){
                vis[nv][nc] = true;
                que.push({nv,nc});
            }
        }
    }

    int ans = -INF;
    rep(i,n){
        rep(j,-w)if(vis[i][j]) ans = max(ans, j+w);
    }
    if(ans == -INF) printf("NA\n");
    else printf("%d\n", ans);
    return 0;
}
accuracy: 1
time_ms: 1270
memory_kb: 39732
final_rank: 11

code_snippet: /*
 * h.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_W = 100;

/* typedef */

typedef pair<int,int> pii;
typedef vector<pii> vpii;

/* global variables */

int w, ans = 0;
vpii nbrs[MAX_N];
bool ews[MAX_N][MAX_W];

/* subroutines */

void rec(int u) {
  ews[u][0] = true;
  vpii &nbru = nbrs[u];

  for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
    int &v = vit->first, &vc = vit->second;
    if (! ews[v][0]) rec(v);
    int maxi = w - vc;
    for (int i = 0; i < maxi; i++)
      if (ews[v][i]) {
	int wsum = vc + i;
	if (! ews[u][wsum]) {
	  ews[u][wsum] = true;
	  if (ans < wsum) ans = wsum;
	}
      }
  }
}

/* main */

int main() {
  int vn, en;
  cin >> vn >> en >> w;
  w = -w;

  for (int i = 0; i < en; i++) {
    int si, ti, ci;
    cin >> si >> ti >> ci;
    nbrs[si].push_back(pii(ti, ci));
  }

  for (int i = 0; i < vn; i++)
    if (! ews[i][0]) rec(i);

  if (ans == 0) cout << "NA" << endl;
  else printf("%d\n", ans - w);
  return 0;
}
accuracy: 0.5483870967741935
time_ms: 90
memory_kb: 19540
final_rank: 16

code_snippet: /*
 * h.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<bitset>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>

using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_W = 100;

/* typedef */

typedef pair<int,int> pii;
typedef vector<pii> vpii;
typedef bitset<MAX_W> bsw;

/* global variables */

int w, ans = 0;
vpii nbrs[MAX_N];
bsw ews[MAX_N];

/* subroutines */

void rec(int u) {
  ews[u].set(0);
  vpii &nbru = nbrs[u];

  for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
    int &v = vit->first, &vw = vit->second;
    if (ews[v].none()) rec(v);
    int maxi = w - vw;
    for (int i = 0; i < maxi; i++)
      if (ews[v].test(i)) {
	int wsum = vw + i;
	if (! ews[u].test(wsum)) {
	  ews[u].set(wsum);
	  if (ans < wsum) ans = wsum;
	}
      }
  }
}

/* main */

int main() {
  int vn, en;
  cin >> vn >> en >> w;
  w = -w;

  for (int i = 0; i < en; i++) {
    int si, ti, ci;
    cin >> si >> ti >> ci;
    nbrs[si].push_back(pii(ti, ci));
  }

  for (int i = 0; i < vn; i++)
    if (ews[i].none()) rec(i);

  if (ans == 0) cout << "NA" << endl;
  else printf("%d\n", ans - w);
  return 0;
}
accuracy: 0.5483870967741935
time_ms: 100
memory_kb: 12900
final_rank: 14

code_snippet: #include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<list>
#include<queue>
#include<cmath>
#include<functional>
#include<algorithm>
#include<bitset>
#define INF (1<<29)
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;


bool visited[100000];
pair<bitset<100>,int> dp[100000];

int v,e,w;
vector<pair<int,int> > edge[100000];

pair<bitset<100>,int> dfs(int u){
	 if(visited[u])return dp[u];
	visited[u]=true;
	pair<bitset<100>,int> res;
	res.second=-INF;
	rep(i,edge[u].size()){
		pair<int,int> pi=edge[u][i];
		pair<bitset<100>,int> x=dfs(pi.first);
		x.first<<=pi.second;
		res.first|=x.first;
		res.second=max(res.second,x.second);
	}
	for(int i=-w-1;i>=0;i--){
		if(res.first[i]){
			res.second=max(res.second,i+w);
			break;
		}
	}
	res.first.set(0);
	return dp[u]=res;
}



int main(){
	cin>>v>>e>>w;
	rep(i,e){
		int s,t,c;
		cin>>s>>t>>c;
		edge[s].push_back(make_pair(t,c));
	}
	int ans=-INF;
	rep(i,v){
		if(!visited[i])ans=max(ans,dfs(i).second);
	}
	if(ans!=-INF)cout<<ans<<endl;
	else cout<<"NA"<<endl;
	return 0;
}
accuracy: 0.5161290322580645
time_ms: 80
memory_kb: 17168
final_rank: 19

code_snippet: #include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;


int n, m, w;

int dp[100001][101];

main(){
	scanf("%d %d %d", &n, &m, &w);
	vector<vector<pii>> g(n);
	vi deg(n);
	REP(i, m){
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		g[a].emplace_back(b, c);
		deg[b] ++;
	}
	queue<int> que;
	REP(i, n) if(deg[i] == 0) que.push(i);
	int ans = -1;
	while(!que.empty()){
		int u = que.front();
		que.pop();
		FOR(e, g[u]){
			int v = e->first;
			int c = e->second;
			REP(i, -w-c) dp[v][i+c] |= dp[u][i];
			dp[v][c] = 1;
			if(--deg[v] == 0) que.push(v);
		}
		REP(i, -w) if(dp[u][i]) ans = max(ans, i);
	}
	if(ans < 0) puts("NA");
	else printf("%d\n", ans+w);
	return 0;
}
accuracy: 1
time_ms: 270
memory_kb: 50992
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;

bool dp[100100][100];
vector<pii> g[100100];

int main(){
  int v,e,w;
  cin >> v >> e >> w; w*=-1;
  
  vector<int> deg(v,0);
  for(int i=0;i<e;i++){
    int s,t,c;
    cin >> s >> t >> c;
    g[s].push_back(pii(t,c));
    deg[t]++;
  }

  vector<int> to;
  queue<int> q;
  for(int i=0;i<v;i++){
    if(deg[i]==0)q.push(i);
  }

  while(q.size()){
    int x = q.front(); q.pop();
    to.push_back(x);
    for(int i=0;i<(int)g[x].size();i++){
      int u = g[x][i].first;
      deg[u]--;
      if(deg[u]==0)q.push(u);
    }
  }

  memset(dp,0,sizeof(dp));
  for(int i=0;i<v;i++)dp[i][0] = 1;

  int ans = -1;
  for(int i=0;i<v;i++){
    int x = to[i];
    for(int k=0;k<w;k++){
      if(!dp[x][k])continue;

      for(int j=0;j<(int)g[x].size();j++){
	int u = g[x][j].first, cost = k+g[x][j].second;
	if(cost<w){
	  dp[u][cost] = true;
	  ans = max(ans,cost);
	}
      }
    }
  }

  if(ans<0)cout << "NA" << endl;
  else cout << ans - w << endl;
}
accuracy: 1
time_ms: 460
memory_kb: 21788
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;

bool dp[100100][100];
vector<pii> g[100100];

int main(){
  int v,e,w;
  cin >> v >> e >> w; w*=-1;
  
  vector<int> deg(v,0);
  for(int i=0;i<e;i++){
    int s,t,c;
    cin >> s >> t >> c;
    g[s].push_back(pii(t,c));
    deg[t]++;
  }

  vector<int> to;
  queue<int> q;
  for(int i=0;i<v;i++){
    if(deg[i]==0)q.push(i);
  }

  while(q.size()){
    int x = q.front(); q.pop();
    to.push_back(x);
    for(int i=0;i<(int)g[x].size();i++){
      int u = g[x][i].first;
      deg[u]--;
      if(deg[u]==0)q.push(u);
    }
  }

  memset(dp,0,sizeof(dp));
  for(int i=0;i<v;i++)dp[i][0] = 1;

  int ans = 0;
  for(int i=0;i<v;i++){
    int x = to[i];
    for(int k=0;k<w;k++){
      if(!dp[x][k])continue;

      for(int j=0;j<(int)g[x].size();j++){
	int u = g[x][j].first, cost = k+g[x][j].second;
	if(cost<w){
	  dp[u][cost] = true;
	  ans = max(ans,cost);
	}
      }
    }
  }

  if(ans == 0)cout << "NA" << endl;
  else cout << ans - w << endl;
}
accuracy: 0.5483870967741935
time_ms: 100
memory_kb: 17120
final_rank: 15

code_snippet: #include <string>
#include <vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<functional>
#include<list>
#include<deque>
#include<bitset>
#include<set>
#include<map>
#include<cstring>
#include<sstream>
#include<complex>
#define X first
#define Y second
#define pb push_back
#define rep(X,Y) for (int (X) = 0;(X) < int(Y);++(X))
#define rrep(X,Y) for (int (X) = int(Y-1);(X) >=0;--(X))
#define all(X) (X).begin(),(X).end()
#define rall(X) (X).rbegin(),(X).rend()

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int v,e,w,memo[112345],re;
pii cur[112345];
vector<pii> edges[2][112345];
set<int> st[2][112345];

void solve(int p,int mode){
  if(st[mode][p].size())
    return;
  st[mode][p].insert(0);
  rep(i,edges[mode][p].size()){
    pii tmp=edges[mode][p][i];
    solve(tmp.X,mode);
//    st[mode][p].insert(tmp.Y);
    for(auto val:st[mode][tmp.X])
      if(val+tmp.Y<w)
        st[mode][p].insert(val+tmp.Y);
  }
  return;
}

int main(){
  int i,j,k;
  cin>>v>>e>>w;
  w=-w;
  int s,t,c;
  rep(i,e){
    cin>>s>>t>>c;
    edges[0][s].pb(pii(t,c));
    edges[1][t].pb(pii(s,c));
    re=-1;
  }
  rep(i,v)
    solve(i,0);
  rep(i,v)
    solve(i,1);
  rep(i,v){
    for(auto val:st[0][i]){
      auto tmp=st[1][i].lower_bound(w-val);
      if(tmp!=st[1][i].begin()){
        tmp--;
  //      cout<<"-"<<*tmp<<",";
        if(*tmp+val)re=max(re,*tmp+val);
      }
    }
  }
  if(re<0)
    cout<<"NA"<<endl;
  else
    cout<<re-w<<endl;
  return 0;
}
accuracy: 0.5161290322580645
time_ms: 100
memory_kb: 34960
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> edge;
#define to first
#define cost second

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);

  int n, m, w;
  cin >> n >> m >> w;

  vector<int> in(n, 0);
  vector<vector<edge>> G(n);
  for(int i = 0; i < m; ++i) {
    int s, t, c;
    cin >> s >> t >> c;
    G[s].emplace_back(t, c);
    ++in[t];
  }

  const int abs_w = abs(w);

  vector<vector<int>> dp(n, vector<int>(abs_w, false));
  queue<int> que;

  for(int v = 0; v < n; ++v) {
    if(in[v] == 0) que.push(v);
  }

  int ans = INT_MIN;
  while(!que.empty()) {
    const int v = que.front();
    que.pop();

    for(int i = 0; i < abs_w; ++i) {
      if(dp[v][i]) {
	ans = max(ans, i - abs_w);
      }
    }

    for(const auto &e : G[v]) {
      for(int i = abs_w - 1 - e.cost; i >= 0; --i) {
	dp[e.to][i + e.cost] |= (i == 0 ? true : dp[v][i]);
      }

      if(--in[e.to] == 0) {
	que.push(e.to);
      }
    }
  }

  cout << (ans == INT_MIN ? "NA" : to_string(ans)) << endl;

  return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 54588
final_rank: 6

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <bitset>

using namespace std;

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
template <typename T> void print_container(ostream& os, const T& c) { const char* _s = " "; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }
template <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; }

template <typename T> void print(T a, int n, const string& split = " ") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }
template <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }
template <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
#define dump(v) (cerr << #v << ": " << v << endl)

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define clr(a, x) memset(a, x, sizeof(a))
#define sz(a) ((int)(a).size())
#define mp(a, b) make_pair(a, b)
#define ten(n) ((long long)(1e##n))

template <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }
template <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }
template <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }
template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }
template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }
void fast_io() { cin.tie(0); ios::sync_with_stdio(false); }
bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }

typedef long long ll;
typedef pair<int, int> pint;

const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };


int main()
{
    int n, m, w;
    scanf("%d%d%d", &n, &m, &w);
    const int max_x = -w - 1;

    static vector<pint> g[ten(5) + 1919];
    rep(i, m)
    {
        int u, v, c;
        scanf("%d%d%d", &u, &v, &c);
        g[u].push_back(pint(v, c));
    }

    static bitset<128> dp[ten(5) + 1919];
    queue<pint> q;
    rep(i, n)
        q.push(pint(i, 0));
    while (!q.empty())
    {
        int pos = q.front().first;
        int cost = q.front().second;
        q.pop();
//         printf("%d %d\n", pos, cost);

        for (auto& e : g[pos])
        {
            int to = e.first;
            int ncost = cost + e.second;
            if (ncost <= max_x && !dp[to][ncost])
            {
                q.push(pint(to, ncost));
                dp[to][ncost] = true;
            }
        }
    }

    int mx = -1;
    rep(i, n) erep(j, max_x)
        if (dp[i][j])
            upmax(mx, j);
    if (mx == -1)
        puts("NA");
    else
        printf("%d\n", mx + w);
}
accuracy: 1
time_ms: 830
memory_kb: 14800
final_rank: 7

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <bitset>

using namespace std;

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
template <typename T> void print_container(ostream& os, const T& c) { const char* _s = " "; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) os << _s; } } }
template <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; }
template <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; }
template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "(" << p.first << ", " << p.second << ")"; return os; }

template <typename T> void print(T a, int n, const string& split = " ") { for (int i = 0; i < n; i++) { cout << a[i]; if (i + 1 != n) cout << split; } cout << endl; }
template <typename T> void print2d(T a, int w, int h, int width = -1, int br = 0) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; }
template <typename T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
#define dump(v) (cerr << #v << ": " << v << endl)

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define erep(i, n) for (int i = 0; i <= (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define clr(a, x) memset(a, x, sizeof(a))
#define sz(a) ((int)(a).size())
#define mp(a, b) make_pair(a, b)
#define ten(n) ((long long)(1e##n))

template <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }
template <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }
template <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }
template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }
template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }
void fast_io() { cin.tie(0); ios::sync_with_stdio(false); }
bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }

typedef long long ll;
typedef pair<int, int> pint;

const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };


int main()
{
    int n, m, w;
    scanf("%d%d%d", &n, &m, &w);
    const int max_x = -w - 1;

    static vector<pint> g[ten(5) + 1919];
    rep(i, m)
    {
        int u, v, c;
        scanf("%d%d%d", &u, &v, &c);
        g[u].push_back(pint(v, c));
    }

    static bitset<128> dp[ten(5) + 1919];
    queue<pint> q;
    rep(i, n)
    {
        q.push(pint(i, 0));
        dp[i][0] = true;
    }
    while (!q.empty())
    {
        int pos = q.front().first;
        int cost = q.front().second;
        q.pop();
//         printf("%d %d\n", pos, cost);

        for (auto& e : g[pos])
        {
            int to = e.first;
            int ncost = cost + e.second;
            if (ncost <= max_x && !dp[to][ncost])
            {
                q.push(pint(to, ncost));
                dp[to][ncost] = true;
            }
        }
    }

    int mx = 0;
    rep(i, n) erep(j, max_x)
        if (dp[i][j])
            upmax(mx, j);
    if (mx == 0)
        puts("NA");
    else
        printf("%d\n", mx + w);
}
accuracy: 0.5483870967741935
time_ms: 120
memory_kb: 8960
final_rank: 13