Problem:
学食
今日はＺ大学のオープンキャンパスです。毎年この日の昼休みには、大勢の高校生たちが学食に列をつくります。そこでＺ大学の事務局は、行列の長さが最大でどのくらいの距離になるかを予測することにしました。事前調査の結果で、以下のことが分かっています。
行列にはそれぞれ 1 から
N
までの番号が振られた
N
人が並びます。
C
個の高校生のペア (
a
i
,
b
i
) それぞれについて、以下の２種類の制約があります：
１つ目の制約は順序に関するもので以下のいずれかです：
a
i
は
b
i
よりも先、または同じ位置に並ばなくてはならない
a
i
は
b
i
よりも後、または同じ位置に並ばなくてはならない
a
i
は
b
i
より先でも、同じ位置でも、後でもよい
２つ目の制約は距離に関するもので以下のいずれかです：
a
i
と
b
i
は
d
i
メートル以上離れなければならない
a
i
と
b
i
は
d
i
メートル以内に並ばなければならない
また、先頭から同じ距離の場所に複数の人が並ぶことができ、行列の先頭には常に番号 1 の人が並ぶことが分かっています。
与えられた
C
個の制約をすべて満たす行列について、先頭から最後尾までの距離が最大となるような並び方をした場合の距離を求めるプログラムを作成してください。ただし、どこまでも離れることができる場合は
inf
と、制約を満たす並び方が不可能な場合は
-1
と出力してください。
入力
入力は以下の形式で与えられる。
N
C
constraint
1
constraint
2
:
constraint
C
1 行目に行列に並ぶ高校生の人数
N
(2 ≤
N
≤ 100) と制約の数
C
(0 ≤
C
≤ 200) が与えられる。続く
C
行に各制約
constraint
i
が次の形式で与えられる。
a
i
o
i
b
i
s
i
d
i
制約には空白は含まれない。
a
i
,
o
i
,
b
i
,
s
i
,
d
i
の意味を以下に示す。
a
i
と
b
i
(1 ≤
a
i
,
b
i
≤
N
かつ
a
i
≠
b
i
) は高校生の番号、
d
i
は距離 (0 ≤
d
≤ 10000) を表す整数である。
o
i
は順序の制約を指定する
<=
、
>=
、
*
のいずれかの文字列であり、
<=
の場合「
a
i
は
b
i
よりも先、または同じ位置に並ばなくてはならない」、
>=
の場合「
a
i
は
b
i
よりも後、または同じ位置に並ばなくてはならない」、
*
の場合「
a
i
は
b
i
より先でも、同じ位置でも、後でもよい」ことを意味する。ただし
o
i
が
*
となる制約は７個以上与えられることはない。
s
i
は距離の制約を指定する
+
または
-
の文字であり、
+
の場合「
a
i
と
b
i
は
d
i
メートル以上離れなければならない」、
-
の場合「
a
i
と
b
i
は
d
i
メートル以内に並ばなければならない」ことを意味する。
ただし、あるペアに対して複数の制約が与えられることはないものとする。
出力
先頭から最後尾までの距離を１行に出力する。
入出力例
入力例１
3 2
1<=2-1
2<=3-2
出力例１
3
入力例２
3 3
1<=2-1
2<=3-2
1<=3+4
出力例２
-1
入力例３
3 2
1<=2-2
2*3+1
出力例３
inf

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n)  for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define popcnt __builtin_popcountll
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=998244353;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}



int main(){
    ll n,m;cin>>n>>m;
    vector<PP> edge(m);
    vp v(m);
    rep(i,m){
        string s;cin>>s;
        ll x=0,y=0;
        while(s[x]!='<'&&s[x]!='>'&&s[x]!='*')x++;
        while(s[y]!='+'&&s[y]!='-')y++;
        ll a=0,b=0,d=0;
        rep(j,x)a=a*10+(s[j]-'0');
        REP(j,x+1,y)if(s[j]!='=')b=b*10+(s[j]-'0');
        REP(j,y+1,s.size())d=d*10+(s[j]-'0');
        edge[i]=PP(a-1,b-1,d);
        if(s[x]=='*')v[i].fi=0;
        if(s[x]=='<')v[i].fi=1;
        if(s[x]=='>')v[i].fi=2;
        if(s[y]=='+')v[i].se=0;
        if(s[y]=='-')v[i].se=1;
    }
    ll ans=-1;
    vvi g(n,vi(n,inf));
    rep(i,n)g[i][0]=0;
    function<void(ll)> dfs=[&](ll l){
        if(l==m){
            vvi dis=g;
            rep(i,n)dis[i][i]=0;
            rep(k,n)rep(i,n)rep(j,n)if(dis[i][k]<inf&&dis[k][j]<inf)chmin(dis[i][j],dis[i][k]+dis[k][j]);
            bool ok=true;
            rep(i,n)if(dis[i][i]<0)ok=false;
            if(ok)rep(i,n)rep(j,n)chmax(ans,dis[i][j]);
            return;
        }
        if(v[l].fi==0||v[l].fi==1){
            ll a,b,d;tie(a,b,d)=edge[l];
            ll t1=g[a][b],t2=g[b][a];
            if(v[l].se==0)chmin(g[b][a],-d);
            else{
                chmin(g[a][b],d);
                chmin(g[b][a],0ll);
            }
            dfs(l+1);
            g[a][b]=t1;g[b][a]=t2;
        }
        if(v[l].fi==0||v[l].fi==2){
            ll a,b,d;tie(a,b,d)=edge[l];swap(a,b);
            ll t1=g[a][b],t2=g[b][a];
            if(v[l].se==0)chmin(g[b][a],-d);
            else{
                chmin(g[a][b],d);
                chmin(g[b][a],0ll);
            }
            dfs(l+1);
            g[a][b]=t1;g[b][a]=t2;
        }
    };dfs(0);
    if(ans==inf)out("inf");
    else out(ans);
}
accuracy: 1
time_ms: 60
memory_kb: 3528
final_rank: 9

