We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents fef0c5c + ffe63fd commit 87dfecbCopy full SHA for 87dfecb
Algorithms/C++/Greedy Algorithms/Candy_pairing.cpp
@@ -0,0 +1,43 @@
1
+#include<iostream>
2
+using namespace std;
3
+int a[100005];
4
+int main()
5
+{
6
+ int n,k;
7
+ long long available_pairs=0;
8
+ cin>>n>>k;
9
+ for(int i=1;i<=n;i++)
10
+ {
11
+
12
+ cin>>a[i];
13
+ available_pairs+=a[i]/2;
14
+ a[i]-=1;
15
16
+ }
17
18
+ if(available_pairs < k)
19
20
+ cout<<"-1\n";
21
22
+ else
23
24
+ long long total=n;
25
+ long long pairs =0;
26
27
28
+ if(a[i]>=2)
29
30
+ total+=a[i]/2*2;
31
+ pairs+=a[i]/2;
32
+ a[i]=a[i]%2;
33
34
35
+ if(pairs >=k)
36
37
+ cout<<k*2+n-1<<"\n";
38
+ return 0;
39
40
+ cout<<total+k-pairs<<"\n";
41
42
43
+}
0 commit comments