include vector include algorithm include iostream include map using na

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
int main()
{
//freopen("input.txt", "r" , stdin);
int n, s;
cin >> n >> s;
vector<int> V(n);
for(int i = 0; i < n; i++)
cin >> V[i];
sort(V.begin(), V.end());
int st = 0;
vector<int> ans;
int t = 0;
for(int i = n-1; i >= 0; i--)
{
for( ; st < i && V[st] + V[i] <= s && st < n; st++)
{
t = st;
}
if (st >= 0 && st < i && V[st] + V[i] > s)
{
ans.push_back(V[st]);
ans.push_back(V[i]);
}
if(st >= i) break;
st++;
}
map<int, int> SS;
map<int, int> B;
for(int i = 0; i < ans.size(); i++)
SS[ans[i]]++;
for(int i = 0; i < n; i++)
B[V[i]]++;
sort(V.rbegin(), V.rend());
for(int i = 0; i < n; i++)
{
if(SS[V[i]] < B[V[i]])
{
ans.push_back(V[i]);
SS[V[i]]++;
}
}
int aa = 0;
int i = 0;
for(; i < ans.size();)
{
if (i+1 < n && ans[i] + ans[i+1] <= s)
{
aa++;
i += 2;
}
else
{
aa++;
i++;
}
}
cout << aa << endl;
for(int i = 0; i < n; i++)
cout << ans[i] << " ";
cout << endl;
return 0;
}