code_snippet: #include <bits/stdc++.h>
//#include"atcoder/all"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
//using namespace atcoder;
#define all(a) a.begin(),a.end()
#define int long long
typedef long long ll;
typedef vector<ll> vi;
typedef pair<int,int> P;
typedef pair<P,P> PP;
const ll inf=1ll<<58;
const ll mod=1000000007;
struct edge{
	int from,to,dist;
	string dir;
};
vector<edge> V1,V2;
bool nloop(int n,vector<edge>es){
	vector<int>d(n);
	rep(i,n){
		rep(j,es.size()){
			edge e=es[j];
			if(d[e.to]>d[e.from]+e.dist){
				d[e.to]=d[e.from]+e.dist;
				if(i==n-1)return true;
			}
		}
	}
	return false;
}
signed main(){
	int n,c;cin>>n>>c;
	rep(i,c){
		int a,b,d;
		string o,s;
		
		string S;cin>>S;
		string h = "";
        int k = 0;
        while (k < S.size() && ('0' <= S[k] && S[k] <= '9')) h += S[k++];
        a = stoi(h);
        h = "";
        
        while (k < S.size() && !('0' <= S[k] && S[k] <= '9')) h += S[k++];
        o=h;
        h = "";

        while (k < S.size() && ('0' <= S[k] && S[k] <= '9')) h += S[k++];
        b = stoi(h);
        h = "";

        while (k < S.size() && !('0' <= S[k] && S[k] <= '9')) h += S[k++];
        s=h;
        h = "";

        while (k < S.size() && ('0' <= S[k] && S[k] <= '9')) h += S[k++];
        d = stoi(h);

		if(o=="<=")V1.push_back({a-1,b-1,d,s});
		if(o==">=")V1.push_back({b-1,a-1,d,s});
		if(o=="*")V2.push_back({a-1,b-1,d,s});
	}
	string z="a";
	int ans=-1,kai=0;
	rep(i,1<<V2.size()){
		vector<edge>ES;
		rep(j,V1.size()){
			if(V1[j].dir=="+"){
				ES.push_back({V1[j].to,V1[j].from,-V1[j].dist,z});
			}
			else{
				ES.push_back({V1[j].from,V1[j].to,V1[j].dist,z});
			}
			ES.push_back({V1[j].to,V1[j].from,0,z});
		}
		rep(j,V2.size()){
			if(i&1<<j)swap(V2[j].from,V2[j].to);
		}
		rep(j,V2.size()){
			if(V2[j].dir=="+"){
				ES.push_back({V2[j].to,V2[j].from,-V2[j].dist,z});
			}
			else{
				ES.push_back({V2[j].from,V2[j].to,V2[j].dist,z});
			}		
			ES.push_back({V2[j].to,V2[j].from,0,z});		
		}
		rep(j,V2.size()){
			if(i&1<<j)swap(V2[j].from,V2[j].to);
		}		
		rep(j,n-1){
			ES.push_back({j+1,0,0,z});
		}
		
		vector<int>d(n,inf);
		d[0]=0;

		rep(k,n){
			rep(j,ES.size()){
				edge e=ES[j];
				if(d[e.from]<inf&&d[e.to]>d[e.from]+e.dist){
					d[e.to]=d[e.from]+e.dist;
				}
			}
		}
		int mx=-inf;
		if(!nloop(n,ES)){
			kai=1;
			rep(j,n){
				mx=max(mx,d[j]);
			}
			ans=max(ans,mx);
		}
	}
	if(!kai)cout<<-1<<endl;
	else{
		if(ans==inf)cout<<"inf"<<endl;
		else cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 3548
final_rank: 8

code_snippet: /*
このコード、と～おれ!
Be accepted!
∧＿∧　
（｡･ω･｡)つ━☆・*。
⊂　　 ノ 　　　・゜+.
　しーＪ　　　°。+ *´¨)
 　　　　　　　　　.· ´¸.·*´¨) ¸.·*¨)
		  　　　　　　　　　　(¸.·´ (¸.·'* ☆
*/

#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <vector>
#include <numeric>
#include <iostream>
#include <random>
#include <map>
#include <unordered_map>
#include <queue>
#include <regex>
#include <functional>
#include <complex>
#include <list>
#include <cassert>
#include <iomanip>
#include <set>
#include <stack>
#include <bitset>
#include <array>
#include <chrono>

//#pragma GCC target("arch=skylake-avx512")
#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse4")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define repeat(i, n, m) for(int i = n; i < (m); ++i)
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define printynl(a) printf(a ? "yes\n" : "no\n")
#define printyn(a) printf(a ? "Yes\n" : "No\n")
#define printYN(a) printf(a ? "YES\n" : "NO\n")
#define printim(a) printf(a ? "possible\n" : "imposible\n")
#define printdb(a) printf("%.50lf\n", a)
#define printLdb(a) printf("%.50Lf\n", a)
#define printdbd(a) printf("%.16lf\n", a)
#define prints(s) printf("%s\n", s.c_str())
#define all(x) (x).begin(), (x).end()
#define deg_to_rad(deg) (((deg)/360.0L)*2.0L*PI)
#define rad_to_deg(rad) (((rad)/2.0L/PI)*360.0L)
#define Please return
#define AC 0
#define manhattan_dist(a, b, c, d) (abs(a - c) + abs(b - d))

using ll = long long;
using ull = unsigned long long;

constexpr int INF = 1073741823;
constexpr int MINF = -1073741823;
constexpr ll LINF = ll(4661686018427387903);
constexpr ll MOD = 1e9 + 7;
constexpr ll mod = 998244353;
constexpr long double eps = 1e-6;
const long double PI = acosl(-1.0L);

using namespace std;

void scans(string& str) {
	char c;
	str = "";
	scanf("%c", &c);
	if (c == '\n')scanf("%c", &c);
	while (c != '\n' && c != -1 && c != ' ') {
		str += c;
		scanf("%c", &c);
	}
}

void scanc(char& str) {
	char c;
	scanf("%c", &c);
	if (c == -1)return;
	while (c == '\n') {
		scanf("%c", &c);
	}
	str = c;
}

double acot(double x) {
	return PI / 2 - atan(x);
}

ll LSB(ll n) { return (n & (-n)); }

template<typename T>
inline T chmin(T& a, const T& b) {
	if (a > b)a = b;
	return a;
}

template<typename T>
inline T chmax(T& a, const T& b) {
	if (a < b)a = b;
	return a;
}

//cpp_int
#if __has_include(<boost/multiprecision/cpp_int.hpp>)
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace boost::multiprecision;
#else
using cpp_int = ll;
#endif

//atcoder library
#if __has_include(<atcoder/all>)
#include <atcoder/all>
//using namespace atcoder;
#endif

/*
	random_device seed_gen;
	mt19937 engine(seed_gen());
	uniform_int_distribution dist(1, 100);
*/


/*----------------------------------------------------------------------------------*/

/*
* @title template(graph)
* @docs kyopro/docs/graph_template.md
*/

template<typename T>
struct edge {
	T cost;
	int from, to;

	edge(int from, int to) : from(from), to(to), cost(T(1)) {}
	edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
};

template<typename T = int>
struct graph {

	int n;
	bool directed, weighted;

	vector<vector<edge<T>>> g;

	graph(int n, bool directed, bool weighted) : g(n), n(n), directed(directed), weighted(weighted) {}

	void add_edge(int from, int to, T cost = T(1)) {
		g[from].emplace_back(from, to, cost);
		if (not directed) {
			g[to].emplace_back(to, from, cost);
		}
	}

	vector<edge<T>>& operator[](const int& idx) {
		return g[idx];
	}

	void read(int e, bool one_indexed) {
		int a, b, c = 1;
		while (e--) {
			scanf("%d%d", &a, &b);
			if (weighted) {
				scanf("%d", &c);
			}
			if (one_indexed)--a, --b;
			add_edge(a, b, c);
		}
	}

	void read(int e, bool one_indexed, const string& format) {
		int a, b;
		T c = T(1);
		while (e--) {
			scanf("%d%d", &a, &b);
			if (weighted) {
				scanf(format.c_str(), &c);
			}
			if (one_indexed)--a, --b;
			add_edge(a, b, c);
		}
	}

};

// 戻り値がtrueなら負の閉路を含む
bool bellman_ford(int n, int s, graph<int> g, vector<int> &dis) { // nは頂点数、sは開始頂点
	fill(all(dis), INF);
	dis[s] = 0;
	int cnt = 0;
	while (cnt < n) {
		bool end = true;
		rep(i, n) {
			for (const auto& e : g[i]) {
				if (dis[e.from] != INF && dis[e.from] + e.cost < dis[e.to]) {
					dis[e.to] = dis[e.from] + e.cost;
					end = false;
				}
			}
		}
		if (end) break;
		cnt++;
	}
	return (cnt == n);
}

int solve(const int &n, const vector<tuple<int, int, int>> &m, const vector<tuple<int, int, int>> &p) {
	graph g(n, true, true);
	rep(i, n)g.add_edge(i, 0, 0);
	for (auto [a, b, d] : m) {
		if (d > 0) {
			g.add_edge(b, a, 0);
			g.add_edge(a, b, d);
		}
		else {
			g.add_edge(a, b, 0);
			g.add_edge(b, a, -d);
		}
	}
	for (auto [a, b, d] : p) {
		if (d > 0) {
			g.add_edge(b, a, 0);
			g.add_edge(b, a, -d);
		}
		else {
			g.add_edge(a, b, 0);
			g.add_edge(a, b, d);
		}
	}
	vector d(n, vector<int>(n));
	bool ushi = false;
	rep(i, n) {
		ushi |= bellman_ford(n, i, g, d[i]);
	}
	if (ushi)return -1;
	int ret = -1;
	rep(i, n) {
		rep(j, n) {
			chmax(ret, d[i][j]);
		}
	}
	return ret;
}

int main() {
	
	int n, c;
	scanf("%d%d", &n, &c);
	vector<tuple<bool, int, int, int>> ast;
	vector<tuple<int, int, int>> m, p;
	rep(i, c) {
		int a;
		scanf("%d", &a);
		char c;
		scanf("%c", &c);
		if (c == '*') {
			int b, d;
			scanf("%d%c%d", &b, &c, &d);
			--a, --b;
			ast.emplace_back(c == '-', a, b, d);
		}
		else {
			int b, d;
			char cc;
			scanf("%*c%d%c%d", &b, &cc, &d);
			--a, --b;
			if (c == '>')d *= -1;
			if (cc == '+') p.emplace_back(a, b, d);
			else m.emplace_back(a, b, d);
		}
	}
	int ans = -1;
	rep(i, (1 << ast.size())) {
		vector<tuple<int, int, int>> mm = m, pp = p;
		rep(j, ast.size()) {
			if (i & (1 << j)) {
				if (get<0>(ast[j]))mm.emplace_back(get<1>(ast[j]), get<2>(ast[j]), -get<3>(ast[j]));
				else pp.emplace_back(get<1>(ast[j]), get<2>(ast[j]), -get<3>(ast[j]));
			}
			else {
				if (get<0>(ast[j]))mm.emplace_back(get<1>(ast[j]), get<2>(ast[j]), get<3>(ast[j]));
				else pp.emplace_back(get<1>(ast[j]), get<2>(ast[j]), get<3>(ast[j]));
			}
		}
		chmax(ans, solve(n, mm, pp));
	}
	if (ans == INF)puts("inf");
	else printf("%d\n", ans);

	Please AC;
}
accuracy: 1
time_ms: 290
memory_kb: 3644
final_rank: 12

code_snippet: #include"bits/stdc++.h"
using namespace std;
int N, C;
int a[200], b[200], d[200];
char s[200];
vector<int> c;

int ans = -1;
void solve(int n) {
	if (n < c.size()) {
		solve(n + 1);
		swap(a[c[n]], b[c[n]]);
		solve(n + 1);
		swap(a[c[n]], b[c[n]]);
	}
	else {
		int cost[100];
		cost[0] = 0;
		for (int i = 1; i < N; i++) cost[i] = 1e8;
		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < C; j++) {
				if (s[j] == '-') {
					if (cost[b[j]] > cost[a[j]] + d[j]) cost[b[j]] = cost[a[j]] + d[j];
				}
				else {
					if (cost[a[j]] > cost[b[j]] - d[j]) cost[a[j]] = cost[b[j]] - d[j];
				}
				if (cost[a[j]] > cost[b[j]] + 0) cost[a[j]] = cost[b[j]];
			}
		}
		for (int j = 0; j < C; j++) {
			if (s[j] == '-') {
				if (cost[b[j]] > cost[a[j]] + d[j]) return;
			}
			else {
				if (cost[a[j]] > cost[b[j]] - d[j]) return;
			}
		}
		for (int i = 0; i < N; i++)
			if (cost[i] < 0) return;
		for (int i = 0; i < N; i++) ans = max(ans, cost[i]);
	}
}

