Skip to content

Commit 322997c

Browse files
authored
Merge pull request #368 from suzzzal/day4s2
Add solution for Day 04 problem in day04sol2.cpp
2 parents 6a2ef34 + a4105b6 commit 322997c

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

0 commit comments

Comments
 (0)