// #define DEBUG_ON #pragma GCC optimize("Ofast") #include #define timer_start() \ chrono::system_clock::time_point start, end; \ start = chrono::system_clock::now(); #define timer_wrap() \ end = chrono::system_clock::now(); \ double time = static_cast( \ chrono::duration_cast(end - start).count() / \ 1000.0); \ printf("time %lf[ms]\n", time); \ start = end; #define optimize_cin() \ cin.tie(0); \ ios::sync_with_stdio(false) using namespace std; using ll = long long; #define INF 1000000000 // 10^9 template bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int main() { // input int n, k; cin >> n >> k; #ifdef DEBUG_ON timer_start(); #endif vector a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } int sum = 0; sort(a.begin(), a.end()); for (int i = a.size() - 1; i >= (n - k); i--) { if (a.at(i) < 0 && i < a.size() - 1) { break; } sum += a.at(i); } cout << sum << endl; #ifdef DEBUG_ON timer_wrap(); #endif return 0; }