-
Notifications
You must be signed in to change notification settings - Fork 66
/
1144 B. Parity Alternated Deletions.cpp
75 lines (75 loc) · 2.01 KB
/
1144 B. Parity Alternated Deletions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
using namespace std;
int main()
{
fastread();
int n;
ll a[2001];
int odd = 0,even = 0,temp;
cin>>n;
for(int i=0; i<n; i++){
cin>>a[i];
if(a[i] % 2 == 0)even++;
else odd++;
}
if(odd == even || (odd+1 == even || even+1 == odd)){
cout<<0<<endl;
return 0;
}
sort(a,a+n,greater<int>());
if(odd > even){
temp = 2;
while(odd+even > 0){
for(int i=0; i<n; i++){
if(a[i] % 2 == 1 && temp % 2 == 0){
temp = a[i];
a[i] = -1;
odd--;
break;
}
else if(a[i] % 2 == 0 && temp % 2 == 1){
temp = a[i];
a[i] = -1;
even--;
break;
}
}
if((odd == 0 && temp % 2 == 0) || (even == 0 && temp % 2 == 1))
break;
}
}
else if(odd < even){
temp = 1;
while(odd+even > 0){
for(int i=0; i<n; i++){
if(a[i] % 2 == 1 && temp % 2 == 0){
temp = a[i];
a[i] = -1;
odd--;
break;
}
else if(a[i] % 2 == 0 && temp % 2 == 1){
temp = a[i];
a[i] = -1;
even--;
break;
}
}
if((odd == 0 && temp % 2 == 0) || (even == 0 && temp % 2 == 1))
break;
}
}
ll ans = 0;
for(int i=0; i<n; i++){
if(a[i] != -1){
ans += a[i];
}
}
cout<<ans<<endl;
return 0;
}