結果
| 問題 | 
                            No.1046 Fruits Rush
                             | 
                    
| コンテスト | |
| ユーザー | 
                             aki1
                         | 
                    
| 提出日時 | 2020-09-13 00:30:57 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2 ms / 2,000 ms | 
| コード長 | 564 bytes | 
| コンパイル時間 | 1,968 ms | 
| コンパイル使用メモリ | 166,236 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2025-01-02 15:09:17 | 
| 合計ジャッジ時間 | 2,472 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 14 | 
ソースコード
// No.1046 Fruits Rush
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(int)(n); i++)
using namespace std;
using ll = long long;
using ull = unsigned long long;
int main(){
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    int ans = 0;
    sort(a.rbegin(), a.rend());
    bool ok = true;
    rep(i, k){
        if(ans < ans+a[i]){
            ans += a[i];
            ok = false;
        }
        else if(a[i] < 0){
            if(ok && ans == 0) ans += a[i];
        }
    }
    cout << ans << endl;
    return 0;
}
            
            
            
        
            
aki1