int main() {
	cin >> N >> C;
	for (int i = 0; i < C; i++) {
		string S;
		cin >> S;
		int j, k;
		for (j = 0; j < S.size()-1; j++) {
			if (S.substr(j, 2) == "<=") break;
			if (S.substr(j, 2) == ">=") break;
			if (S.substr(j, 1) == "*") break;
		}
		for (k = j + 1; k < S.size(); k++) {
			if (S.substr(k, 1) == "+") break;
			if (S.substr(k, 1) == "-") break;
		}
		a[i] = stoi(S.substr(0, j));
		if (S.substr(j, 1) == "*") {
			b[i] = stoi(S.substr(j + 1, k - (j + 1)));
			c.push_back(i);
		}
		else {
			b[i] = stoi(S.substr(j + 2, k - (j + 2)));
		}
		if (S.substr(j, 2) == ">=") swap(a[i], b[i]);
		a[i]--; b[i]--;
		d[i] = stoi(S.substr(k + 1, S.size() - (k + 1)));
		s[i] = S[k];
	}
	solve(0);
	if (ans == (int)1e8) cout << "inf" << endl;
	else cout << ans << endl;

	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3224
final_rank: 11

code_snippet: #include"bits/stdc++.h"
using namespace std;
int N, C;
int a[200], b[200], d[200];
char s[200];
vector<int> c;

int ans = -1;
void solve(int n) {
	if (n < c.size()) {
		solve(n + 1);
		swap(a[c[n]], b[c[n]]);
		solve(n + 1);
		swap(a[c[n]], b[c[n]]);
	}
	else {
		int cost[100];
		cost[0] = 0;
		for (int i = 1; i < N; i++) cost[i] = 1e8;
		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < C; j++) {
				if (s[j] == '-') {
					if (cost[b[j]] > cost[a[j]] + d[j]) cost[b[j]] = cost[a[j]] + d[j];
				}
				else {
					if (cost[a[j]] > cost[b[j]] - d[j]) cost[a[j]] = cost[b[j]] - d[j];
				}
				if (cost[a[j]] > cost[b[j]] + 0) cost[a[j]] = cost[b[j]];
			}
		}
		for (int j = 0; j < C; j++) {
			if (s[j] == '-') {
				if (cost[b[j]] > cost[a[j]] + d[j]) return;
			}
			else {
				if (cost[a[j]] > cost[b[j]] - d[j]) return;
			}
		}
		for (int i = 0; i < N; i++)
			if (cost[i] < 0) return;
		for (int i = 0; i < N; i++) ans = max(ans, cost[i]);
	}
}

