Created
October 2, 2020 03:36
-
-
Save Thiago4532/a8ef19b79dd30e1491f5f79cc39bd9dc 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
using namespace std; | |
const int maxn = 1e5 + 10; | |
int v[maxn], n; | |
int32_t main() { | |
cin >> n; | |
for (int i = 1; i <= n; i++) | |
cin >> v[i]; | |
sort(v+1, v+n+1); // ordeno o vetor | |
int s = 0; | |
for (int i = n; i >= 1; i -= 3) { // pecorro o vetor do maior para o menor, pulando de 3 em 3 | |
if (i > 1) s += (v[i] + v[i-1]); // adiciono apenas os dois maiores (caso exista apenas dois elementos eles serão somados) | |
else s += v[i]; // caso só tenha um elemento no vetor, adiciono ele | |
} | |
cout << s << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment