#include<cstdio> #include<algorithm> #include<vector> int occ[600*1000]; int main() { int n, k; scanf("%i%i", &n, &k); std::fill(occ, occ+600*1000, -1); int tmp; for (int i = 0; i < n; ++i) { scanf("%i", &tmp); tmp--; occ[tmp] = (occ[tmp] == -1) ? i : occ[tmp]; } std::vector<int> tab; for (int i = 0; i < n; ++i) { if (occ[i] != -1) tab.push_back(occ[i]); } std::sort(tab.begin(), tab.end()); if (tab.size() < k) printf("-1\n"); else { long long res = 0LL; for (int i = 0; i < k; ++i) res += tab[i] - i; printf("%lli\n", res); } return 0; }
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 | #include<cstdio> #include<algorithm> #include<vector> int occ[600*1000]; int main() { int n, k; scanf("%i%i", &n, &k); std::fill(occ, occ+600*1000, -1); int tmp; for (int i = 0; i < n; ++i) { scanf("%i", &tmp); tmp--; occ[tmp] = (occ[tmp] == -1) ? i : occ[tmp]; } std::vector<int> tab; for (int i = 0; i < n; ++i) { if (occ[i] != -1) tab.push_back(occ[i]); } std::sort(tab.begin(), tab.end()); if (tab.size() < k) printf("-1\n"); else { long long res = 0LL; for (int i = 0; i < k; ++i) res += tab[i] - i; printf("%lli\n", res); } return 0; } |