Cod sursa(job #964587)
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
ifstream f("oite.in");
ofstream g("oite.out");
#define ll long long
#define LE 1066
map<int,int> Hash1,Hash2;
int a[LE],i,j,n,tpos;
ll result;
int S;
int main(){
f>>n>>S;
fo(i,1,n) {
f>>a[i];
Hash1[a[i]]++;
}
for(i=1;i<=n;++i)
for(j=i+1;j<=n;++j)
if (a[i]+a[j]<=S)
Hash2[a[i]+a[j]]++;
for(i=1;i<=n;++i)
for(j=i+1;j<=n;++j)
if (a[i]+a[j]<=S){
int rest=S-a[i]-a[j];
cout<<rest<<" "<<a[i]<<" "<<a[j];
Hash1[a[i]]--;
Hash1[a[j]]--;
tpos=Hash2[rest]-Hash1[rest-a[i]]-Hash1[rest-a[j]];
if (a[i]+a[j]==rest) tpos--;
cout<<"---->"<<tpos<<'\n';
++Hash1[a[i]];
++Hash1[a[j]];
result+=tpos;
}
g<<result/6<<'\n';
f.close();
g.close();
return 0;
}