#include using namespace std; #define all(v) (v).begin(),(v).end() #define pb(a) push_back(a) #define rep(i, n) for(long long i=0;i dist(lower, upper); return dist(engine); } std::vector next(int n, int lower, int upper) { std::vector a(n); for(auto& e: a) e = next(lower, upper); return std::move(a); } bool next_bool() { return next(0, 1); } // [lower, upper] 中の実数から一様ランダムに選ぶ。 double next_double(double lower, double upper) { std::uniform_real_distribution<> dist(lower, upper); return dist(engine); } // char char next_char_lower_case() { return next('a', 'z'); } char next_char_upper_case() { return next('A', 'Z'); } char next_char() { if(next_bool()) return next_char_lower_case(); return next_char_upper_case(); } char next_char(const std::string& chars) { assert(not chars.empty()); return chars[next(0, chars.size() - 1)]; } // string std::string next_string_lower_case(const int len) { std::string s(len, ' '); for(auto& c: s) c = next_char_lower_case(); return std::move(s); } std::string next_string_upper_case(const int len) { std::string s(len, ' '); for(auto& c: s) c = next_char_upper_case(); return std::move(s); } std::string next_string(const int len) { std::string s(len, ' '); for(auto& c: s) c = next_char(); return std::move(s); } std::string next_string(const int len, const std::string& chars) { std::string s(len, ' '); for(auto& c: s) c = next_char(chars); return std::move(s); } // 頂点数 order の木を生成する。 // p[i] := 頂点 i + 1 の親 std::vector next_tree(const int order) { std::vector p(order - 1); for(size_t i = 0; i < p.size(); i++) p[i] = next(0, i); return std::move(p); } // 頂点数 order 辺数 size のグラフを生成する。 std::vector> next_graph(const int order, const int size, bool is_connected = true, bool is_simple = true) { std::vector> edges; std::set> edges_set; if(is_connected) { assert(order - 1 <= size); auto p = next_tree(order); for(size_t i = 0; i < p.size(); i++) { edges.push_back({p[i], i + 1}); edges_set.insert({p[i], i + 1}); edges_set.insert({i + 1, p[i]}); } } while((int)edges.size() != size) { if(is_simple) { int a = next(0, order - 1); int b = next(a + 1, order - 1); if(edges_set.count({a, b})) continue; edges.push_back({a, b}); edges_set.insert({a, b}); edges_set.insert({b, a}); } else { int a = next(0, order - 1); int b = next(0, order - 1); edges.push_back({a, b}); edges_set.insert({a, b}); edges_set.insert({b, a}); } } return std::move(edges); } // 頂点数 order のなもりグラフを生成する。 std::vector> next_namori(const int order) { return next_graph(order, order); } }; // random_devicer rnd(seed); // rnd.next(0, n) [0, n] bool flag = 0; //bellman_ford template std::vector bellman_ford(T &G, int start, int lim = 40000){ random_devicer rnd(0); // T -> vector>> // edge pair -> pair int n = G.size(); std::vector dis(n, (1LL << 59)); dis[start] = 0; for(int i = 0; i < lim; i ++) { bool update = 0; for(int j = 0; j < n; j ++) { for(auto [cost, to] : G[j]) { if(dis[j] + cost < dis[to]) { dis[to] = dis[j] + cost; if(i >= 20000) { if(dis[to] < (1LL << 30) * n) { flag = 1; return dis; } } update = 1; } } } if(!update) break; } return dis; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; vector a(n); rep(i, n) cin >> a[i]; vector>> v(n * 2); rep(i, m) { ll x, y, c; cin >> x >> y >> c; x --; y --; v[x * 2 + 1].push_back({c, y * 2}); } rep(i, n) v[i*2].push_back({-a[i], i*2+1}); auto ret = bellman_ford(v, 0); if(flag) { cout << "inf" << endl; return 0; } assert(-(1LL << 30) * n <= ret.back() and ret.back() <= (1LL << 30) * n); cout << - ret.back() << endl; return 0; }