fork(1) download
  1. /* paiza POH! Vol.1
  2.  * result:
  3.  * http://p...content-available-to-author-only...a.jp/poh/ec-campaign/result/1e6a3a72f80fd4e8a12dee38bbd71aa9
  4.  * author: Leonardone @ NEETSDKASU
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #define DFSIZE (5)
  9.  
  10. int price[1000000] = {0};
  11. int list[500000];
  12.  
  13. inline int getInt(void) {
  14. register int c, n = 0;
  15. do {
  16. c = getchar();
  17. } while ((c < '0') || (c > '9'));
  18. do {
  19. n = n * 10 + (c - '0');
  20. c = getchar();
  21. } while ((c >= '0') && (c <= '9'));
  22. return n;
  23. }
  24.  
  25. void putInt(register int n) {
  26. register int t;
  27. if (n < 10) {
  28. putchar(n + '0');
  29. } else {
  30. t = n / 10;
  31. putInt(t);
  32. putchar(n - t * 10 + '0');
  33. }
  34. }
  35.  
  36. int foo(const int *p, const int f, const int e, const int m) {
  37. int ci, df = e - f;
  38. if (df < DFSIZE) return e;
  39. if (m < p[ci = (f + (df >> 1))]) {
  40. return foo(p, f, ci, m);
  41. } else {
  42. return foo(p, ci, e, m);
  43. }
  44. }
  45.  
  46. int bar(const int *p, const int f, const int e, const int m) {
  47. int ci, df = e - f;
  48. if (df < DFSIZE) return f;
  49. if (m > p[ci = (f + (df >> 1))]) {
  50. return bar(p, ci, e, m);
  51. } else {
  52. return bar(p, f, ci, m);
  53. }
  54. }
  55.  
  56. int main(void) {
  57. register int j, *f, *e, sum, tmp;
  58. int n, d, m, p, fi, i;
  59.  
  60. n = getInt();
  61. d = getInt();
  62.  
  63. j = n + 1;
  64. while (--j) {
  65. ++price[getInt()];
  66. }
  67.  
  68. /* sort */
  69. j = 0;
  70. i = 10;
  71. do {
  72. while (!price[i]) ++i;
  73. --price[i];
  74. list[j] = i;
  75. ++j;
  76. } while (j < n);
  77.  
  78. --n;
  79. j = d + 1;
  80. while (--j) {
  81. m = getInt();
  82. f = list + (fi = foo(list, 0, n, m));
  83. e = list + bar(list, 0, fi, m - *f);
  84. tmp = 0;
  85. while (f != e) {
  86. sum = *f + *e;
  87. if (sum > m)
  88. --f;
  89. else {
  90. if (sum > tmp) tmp = sum;
  91. ++e;
  92. }
  93. }
  94. putInt(tmp);
  95. putchar('\n');
  96. }
  97. return 0;
  98. }
  99.  
  100.  
Success #stdin #stdout 0s 8152KB
stdin
    10 6
    4500
   13300
    1100
    2200
   25100
    4000
    3000
    1000
    2000
    5000
    10000
    3000
    15000
    30000
    4000
    5600
stdout
9500
3000
14400
29600
4000
5600