Skip to content

Commit 616d697

Browse files
committed
add solution of New-Year Presents
1 parent 4f53c6d commit 616d697

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

18. Greedy/New-YearPresents.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Link problem : https://www.e-olymp.com/en/problems/26
2+
3+
#include <algorithm>
4+
#include <iostream>
5+
#include <vector>
6+
7+
using namespace std;
8+
9+
bool cmp(vector<int> present1, vector<int> present2)
10+
{
11+
return min(present1[0], present2[1]) < min(present2[0], present1[1]);
12+
}
13+
14+
int main()
15+
{
16+
int n;
17+
18+
cin >> n;
19+
int time, ans = 0, temp = 0;
20+
21+
vector<vector<int>> presents;
22+
23+
for (int i = 0; i < n; i++)
24+
{
25+
vector<int> inp = {0, 0};
26+
presents.push_back(inp);
27+
}
28+
29+
for (int i = 0; i < n * 2; i++)
30+
{
31+
cin >> time;
32+
presents[i % n][i / n] = time;
33+
}
34+
35+
sort(presents.begin(), presents.end(), cmp);
36+
37+
for (int i = 0; i < presents.size(); i++)
38+
{
39+
temp = temp + presents[i][0];
40+
ans = max(ans, temp) + presents[i][1];
41+
}
42+
43+
cout << ans;
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)