//#include #include #include #include #include #include #include #include #include #include using namespace std; bool DBG = false; /* * generics for HackerRank problems. */ vector SieveOut; #define MAXN 100 #define MAXSIEVEN 1000000 vector V; vector > M; uint32_t N, R, C; // generics N = sizeof(V), R,C = sizeof M... UINT32_MAX string S; #define SORT(x) sort(x.begin(), x.end()); #define RSORT(x) sort(x.begin(), x.end(), mygreater()); struct mygreater { template bool operator()(T const &a, T const &b) const { return a > b; } }; class HRGeneric // generic hackerrank class { public: HRGeneric() {}; uint32_t val; string S; bool operator <(const HRGeneric& rhs) const { return val < rhs.val; } bool operator >(const HRGeneric& rhs) const { return val > rhs.val; } HRGeneric operator =(const HRGeneric& rhs) { val=rhs.val; S=rhs.S; return *this; } // FRIEND FUNCTIONS friend std::ostream& operator<<(std::ostream &os, const HRGeneric &HR); friend std::istream& operator>>(std::istream &is, HRGeneric &HR); }; // for displaying HRGeneric class std::ostream& operator<<(std::ostream& out, const HRGeneric& C) { out << "(" << C.val <<":"<< C.S << ")"; return out; } // for reading in HRGeneric class std::istream& operator>>(std::istream& in, HRGeneric& C) { in >> C.val >> C.S; return in; } vector H; vector > MH; // matrix of the generic class; //display a vector. template ostream& operator<< (ostream &out, const vector &a) { if (!a.empty()) { out << '['; copy(a.begin(), a.end(), ostream_iterator(out, " ")); out << "\b]"; } return out; } //display a matrix. template ostream& operator<< (ostream &out, const vector> &a) { int sz = a.size(); for(int i = 0; i < sz; i++) { int sz2 = a[i].size(); for(int j = 0; j < sz2; j++) { out << a[i][j] << ' '; } out << endl; } return out; } // initialize the matrix - r rows, c columns template void initM(vector> & myM, int r, int c) { myM.resize(r); for (int i = 0; i < r; i ++){ myM[i].resize(c); } } //Read in an array into templated class. This works for ints & HRGeneric. template void readV(vector & myH, int N) { myH.resize(N); for (uint32_t i = 0; i < N; i ++) { cin >> myH[i]; if (cin.eof()) { DBG && cout << "Reached unexpected EOF before completing cin\n"; break; } } } uint64_t solveOne(uint64_t val) { int sz = SieveOut.size(); uint64_t ans = val; // eat all the pieces uint64_t mul = 1; for (int i = sz-1; i >= 0; i --) { if (val % SieveOut[i] == 0) { val /= SieveOut[i]; ans += mul; mul *= SieveOut[i]; if (val % SieveOut[i] == 0) { // if multiple factor, just increment i ++; } } } return ans; } //primed to read in an array of integers into the global H array. void solve() { cin >> N; readV(V, N); uint64_t ans = 0; for (auto it : V) { ans += solveOne(it); DBG && cout << ans << endl; } cout << ans << endl; } int main(int argc, char * argv[]) { if ( argc > 1 && !strcmp(argv[1], "-d")) DBG = true; vector SieveIn; SieveOut.push_back(2); //fill in sieve of Erasthenese 12/17/2017 SieveIn.resize(MAXSIEVEN, true); int iOut = 0; uint32_t val = 3; SieveIn[0] = false; SieveIn[1] = false; SieveIn[2] = false; for (int i = 0; i < MAXSIEVEN; i += 2) { SieveIn[i] = false; } while (val < MAXSIEVEN) { // DBG && cout << val << " "; SieveOut.push_back(val); iOut ++; for (int i = val; i < MAXSIEVEN; i += val) { SieveIn[i] = false; } for (;val < MAXSIEVEN && !SieveIn[val]; val++){}; } solve(); return 0; }