fork download
  1. #include<iostream>
  2. #include<algorithm>
  3. using namespace std;
  4.  
  5. long long S[100'005];
  6. long long strength[100'005];
  7.  
  8. int main() {
  9.  
  10. int n, q;
  11. cin >> n;
  12. for (int i = 0;i < n;i++) {
  13. cin >> S[i];
  14. }
  15.  
  16. cin >> q;
  17.  
  18. sort(S, S + n);
  19.  
  20. strength[0] = S[0];
  21. for (int i = 1; i < n; i++) {
  22. strength[i] = strength[i-1] + S[i];
  23. }
  24.  
  25. while (q--) {
  26.  
  27. long long p;
  28. int best = -1;
  29. cin >> p;
  30.  
  31. int start = 0, end = n - 1; long long mid;
  32.  
  33. while (start <= end) {
  34. mid = start + (end - start) / 2;
  35. if (p >= S[mid]) {
  36. best = mid;
  37. start = mid + 1;
  38. }
  39. else {
  40. end = mid - 1;
  41. }
  42. }
  43.  
  44. if (best > 0) {
  45. cout << best + 1 << " " << strength[best] << endl;
  46. }
  47. else { cout << "-1" << endl; }
  48. }
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty