From a4105b6274285a5de88be6abf688f38cae9722ff Mon Sep 17 00:00:00 2001 From: suzzzal Date: Tue, 30 Dec 2025 01:09:26 +0530 Subject: [PATCH] Add solution for Day 04 problem in day04sol2.cpp Implement solution for Codeforces problem involving good chunks. --- .../Day-04/sol/Sujal_Kshatri/day04sol2.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Problems/Data-structures/Day-04/sol/Sujal_Kshatri/day04sol2.cpp diff --git a/Problems/Data-structures/Day-04/sol/Sujal_Kshatri/day04sol2.cpp b/Problems/Data-structures/Day-04/sol/Sujal_Kshatri/day04sol2.cpp new file mode 100644 index 0000000..c5c4d26 --- /dev/null +++ b/Problems/Data-structures/Day-04/sol/Sujal_Kshatri/day04sol2.cpp @@ -0,0 +1,57 @@ +//submission link - https://codeforces.com/problemset/submission/1912/355802995 + +/* +Approach: + +We start with a number `num`. + +From each list, we take numbers from left to right. +If numbers go down first but later go up, we save that part as a "good chunk". + +For every good chunk, we remember: +-how much `num` we need to start it +- how much `num` increases after it + +Then we sort all chunks by how much `num` they need. +We take chunks one by one if we can afford them. + +This way, `num` grows as much as possible. +*/ + +#include +using namespace std; + +int main() +{ + long long num,k; + cin >> num >> k; + long long i,j; + vector> v; + for(i=0;i> n; + long long maxi=0,add=0; + for(j=0;j> x; + add += x; + maxi = max(maxi,-add); + if(add>0) + { + v.push_back({maxi,add}); + add=0; + } + } + } + sort(v.begin(),v.end()); + for(auto x:v) + { + if(x.first>num) + break; + num += x.second; + } + cout << num << endl; + return 0; +}