ソースを参照

排序后贪心,从最大的开始加

jal 5 年 前
コミット
c0efd26511
1 ファイル変更25 行追加0 行削除
  1. 25 0
      E.cpp

+ 25 - 0
E.cpp

@@ -0,0 +1,25 @@
+//
+// Created by jal on 2019-04-01.
+//
+#include <bits/stdc++.h>
+using namespace std;
+int main(){
+    int s;
+    cin >> s;
+    int n;
+    cin >> n;
+    vector<int>v(n);
+    for(auto &i:v)cin >> i;
+    sort(v.begin(), v.end());
+    int t = 0;
+    for(int i = n-1; i >= 0; i--){
+        t += v[i];
+        if(t > s){
+            cout << n - i << endl;
+            break;
+        }
+    }
+    if(t <= s){
+        cout << -1 << endl;
+    }
+}