int main() {
	cin >> N >> C;
	for (int i = 0; i < C; i++) {
		string S;
		cin >> S;
		int j, k;
		for (j = 0; j < S.size()-1; j++) {
			if (S.substr(j, 2) == "<=") break;
			if (S.substr(j, 2) == ">=") break;
			if (S.substr(j, 1) == "*") break;
		}
		for (k = j + 1; k < S.size(); k++) {
			if (S.substr(k, 1) == "+") break;
			if (S.substr(k, 1) == "-") break;
		}
		a[i] = stoi(S.substr(0, j));
		if (S.substr(j, 1) == "*") {
			b[i] = stoi(S.substr(j + 1, k - (j + 1)));
			c.push_back(i);
		}
		else {
			b[i] = stoi(S.substr(j + 2, k - (j + 2)));
		}
		if (S.substr(j, 2) == ">=") swap(a[i], b[i]);
		a[i]--; b[i]--;
		d[i] = stoi(S.substr(k + 1, S.size() - (k + 1)));
		s[i] = S[k];
	}
	solve(0);
	if (ans == (int)1e8) cout << "inf" << endl;
	else cout << ans << endl;

	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3212
final_rank: 10

code_snippet: #include"bits/stdc++.h"
using namespace std;
int N, C;
int a[200], b[200], d[200];
char s[200];
vector<int> c;

int ans = -1;
void solve(int n) {
	if (n < c.size()) {
		solve(n + 1);
		swap(a[c[n]], b[c[n]]);
		solve(n + 1);
		swap(a[c[n]], b[c[n]]);
	}
	else {
		int cost[100];
		cost[0] = 0;
		for (int i = 1; i < N; i++) cost[i] = 1e8;
		for (int i = 0; i < 1000000; i++) {
			for (int j = 0; j < C; j++) {
				if (s[j] == '-') {
					if (cost[b[j]] > cost[a[j]] + d[j]) cost[b[j]] = cost[a[j]] + d[j];
				}
				else {
					if (cost[a[j]] > cost[b[j]] - d[j]) cost[a[j]] = cost[b[j]] - d[j];
				}
				if (cost[a[j]] > cost[b[j]] + 0) cost[a[j]] = cost[b[j]];
			}
		}
		for (int j = 0; j < C; j++) {
			if (s[j] == '-') {
				if (cost[b[j]] > cost[a[j]] + d[j]) return;
			}
			else {
				if (cost[a[j]] > cost[b[j]] - d[j]) return;
			}
		}
		for (int i = 0; i < N; i++) ans = max(ans, cost[i]);
	}
}

int main() {
	cin >> N >> C;
	for (int i = 0; i < C; i++) {
		string S;
		cin >> S;
		int j, k;
		for (j = 0; j < S.size()-1; j++) {
			if (S.substr(j, 2) == "<=") break;
			if (S.substr(j, 2) == ">=") break;
			if (S.substr(j, 1) == "*") break;
		}
		for (k = j + 1; k < S.size(); k++) {
			if (S.substr(k, 1) == "+") break;
			if (S.substr(k, 1) == "-") break;
		}
		a[i] = stoi(S.substr(0, j));
		if (S.substr(j, 1) == "*") {
			b[i] = stoi(S.substr(j + 1, k - (j + 1)));
			c.push_back(i);
		}
		else {
			b[i] = stoi(S.substr(j + 2, k - (j + 2)));
		}
		if (S.substr(j, 2) == ">=") swap(a[i], b[i]);
		a[i]--; b[i]--;
		d[i] = stoi(S.substr(k + 1, S.size() - (k + 1)));
		s[i] = S[k];
	}
	solve(0);
	if (ans == (int)1e8) cout << "inf" << endl;
	else cout << ans << endl;

	return 0;
}
accuracy: 0.7727272727272727
time_ms: 250
memory_kb: 3212
final_rank: 13

code_snippet: #include"bits/stdc++.h"
using namespace std;
int N, C;
int a[200], b[200], d[200];
char s[200];
vector<int> c;

int ans = -1;
void solve(int n) {
	if (n < c.size()) {
		solve(n + 1);
		swap(a[c[n]], b[c[n]]);
		solve(n + 1);
		swap(a[c[n]], b[c[n]]);
	}
	else {
		int cost[100];
		cost[0] = 0;
		for (int i = 1; i < N; i++) cost[i] = 1e8;
		for (int i = 0; i < 1000000; i++) {
			for (int j = 0; j < C; j++) {
				if (s[j] == '-') {
					if (cost[b[j]] > cost[a[j]] + d[j]) cost[b[j]] = cost[a[j]] + d[j];
				}
				else {
					if (cost[a[j]] > cost[b[j]] - d[j]) cost[a[j]] = cost[b[j]] - d[j];
				}
			}
		}
		for (int j = 0; j < C; j++) {
			if (s[j] == '-') {
				if (cost[b[j]] > cost[a[j]] + d[j]) return;
			}
			else {
				if (cost[a[j]] > cost[b[j]] - d[j]) return;
			}
		}
		for (int i = 0; i < N; i++) ans = max(ans, cost[i]);
	}
}

int main() {
	cin >> N >> C;
	for (int i = 0; i < C; i++) {
		string S;
		cin >> S;
		int j, k;
		for (j = 0; j < S.size()-1; j++) {
			if (S.substr(j, 2) == "<=") break;
			if (S.substr(j, 2) == ">=") break;
			if (S.substr(j, 1) == "*") break;
		}
		for (k = j + 1; k < S.size(); k++) {
			if (S.substr(k, 1) == "+") break;
			if (S.substr(k, 1) == "-") break;
		}
		a[i] = stoi(S.substr(0, j));
		if (S.substr(j, 1) == "*") {
			b[i] = stoi(S.substr(j + 1, k - (j + 1)));
			c.push_back(i);
		}
		else {
			b[i] = stoi(S.substr(j + 2, k - (j + 2)));
		}
		if (S.substr(j, 2) == ">=") swap(a[i], b[i]);
		a[i]--; b[i]--;
		d[i] = stoi(S.substr(k + 1, S.size() - (k + 1)));
		s[i] = S[k];
	}
	solve(0);
	if (ans == (int)1e8) cout << "inf" << endl;
	else cout << ans << endl;

	return 0;
}
accuracy: 0.4090909090909091
time_ms: 10
memory_kb: 3180
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
/**
vector<int>ar(3);
for(auto&e:ar){
    scanf("%d",&e);
        }
sort(ar.begin(),ar.end())
int sum=accumulate(ar.begin(),ar.end(),0);
**/
//memo d17202 1274246
const int NIL = -1;
const int INF = 1000000000;
 
typedef tuple<int, int, int, bool> Uncertain;
 
int BellmanFord(const vector<vector<int>>& g, int start) {
  vector<int> distance(g.size(), INF);
  distance[start] = 0;
  for(int step = 1; step <= g.size(); ++step) {
    for(int i = 0; i < g.size(); ++i) {
      for(int j = 0; j < g.size(); ++j) {
        if(g[i][j] == INF) continue;
        if(distance[j] <= distance[i] + g[i][j]) continue;
        if(step == g.size()) return NIL;
        distance[j] = distance[i] + g[i][j];
      }
    }
  }
  return *max_element(begin(distance), end(distance));
}
 
int solve(const vector<vector<int>>& g, const vector<Uncertain>& uncertain, bitset<8> bit = 0, int depth = 0) {
  if(depth == uncertain.size()) {
    auto h(g);
    for(int i = 0; i < uncertain.size(); ++i) {
      int u, v, d;
      bool positive;
      tie(u, v, d, positive) = uncertain[i];
      if(bit[i]) swap(u, v);
      h[v][u] = 0;
      if(positive) h[v][u] = -d;
      else         h[u][v] = d;
    }
    return BellmanFord(h, 0);
  }
  return max(solve(g, uncertain, bit.set(depth), depth + 1), solve(g, uncertain, bit.reset(depth), depth + 1));
}
int main(){
    double pai=3.141592653589;
  int N, C;
  cin >> N >> C;
  vector<vector<int>> g(N, vector<int>(N, INF));
  for(int i = 1; i < N; ++i) g[i][0] = 0;
  vector<Uncertain> uncertain;
  for(int i = 0; i < C; ++i) {
    string s;
    cin >> s;
    // parse
    enum {LESS = '<', EQUAL = '*', MORE = '>'};
    int u = 0, v = 0, d = 0;
    char order;
    bool positive;
    int index = 0;
    while(isdigit(s[index])) u = 10 * u + (s[index++] - '0');
    if((order = s[index++]) != EQUAL) ++index;
    while(isdigit(s[index])) v = 10 * v + (s[index++] - '0');
    positive = (s[index++] == '+');
    while(isdigit(s[index])) d = 10 * d + (s[index++] - '0');
    // edge
    --u;
    --v;
    if(order == EQUAL) {
      uncertain.emplace_back(u, v, d, positive);
    } else {
      if(order == MORE) swap(u, v);
      g[v][u] = 0;
      if(positive) g[v][u] = -d;
      else         g[u][v] = d;
    }
  }
  // output
  int answer = solve(g, uncertain);
  if(answer == INF) cout << "inf";
  else              cout << answer;
  cout << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 3280
final_rank: 7

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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<58;
const double eps=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

string split(const string &str, const string &delim){
    string res;
    size_t current = 0, found, delimlen = delim.size();
    while((found = str.find(delim, current)) != string::npos){
        res+=string(str, current, found - current) + " " + delim + " ";
        current = found + delimlen;
    }
    res+=string(str, current, str.size() - current);
    return res;
}

int n,c;
typedef tuple<int,int,int> edge;
vector<edge> es;
vector<int> dist;

void bellmanford(){
    fill_n(dist.begin(),n,inf);
    dist[0]=0;

    for(int i=0; i<2*n; ++i){
        for(edge& e:es){
            int a=get<0>(e),b=get<1>(e),w=get<2>(e);
            if(dist[a]+w<dist[b]) dist[b]=dist[a]+w;
        }
    }
}

void solve(){
    cin >> n >> c;

    dist.resize(n);
    vector<edge> unfixed;

    rep(i,0,c){
        string constraint;
        cin >> constraint;
        constraint=split(constraint,">=");
        constraint=split(constraint,"<=");
        constraint=split(constraint,"*");
        constraint=split(constraint,"+");
        constraint=split(constraint,"-");
        dump(constraint);

        int a,b,d;
        string tmp1,tmp2;
        stringstream ss(constraint);
        ss >> a >> tmp1 >> b >> tmp2 >> d;
        --a; --b;

        if(tmp1=="<=") es.push_back(make_tuple(b,a,0));
        else if(tmp1==">=") es.push_back(make_tuple(a,b,0));

        int w=-d;
        if(tmp2=="-"){
            w*=-1;
            swap(a,b);
        }
        if(tmp1=="<=") es.push_back(make_tuple(b,a,w));
        else if(tmp1==">=") es.push_back(make_tuple(a,b,w));
        else  unfixed.push_back(make_tuple(a,b,w));
    }
    rep(v,1,n) es.push_back(make_tuple(v,0,0));

    vector<edge> es_=es;
    int ans=-1;
    rep(b,0,1<<es.size()){
        es=es_;
        rep(i,0,unfixed.size()){
            edge& e=unfixed[i];
            int a=get<0>(e),b=get<1>(e),w=get<2>(e);
            if(1&(b>>i)){
                es.push_back(make_tuple(a,b,w));
                if(w<0) es.push_back(make_tuple(a,b,0));
                else es.push_back(make_tuple(b,a,0));
            }else{
                es.push_back(make_tuple(b,a,w));
                if(w<0) es.push_back(make_tuple(b,a,0));
                else es.push_back(make_tuple(a,b,0));
            }
        }

        bellmanford();
        if(dist[0]<0) continue;
        rep(i,0,n) ans=max(ans,dist[i]);
    }
    if(ans==inf) cout << "inf" << endl;
    else cout << ans << endl;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	solve();
	return 0;
}
accuracy: 0.2727272727272727
time_ms: 550
memory_kb: 3248
final_rank: 18

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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<58;
const double eps=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

string split(const string &str, const string &delim){
    string res;
    size_t current = 0, found, delimlen = delim.size();
    while((found = str.find(delim, current)) != string::npos){
        res+=string(str, current, found - current) + " " + delim + " ";
        current = found + delimlen;
    }
    res+=string(str, current, str.size() - current);
    return res;
}

int n,c;
typedef tuple<int,int,int> edge;
vector<edge> es;
vector<int> dist;

void bellmanford(){
    fill_n(dist.begin(),n,inf);
    dist[0]=0;

    for(int i=0; i<n; ++i){
        for(edge& e:es){
            int a=get<0>(e),b=get<1>(e),w=get<2>(e);
            if(dist[a]+w<dist[b]) dist[b]=dist[a]+w;
        }
    }
}

void solve(){
    cin >> n >> c;

    dist.resize(n);
    vector<edge> unfixed;

    rep(i,0,c){
        string constraint;
        cin >> constraint;
        constraint=split(constraint,">=");
        constraint=split(constraint,"<=");
        constraint=split(constraint,"*");
        constraint=split(constraint,"+");
        constraint=split(constraint,"-");
        dump(constraint);

        int a,b,d;
        string tmp1,tmp2;
        stringstream ss(constraint);
        ss >> a >> tmp1 >> b >> tmp2 >> d;
        --a; --b;

        if(tmp1=="<=") es.push_back(make_tuple(b,a,0));
        else if(tmp1==">=") es.push_back(make_tuple(a,b,0));

        int w=-d;
        if(tmp2=="-"){
            w*=-1;
            swap(a,b);
        }
        if(tmp1=="<=") es.push_back(make_tuple(b,a,w));
        else if(tmp1==">=") es.push_back(make_tuple(a,b,w));
        else  unfixed.push_back(make_tuple(a,b,w));
    }
    rep(v,1,n) es.push_back(make_tuple(v,0,0));

    vector<edge> es_=es;
    int ans=-1;
    rep(b,0,1<<es.size()){
        es=es_;
        rep(i,0,unfixed.size()){
            edge& e=unfixed[i];
            int a=get<0>(e),b=get<1>(e),w=get<2>(e);
            if(1&(b>>i)){
                es.push_back(make_tuple(a,b,w));
                if(w<0) es.push_back(make_tuple(a,b,0));
                else es.push_back(make_tuple(b,a,0));
            }else{
                es.push_back(make_tuple(b,a,w));
                if(w<0) es.push_back(make_tuple(b,a,0));
                else es.push_back(make_tuple(a,b,0));
            }
        }

        bellmanford();
        if(dist[0]<0) continue;
        rep(i,0,n) ans=max(ans,dist[i]);
    }
    if(ans==inf) cout << "inf" << endl;
    else cout << ans << endl;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	solve();
	return 0;
}
accuracy: 0.2727272727272727
time_ms: 300
memory_kb: 3268
final_rank: 15

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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<58;
const double eps=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

string split(const string &str, const string &delim){
    string res;
    size_t current = 0, found, delimlen = delim.size();
    while((found = str.find(delim, current)) != string::npos){
        res+=string(str, current, found - current) + " " + delim + " ";
        current = found + delimlen;
    }
    res+=string(str, current, str.size() - current);
    return res;
}

int n,c;
typedef tuple<int,int,int> edge;
vector<edge> es;
vector<int> dist;

void bellmanford(){
    fill_n(dist.begin(),n,inf);
    dist[0]=0;

    for(int i=0; i<2*n; ++i){
        for(edge& e:es){
            int a=get<0>(e),b=get<1>(e),w=get<2>(e);
            if(dist[a]+w<dist[b]) dist[b]=dist[a]+w;
        }
    }
}

void solve(){
    cin >> n >> c;

    dist.resize(n);
    vector<edge> unfixed;

    rep(i,0,c){
        string constraint;
        cin >> constraint;
        constraint=split(constraint,">=");
        constraint=split(constraint,"<=");
        constraint=split(constraint,"*");
        constraint=split(constraint,"+");
        constraint=split(constraint,"-");
        dump(constraint);

        int a,b,d;
        string tmp1,tmp2;
        stringstream ss(constraint);
        ss >> a >> tmp1 >> b >> tmp2 >> d;
        --a; --b;

        if(tmp1=="<=") es.push_back(make_tuple(b,a,0));
        else if(tmp1==">=") es.push_back(make_tuple(a,b,0));

        int w=-d;
        if(tmp2=="-"){
            w*=-1;
            swap(a,b);
        }
        if(tmp1=="<=") es.push_back(make_tuple(b,a,w));
        else if(tmp1==">=") es.push_back(make_tuple(a,b,w));
        else  unfixed.push_back(make_tuple(a,b,w));
    }
    rep(v,1,n) es.push_back(make_tuple(v,0,0));

    vector<edge> es_=es;
    int ans=-1;
    rep(b,0,1<<es.size()){
        es=es_;
        rep(i,0,unfixed.size()){
            edge& e=es[i];
            int a=get<0>(e),b=get<1>(e),w=get<2>(e);
            if(1&(b>>i)){
                es.push_back(make_tuple(a,b,w));
                if(w<0) es.push_back(make_tuple(a,b,0));
                else es.push_back(make_tuple(b,a,0));
            }else{
                es.push_back(make_tuple(b,a,w));
                if(w<0) es.push_back(make_tuple(b,a,0));
                else es.push_back(make_tuple(a,b,0));
            }
        }

        bellmanford();
        if(dist[0]<0) continue;
        rep(i,0,n) ans=max(ans,dist[i]);
    }
    if(ans==inf) cout << "inf" << endl;
    else cout << ans << endl;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	solve();
	return 0;
}
accuracy: 0.2727272727272727
time_ms: 620
memory_kb: 3264
final_rank: 19

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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<58;
const double eps=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

string split(const string &str, const string &delim){
    string res;
    size_t current = 0, found, delimlen = delim.size();
    while((found = str.find(delim, current)) != string::npos){
        res+=string(str, current, found - current) + " " + delim + " ";
        current = found + delimlen;
    }
    res+=string(str, current, str.size() - current);
    return res;
}

int n,c;
typedef tuple<int,int,int> edge;
vector<edge> es;
vector<int> dist;

void bellmanford(){
    fill_n(dist.begin(),n,inf);
    dist[0]=0;

    for(int i=0; i<n; ++i){
        for(edge& e:es){
            int a=get<0>(e),b=get<1>(e),w=get<2>(e);
            if(dist[a]+w<dist[b]) dist[b]=dist[a]+w;
        }
    }
}

void solve(){
    cin >> n >> c;

    dist.resize(n);
    vector<edge> unfixed;

    rep(i,0,c){
        string constraint;
        cin >> constraint;
        constraint=split(constraint,">=");
        constraint=split(constraint,"<=");
        constraint=split(constraint,"*");
        constraint=split(constraint,"+");
        constraint=split(constraint,"-");
        dump(constraint);

        int a,b,d;
        string tmp1,tmp2;
        stringstream ss(constraint);
        ss >> a >> tmp1 >> b >> tmp2 >> d;
        --a; --b;

        if(tmp1=="<=") es.push_back(make_tuple(b,a,0));
        else if(tmp1==">=") es.push_back(make_tuple(a,b,0));

        int w=-d;
        if(tmp2=="-"){
            w*=-1;
            swap(a,b);
        }
        if(tmp1=="<=") es.push_back(make_tuple(b,a,w));
        else if(tmp1==">=") es.push_back(make_tuple(a,b,w));
        else  unfixed.push_back(make_tuple(a,b,w));
    }
    rep(v,1,n) es.push_back(make_tuple(v,0,0));

    vector<edge> es_=es;
    int ans=-1;
    rep(b,0,1<<es.size()){
        es=es_;
        rep(i,0,unfixed.size()){
            edge& e=es[i];
            int a=get<0>(e),b=get<1>(e),w=get<2>(e);
            if(1&(b>>i)){
                es.push_back(make_tuple(a,b,w));
                if(w<0) es.push_back(make_tuple(a,b,0));
                else es.push_back(make_tuple(b,a,0));
            }else{
                es.push_back(make_tuple(b,a,w));
                if(w<0) es.push_back(make_tuple(b,a,0));
                else es.push_back(make_tuple(a,b,0));
            }
        }

        bellmanford();
        if(dist[0]<0) continue;
        rep(i,0,n) ans=max(ans,dist[i]);
    }
    if(ans==inf) cout << "inf" << endl;
    else cout << ans << endl;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	solve();
	return 0;
}
accuracy: 0.2727272727272727
time_ms: 330
memory_kb: 3264
final_rank: 17

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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
//template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<58;
const double eps=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

string split(const string &str, const string &delim){
    string res;
    size_t current = 0, found, delimlen = delim.size();
    while((found = str.find(delim, current)) != string::npos){
        res+=string(str, current, found - current) + " " + delim + " ";
        current = found + delimlen;
    }
    res+=string(str, current, str.size() - current);
    return res;
}

int n,c;
typedef tuple<int,int,int> edge;
vector<edge> es;
vector<int> dist;

void bellmanford(){
    fill_n(dist.begin(),n,inf);
    dist[0]=0;

    for(int i=0; i<n; ++i){
        for(edge& e:es){
            int a=get<0>(e),b=get<1>(e),w=get<2>(e);
            if(dist[a]+w<dist[b]) dist[b]=dist[a]+w;
        }
    }
}

void solve(){
    cin >> n >> c;

    dist.resize(n);
    vector<edge> unfixed;

    rep(i,0,c){
        string constraint;
        cin >> constraint;
        constraint=split(constraint,">=");
        constraint=split(constraint,"<=");
        constraint=split(constraint,"*");
        constraint=split(constraint,"+");
        constraint=split(constraint,"-");
        dump(constraint);

        int a,b,d;
        string tmp1,tmp2;
        stringstream ss(constraint);
        ss >> a >> tmp1 >> b >> tmp2 >> d;
        --a; --b;

        if(tmp1=="<=") es.push_back(make_tuple(b,a,0));
        else if(tmp1==">=") es.push_back(make_tuple(a,0,b));

        int w=-d;
        if(tmp2=="-"){
            w*=-1;
            swap(a,b);
        }
        if(tmp1=="<=") es.push_back(make_tuple(b,a,w));
        else if(tmp1==">=") es.push_back(make_tuple(a,b,w));
        else  unfixed.push_back(make_tuple(a,b,w));
    }
    rep(v,1,n) es.push_back(make_tuple(v,0,0));

    vector<edge> es_=es;
    int ans=-1;
    rep(b,0,1<<es.size()){
        es=es_;
        rep(i,0,unfixed.size()){
            edge& e=es[i];
            int a=get<0>(e),b=get<1>(e),w=get<2>(e);
            if(1&(b>>i)){
                es.push_back(make_tuple(a,b,w));
                if(w<0) es.push_back(make_tuple(a,b,0));
                else es.push_back(make_tuple(b,a,0));
            }else{
                es.push_back(make_tuple(b,a,w));
                if(w<0) es.push_back(make_tuple(b,a,0));
                else es.push_back(make_tuple(a,b,0));
            }
        }

        bellmanford();
        if(dist[0]<0) continue;
        rep(i,0,n) ans=max(ans,dist[i]);
    }
    if(ans==inf) cout << "inf" << endl;
    else cout << ans << endl;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	solve();
	return 0;
}
accuracy: 0.2727272727272727
time_ms: 320
memory_kb: 3248
final_rank: 16

code_snippet: // AOJ 0304 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0304)
#include<cctype>
#include<iostream>
#include<string>
#include<vector>
#include<limits>
#define rep(i,a) for(int i=0;i<(a);++i)

const int MAX_N = 210, MAX_C = 210, INF = std::numeric_limits<int>::max()>>2;

struct edge
{
	int from, to, cost;

	edge( int from, int to, int cost )
	:	from(from), to(to), cost(cost)
	{}
};

struct Dat
{
	int a, b, d;
	bool pm;

	Dat( int a, int b, int d, bool pm )
	:	a(a), b(b), d(d), pm(pm)
	{}
};

int N, C;
std::string cst[MAX_C];
std::vector<edge> es;
std::vector<Dat> dats;
int d[MAX_N];

int read( int &p, const std::string &s )
{
	int ret = 0;

	while( p < s.size() && isdigit(s[p]) )
		ret *= 10, ret += s[p++]-'0';

	return ret;
}

int bellmanFord()
{
	std::fill( d, d+N, INF );
	d[0] = 0;
	
	rep( j, N )
	{
		rep( i, es.size() ) if( d[es[i].from] != INF && d[es[i].from]+es[i].cost < d[es[i].to] )
		{
			d[es[i].to] = d[es[i].from]+es[i].cost;

			if( j == N-1 )
				return -INF;
		}
	}

	int ret = -INF;
	rep( j, N )
		ret = std::max( ret, d[j] );

	return ret;
}

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

	std::cin >> N >> C;
	rep( i, C )
	{
		std::cin >> cst[i];

		int p = 0, t = 0, d = 0;
		bool pm;

		int a = read( p, cst[i] );
			
		if( cst[i][p] == '<' )
			t = 0, p += 2;
		else if( cst[i][p] == '>' )
			t = 1, p += 2;
		else if( cst[i][p] == '*' )
			t = 2, ++p;

		int b = read( p, cst[i] );
		pm = cst[i][p++]=='+';
		d = read( p, cst[i] );

		--a; --b;

		es.push_back( edge( a, 0, 0 ) );
		es.push_back( edge( b, 0, 0 ) );
	
		if( !t )
		{
			es.push_back( edge( b, a, 0 ) );
			es.push_back( pm?edge( b, a, -d ):edge( a, b, d ) );
		}
		else if( t == 1 )
		{
			es.push_back( edge( a, b, 0 ) );
			es.push_back( pm?edge( a, b, -d ):edge( b, a, d ) );
		}
		else if( t == 2 )
			dats.push_back( Dat( a, b, d, pm ) );
	}

	int ans = -INF;

	if( dats.empty() )
		ans = std::max( ans, bellmanFord() );

	rep( i, 1<<(dats.size()) )
	{
		rep( j, dats.size() )
		{
			if( i>>j&1 )
			{
				es.push_back( edge( dats[j].b, dats[j].a, 0 ) );
				es.push_back( dats[j].pm?edge( dats[j].b, dats[j].a, -dats[j].d ):edge( dats[j].a, dats[j].b, dats[j].d ) );
			}
			else
			{
				es.push_back( edge( dats[j].a, dats[j].b, 0 ) );
				es.push_back( dats[j].pm?edge( dats[j].a, dats[j].b, -dats[j].d ):edge( dats[j].b, dats[j].a, dats[j].d ) );
			}
		}

		 ans = std::max( ans, bellmanFord() );

		 rep( j, dats.size()*2 )
			es.pop_back();
	}

	if( ans == INF )
		std::cout << "inf" << std::endl;
	else
		std::cout << (ans>=0?ans:-1) << std::endl;

	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1236
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
/*
dist[a] + w >= dist[b]

a ??? b ????????????:
dist[b] + 0 >= dist[a]

d ??????????????\???:
dist[b] - d >= dist[a]

d ??????????????\???:
dist[a] + d >= dist[b]
*/

const int INF = 1 << 28;

struct Edge {
	int src, dst, cost;
	Edge(){}
	Edge(int src, int dst, int cost): src(src), dst(dst), cost(cost){}
};

int n, c;
vector<Edge> es;
vector<int> any;
int dist[100][100];

int calc()
{
	fill_n(*dist, 100 * 100, INF);
	for (int i = 0; i < 100; i++) dist[i][i] = 0;
	for (auto &e : es) dist[e.src][e.dst] = min(dist[e.src][e.dst], e.cost);
	
	for (int k = 0; k < n; k++){
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}
	
	for (int i = 0; i < n; i++) if (dist[i][i] < 0) return -1;
	int res = 0;
	for (int i = 0; i < n; i++) res = max(res, dist[0][i]);
	
	return res;
}

int main()
{
	scanf("%d %d", &n, &c);
	for (int i = 0; i < c; i++){
		int a, b, d;
		char o, s;
		
		scanf("%d", &a); --a;
		scanf("%c", &o);
		if (o != '*') scanf("%*c");
		else any.push_back(i);
		scanf("%d%c%d", &b, &s, &d); --b;
		
		if (o == '>') swap(a, b);
		es.push_back(Edge(b, a, 0));
		if (s == '+') es.push_back(Edge(b, a, -d));
		else es.push_back(Edge(a, b, d));
	}
	for (int i = 1; i < n; i++){
		es.push_back(Edge(i, 0, 0));
	}
	
	//for (auto e : es) printf("%d -> %d %d\n", e.src, e.dst, e.cost);
	
	int res = -1;
	for (int i = 0; i < (1 << any.size()); i++){
		for (int j = 0; j < any.size(); j++){
			if (i & (1 << j)){
				swap(es[any[j] * 2].src, es[any[j] * 2].dst);
				swap(es[any[j] * 2 + 1].src, es[any[j] * 2 + 1].dst);
			}
		}
		res = max(res, calc());
		for (int j = 0; j < any.size(); j++){
			if (i & (1 << j)){
				swap(es[any[j] * 2].src, es[any[j] * 2].dst);
				swap(es[any[j] * 2 + 1].src, es[any[j] * 2 + 1].dst);
			}
		}
	}
	if (res == INF) puts("inf");
	else printf("%d\n", res);
}
accuracy: 1
time_ms: 70
memory_kb: 1244
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;

struct Edge {
	int a, b, w;
	Edge(){}
	Edge(int a, int b, int w): a(a), b(b), w(w){}
};

int n, c;
Edge g[400];
int dist[100][100];

int solve()
{
	fill_n(*dist, 100 * 100, INF);
	for (int i = 0; i < n; i++) dist[i][i] = 0;
	
	for (int i = 0; i < c * 2; i++){
		int &t = dist[g[i].a][g[i].b];
		t = min(t, g[i].w);
		//printf("%d -> %d %d\n", g[i].a, g[i].b, g[i].w);
	}
	
	for (int k = 0; k < n; k++){
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}
	
	int res = -INF;
	for (int i = 0; i < n; i++){
		if (dist[i][i] < 0){
			return -1;
		}
		if (dist[0][i] < 0){
			return -1;
		}
		res = max(res, dist[0][i]);
	}
	
	return res;
}

int main()
{
	scanf("%d %d", &n, &c);
	vector<int> v;
	
	for (int i = 0; i < c; i++){
		int a, b, w;
		char ch, sg;
		scanf("%d", &a);
		scanf("%c", &ch);
		if (ch != '*') scanf("%*c");
		else v.push_back(i);
		scanf("%d", &b);
		if (ch == '>') swap(a, b);
		scanf("%c", &sg);
		scanf("%d", &w);
		--a; --b;
		
		if (sg == '-') g[i] = Edge(a, b, w);
		else g[i] = Edge(b, a, -w);
		g[i + c] = Edge(b, a, 0);
	}
	
	int res = -1;
	
	for (int x = 0; x < (1 << v.size()); x++){
		for (int k = 0; k < v.size(); k++){
			if (x & (1 << k)){
				swap(g[v[k]].a, g[v[k]].b);
				swap(g[v[k] + c].a, g[v[k] + c].b);
			}
		}
		
		res = max(res, solve());
		
		for (int k = 0; k < v.size(); k++){
			if (x & (1 << k)){
				swap(g[v[k]].a, g[v[k]].b);
				swap(g[v[k] + c].a, g[v[k] + c].b);
			}
		}
	}
	
	if (res == INF) puts("inf");
	else printf("%d\n", res);
}
accuracy: 1
time_ms: 70
memory_kb: 1256
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;

struct Edge {
	int a, b, w;
	Edge(){}
	Edge(int a, int b, int w): a(a), b(b), w(w){}
};

int n, c;
Edge g[400];
int dist[100][100];

int solve()
{
	fill_n(*dist, 100 * 100, INF);
	for (int i = 0; i < n; i++) dist[i][i] = 0;
	
	for (int i = 0; i < c * 2; i++){
		int &t = dist[g[i].a][g[i].b];
		t = min(t, g[i].w);
		//printf("%d -> %d %d\n", g[i].a, g[i].b, g[i].w);
	}
	
	for (int k = 0; k < n; k++){
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}
	
	int res = -INF;
	for (int i = 0; i < n; i++){
		if (dist[i][i] < 0){
			return -1;
		}
		if (dist[0][i] < 0){
			return -1;
		}
		res = max(res, dist[0][i]);
	}
	
	return res;
}

int main()
{
	scanf("%d %d", &n, &c);
	vector<int> v;
	
	for (int i = 0; i < c; i++){
		int a, b, w;
		char ch, sg;
		scanf("%d", &a);
		scanf("%c", &ch);
		if (ch != '*') scanf("%*c");
		else v.push_back(i);
		scanf("%d", &b);
		if (ch == '>') swap(a, b);
		scanf("%c", &sg);
		scanf("%d", &w);
		--a; --b;
		
		if (sg == '-') g[i] = Edge(a, b, w);
		else g[i] = Edge(b, a, -w);
		g[i + c] = Edge(b, a, 0);
	}
	
	int res = -1;
	
	for (int x = 0; x < (1 << v.size()); x++){
		for (int k = 0; k < v.size(); k++){
			if (x & (1 << k)){
				swap(g[v[k]].a, g[v[k]].b);
				swap(g[v[k] + c].a, g[v[k] + c].b);
			}
		}
		
		res = max(res, solve());
		
		for (int k = 0; k < v.size(); k++){
			if (x & (1 << k)){
				swap(g[v[k]].a, g[v[k]].b);
				swap(g[v[k] + c].a, g[v[k] + c].b);
			}
		}
	}
	
	if (res > INF / 2) puts("inf");
	else printf("%d\n", res);
}
accuracy: 1
time_ms: 70
memory_kb: 1256
final_rank: 3

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

const int NIL = -1;
const int INF = 1000000000;

typedef tuple<int, int, int, bool> Uncertain;

int WarshallFloyd(vector<vector<int>> g, int start) {
  for(int i = 0; i < g.size(); ++i) g[i][i] = 0;
  for(int k = 0; k < g.size(); ++k)
    for(int i = 0; i < g.size(); ++i)
      for(int j = 0; j < g.size(); ++j)
        g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
  for(int i = 0; i < g.size(); ++i) if(g[i][i] < 0) return NIL;
  return *max_element(begin(g[start]), end(g[start]));
}

int solve(const vector<vector<int>>& g, const vector<Uncertain>& uncertain, bitset<8> bit = 0, int depth = 0) {
  if(depth == uncertain.size()) {
    auto h(g);
    for(int i = 0; i < uncertain.size(); ++i) {
      int u, v, d;
      bool positive;
      tie(u, v, d, positive) = uncertain[i];
      if(bit[i]) swap(u, v);
      h[v][u] = 0;
      if(positive) h[v][u] = -d;
      else         h[u][v] = d;
    }
    return WarshallFloyd(h, 0);
  }
  return max(solve(g, uncertain, bit.set(depth), depth + 1), solve(g, uncertain, bit.reset(depth), depth + 1));
}

int main() {
  int N, C;
  cin >> N >> C;
  vector<vector<int>> g(N, vector<int>(N, INF));
  for(int i = 1; i < N; ++i) g[i][0] = 0;
  vector<Uncertain> uncertain;
  for(int i = 0; i < C; ++i) {
    string s;
    cin >> s;
    // parse
    enum {LESS = '<', EQUAL = '*', MORE = '>'};
    int u = 0, v = 0, d = 0;
    char order;
    bool positive;
    int index = 0;
    while(isdigit(s[index])) u = 10 * u + (s[index++] - '0');
    if((order = s[index++]) != EQUAL) ++index;
    while(isdigit(s[index])) v = 10 * v + (s[index++] - '0');
    positive = (s[index++] == '+');
    while(isdigit(s[index])) d = 10 * d + (s[index++] - '0');
    // edge
    --u;
    --v;
    if(order == EQUAL) {
      uncertain.emplace_back(u, v, d, positive);
    } else {
      if(order == MORE) swap(u, v);
      g[v][u] = 0;
      if(positive) g[v][u] = -d;
      else         g[u][v] = d;
    }
  }
  // output
  int answer = solve(g, uncertain);
  if(answer == INF) cout << "inf";
  else              cout << answer;
  cout << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 1360
final_rank: 6

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

const int NIL = -1;
const int INF = 1000000000;

typedef tuple<int, int, int, bool> Uncertain;

int BellmanFord(const vector<vector<int>>& g, int start) {
  vector<int> distance(g.size(), INF);
  distance[start] = 0;
  for(int step = 1; step <= g.size(); ++step) {
    for(int i = 0; i < g.size(); ++i) {
      for(int j = 0; j < g.size(); ++j) {
        if(g[i][j] == INF) continue;
        if(distance[j] <= distance[i] + g[i][j]) continue;
        if(step == g.size()) return NIL;
        distance[j] = distance[i] + g[i][j];
      }
    }
  }
  return *max_element(begin(distance), end(distance));
}

int solve(const vector<vector<int>>& g, const vector<Uncertain>& uncertain, bitset<8> bit = 0, int depth = 0) {
  if(depth == uncertain.size()) {
    auto h(g);
    for(int i = 0; i < uncertain.size(); ++i) {
      int u, v, d;
      bool positive;
      tie(u, v, d, positive) = uncertain[i];
      if(bit[i]) swap(u, v);
      h[v][u] = 0;
      if(positive) h[v][u] = -d;
      else         h[u][v] = d;
    }
    return BellmanFord(h, 0);
  }
  return max(solve(g, uncertain, bit.set(depth), depth + 1), solve(g, uncertain, bit.reset(depth), depth + 1));
}

int main() {
  int N, C;
  cin >> N >> C;
  vector<vector<int>> g(N, vector<int>(N, INF));
  for(int i = 1; i < N; ++i) g[i][0] = 0;
  vector<Uncertain> uncertain;
  for(int i = 0; i < C; ++i) {
    string s;
    cin >> s;
    // parse
    enum {LESS = '<', EQUAL = '*', MORE = '>'};
    int u = 0, v = 0, d = 0;
    char order;
    bool positive;
    int index = 0;
    while(isdigit(s[index])) u = 10 * u + (s[index++] - '0');
    if((order = s[index++]) != EQUAL) ++index;
    while(isdigit(s[index])) v = 10 * v + (s[index++] - '0');
    positive = (s[index++] == '+');
    while(isdigit(s[index])) d = 10 * d + (s[index++] - '0');
    // edge
    --u;
    --v;
    if(order == EQUAL) {
      uncertain.emplace_back(u, v, d, positive);
    } else {
      if(order == MORE) swap(u, v);
      g[v][u] = 0;
      if(positive) g[v][u] = -d;
      else         g[u][v] = d;
    }
  }
  // output
  int answer = solve(g, uncertain);
  if(answer == INF) cout << "inf";
  else              cout << answer;
  cout << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 1316
final_rank: 5