File tree Expand file tree Collapse file tree
Problems/Data-structures/Day-04/sol/Sujal_Kshatri Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // submission link - https://codeforces.com/problemset/submission/1912/355802995
2+
3+ /*
4+ Approach:
5+
6+ We start with a number `num`.
7+
8+ From each list, we take numbers from left to right.
9+ If numbers go down first but later go up, we save that part as a "good chunk".
10+
11+ For every good chunk, we remember:
12+ -how much `num` we need to start it
13+ - how much `num` increases after it
14+
15+ Then we sort all chunks by how much `num` they need.
16+ We take chunks one by one if we can afford them.
17+
18+ This way, `num` grows as much as possible.
19+ */
20+
21+ #include < bits/stdc++.h>
22+ using namespace std ;
23+
24+ int main ()
25+ {
26+ long long num,k;
27+ cin >> num >> k;
28+ long long i,j;
29+ vector<pair<long long ,long long >> v;
30+ for (i=0 ;i<k;i++)
31+ {
32+ long long n;
33+ cin >> n;
34+ long long maxi=0 ,add=0 ;
35+ for (j=0 ;j<n;j++)
36+ {
37+ long long x;
38+ cin >> x;
39+ add += x;
40+ maxi = max (maxi,-add);
41+ if (add>0 )
42+ {
43+ v.push_back ({maxi,add});
44+ add=0 ;
45+ }
46+ }
47+ }
48+ sort (v.begin (),v.end ());
49+ for (auto x:v)
50+ {
51+ if (x.first >num)
52+ break ;
53+ num += x.second ;
54+ }
55+ cout << num << endl;
56+ return 0 ;
57+ }
You can’t perform that action at this time.
0 commit comments