Created
May 7, 2017 11:05
-
-
Save tuankiet65/05e30841f971b037d9ad64a2eb9716e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <algorithm> | |
#include <vector> | |
int n; | |
std::vector<int> a; | |
int main(){ | |
int i, tmp; | |
int count_1 = 0, count_2 = 0, len = 0; | |
scanf("%d", &n); | |
if (n <= 0){ | |
printf("0"); | |
return 0; | |
} | |
if (n == 1){ | |
printf("1"); | |
return 0; | |
} | |
for (i = 0; i < n; i++){ | |
scanf("%d", &(tmp)); | |
if (tmp == 1){ | |
count_1++; | |
} else if (tmp == 2){ | |
count_2++; | |
} else { | |
a.push_back(tmp); | |
} | |
} | |
std::sort(a.begin(), a.end(), std::greater<int>()); | |
if (count_1 < 2){ | |
printf("-1"); | |
return 0; | |
} | |
count_1 -= 1; | |
len = 1 + count_2; | |
while (!a.empty()){ | |
int curr = a.back(); | |
if ((count_1 == (curr-1)) && (a.size() == 1)){ | |
len++; | |
break; | |
} else if ((count_1 != (curr-1)) && (a.size() == 1)){ | |
printf("-1"); | |
return 0; | |
} else if (count_1 < (curr - 2)){ | |
printf("-1"); | |
return 0; | |
} else { | |
count_1 -= (curr - 2); | |
len += 1; | |
a.pop_back(); | |
} | |
} | |
printf("%d", len); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment