#include <cstddef>
#include <limits>
#include <algorithm>
#include <array>
#include <vector>
#include <iostream>
constexpr std::size_t GRAPH_SIZE { 7 };
using Graph = std::vector<std::vector<std::size_t>>;
using ValuesTable = std::array<std::ptrdiff_t, GRAPH_SIZE>;
void makeStep(Graph &graph, ValuesTable &table) {
ValuesTable stepLocalTable { table };
for (std::size_t i { 0 }; i < GRAPH_SIZE; i++)
for (const auto &adj : graph[i])
if (stepLocalTable[i] > 0)
table[adj] += stepLocalTable[i];
}
int main() {
Graph graph {
{ 1, 2, 6 }, { 0, 3 }, { 0, 4 }, { 1, 5 }, { 2, 6, 5 }, { 3, 4 }
};
ValuesTable table {
1, -3, -5, -50, -30, -10, -250
};
for (std::size_t k { 0 }; k < std::numeric_limits<std::size_t>::max(); k++) {
makeStep(graph, table);
if (std::all_of(table.begin(), table.end(), [](auto i) { return i > 0; })) {
std::cout << k;
break;
}
}
}