File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+ using ll = int64_t ;
5
+ using ff = long double ;
6
+
7
+ int N, M;
8
+ vector<int > A;
9
+ vector<vector<int >> G;
10
+
11
+ int ans;
12
+ vector<bool > done;
13
+ vector<int > maxi;
14
+ void dfs (int v) {
15
+ if (done[v]) return ;
16
+ done[v] = true ;
17
+ maxi[v] = A[v];
18
+ for (int u : G[v]) {
19
+ dfs (u);
20
+ ans = max (ans, maxi[u] - A[v]);
21
+ maxi[v] = max (maxi[u], maxi[v]);
22
+ }
23
+ }
24
+
25
+ int solve () {
26
+ ans = numeric_limits<int >::lowest ();
27
+ done.assign (N, 0 );
28
+ maxi.assign (N, numeric_limits<int >::lowest ());
29
+ for (int v = 0 ; v < N; ++v) {
30
+ if (done[v]) continue ;
31
+ dfs (v);
32
+ }
33
+ return ans;
34
+ }
35
+
36
+ int main () {
37
+ ios_base::sync_with_stdio (false );
38
+ cin.tie (0 ); cout.tie (0 );
39
+
40
+ cin >> N >> M;
41
+ A.assign (N, 0 );
42
+ for (int i = 0 ; i < N; ++i) {
43
+ cin >> A[i];
44
+ }
45
+ G.assign (N, {});
46
+ for (int i = 0 ; i < M; ++i) {
47
+ int a, b;
48
+ cin >> a >> b;
49
+ --a, --b;
50
+ G[a].push_back (b);
51
+ }
52
+ cout << solve () << endl;
53
+
54
+ return 0 ;
55
+ }
You can’t perform that action at this time.
0 commit comments