-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeforces_sleepingschedule.cpp
More file actions
189 lines (162 loc) · 4.96 KB
/
codeforces_sleepingschedule.cpp
File metadata and controls
189 lines (162 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*E. Sleeping Schedule
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vova had a pretty weird sleeping schedule. There are h hours in a day. Vova will sleep exactly n times. The i-th time he will sleep exactly after ai hours from the time he woke up. You can assume that Vova woke up exactly at the beginning of this story (the initial time is 0). Each time Vova sleeps exactly one day (in other words, h hours).
Vova thinks that the i-th sleeping time is good if he starts to sleep between hours l and r inclusive.
Vova can control himself and before the i-th time can choose between two options: go to sleep after ai hours or after ai−1 hours.
Your task is to say the maximum number of good sleeping times Vova can obtain if he acts optimally.
Input
The first line of the input contains four integers n,h,l and r (1≤n≤2000,3≤h≤2000,0≤l≤r<h) — the number of times Vova goes to sleep, the number of hours in a day and the segment of the good sleeping time.
The second line of the input contains n integers a1,a2,…,an (1≤ai<h), where ai is the number of hours after which Vova goes to sleep the i-th time.
Output
Print one integer — the maximum number of good sleeping times Vova can obtain if he acts optimally.
Example
inputCopy
7 24 21 23
16 17 14 20 20 11 22
outputCopy
3
Note
The maximum number of good times in the example is 3.
The story starts from t=0. Then Vova goes to sleep after a1−1 hours, now the time is 15. This time is not good. Then Vova goes to sleep after a2−1 hours, now the time is 15+16=7. This time is also not good. Then Vova goes to sleep after a3 hours, now the time is 7+14=21. This time is good. Then Vova goes to sleep after a4−1 hours, now the time is 21+19=16. This time is not good. Then Vova goes to sleep after a5 hours, now the time is 16+20=12. This time is not good. Then Vova goes to sleep after a6 hours, now the time is 12+11=23. This time is good. Then Vova goes to sleep after a7 hours, now the time is 23+22=21. This time is also good. */
/*#include<iostream>
#define f(i,a,b) for(i=a;i<b;i++)
using namespace std;
int incheck(int a,int b,int s,int l,int r)
{
cout<<"\na[i]="<<a[i]<<"\ts="<<s<<"\n";
cout<<"\nInitial Check:\n";
int y=s+a;
int flag=0;
if(y>24)
{
y-=24;
//cout<<"if y change:"<<y<<"\n";
}
int m=y+b;
if(m>24)
{
m-=24;
//cout<<"if y change:"<<y<<"\n";
}
if(m>l-1 && m<r+1)
{
flag=1;
}
if(y >= l && y <= r || flag == 1)
{
return a;
}
else
{
return a-1;
}
}
main()
{
int n,h,l,r;
cin>>n>>h>>l>>r;
int a[n],i=0;
f(i,0,n) cin>>a[i];
int c=0,s=0,x;
f(i,0,n)
{
x=incheck(a[i],a[i+1],s,l,r);
//cout<<"\nFinal Check:\n";
//cout<<"x:"<<x<<"\n";
s+=x;
//cout<<"s:"<<s<<"\n";
if(s>24)
{
s-=24;
//out<<"if s change:"<<s<<"\n";
}
if(s>=l && s<=r)
{
c++;
//cout<<"c:"<<c<<"\n";
}
//cout<<"\nEND of Loop\n";
}
cout<<c;
}*/
/*#include <bits/stdc++.h>
using namespace std;
bool in(int x, int l, int r) {
return l <= x && x <= r;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, h, l, r;
cin >> n >> h >> l >> r;
vector<int> a(n);
for (auto &it : a) cin >> it;
vector<vector<int>> dp(n + 1, vector<int>(n + 1, INT_MIN));
dp[0][0] = 0;
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += a[i];
for (int j = 0; j <= n; ++j) {
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + in((sum - j) % h, l, r));
if (j < n) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + in((sum - j - 1) % h, l, r));
}
}
cout << *max_element(dp[n].begin(), dp[n].end()) << endl;
return 0;
}*/
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
vector<int> dp;
vector<int> ans;
vector<vector<int>> g;
void dfs(int v, int p = -1) {
dp[v] = a[v];
for (auto to : g[v]) {
if (to == p) continue;
dfs(to, v);
dp[v] += max(dp[to], 0);
}
}
void dfs2(int v, int p = -1) {
ans[v] = dp[v];
for (auto to : g[v]) {
if (to == p) continue;
dp[v] -= max(0, dp[to]);
dp[to] += max(0, dp[v]);
dfs2(to, v);
dp[to] -= max(0, dp[v]);
dp[v] += max(0, dp[to]);
}
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
a = dp = ans = vector<int>(n);
g = vector<vector<int>>(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
if (a[i] == 0) a[i] = -1;
}
for (int i = 0; i < n - 1; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(0);
dfs2(0);
for (auto it : ans) cout << it << " ";
cout << endl;
return 0;
}