paiza POH ec-campaign #paizahack_01 [VB/F#/D]

|問題|https://paiza.jp/poh/ec-campaign|
|:--|:--|
|タイム一覧/C++|http://qiita.com/cielavenir/items/a61cfe8390eb16866ae5|
|Python/Ruby(1)|http://qiita.com/cielavenir/items/b10ff4d201150f525062|
|C#/Java/Python/Ruby|http://qiita.com/cielavenir/items/d89e85f069cf570e6786|
|Perl/PHP|http://qiita.com/cielavenir/items/1a650a4c41d7bdd31392|
|JavaScript|http://qiita.com/cielavenir/items/a85b985888fdc15c52b7|
|Go/CoffeeScript/Scala/R/Bash|http://qiita.com/cielavenir/items/79016a0afd30470f440d|
|VB/F#/D|http://qiita.com/cielavenir/items/cb6094bab56253de992c|

VB

```vbnet:paizapoh1.vb
module PaizaPOH1
	dim SIZE as integer=9999999
	dim z(SIZE) as byte
	dim input_count as integer=0

	function myget() as integer
		dim r as integer
		while 48<=z(input_count) andalso z(input_count)<=57
			r=r*10+z(input_count)-48
			input_count+=1
		end while
		input_count+=1
		return r
	end function

	sub Main()
		Console.OpenStandardInput().Read(z,0,SIZE)
		dim n as integer=myget()
		dim d as integer=myget()
		dim zv(1000000) as integer
		dim v(n-1) as integer

		for i as integer=0 to n-1
			zv(myget())+=1
		next
		dim x as integer=0
		for j as integer=0 to 1000000
			dim k as integer=0
			while k<zv(j)
				v(x)=j
				x+=1
				k+=1
			end while
		next
		for i as integer=0 to d-1
			dim m as integer=myget()
			dim idx as integer=Array.BinarySearch(v,m-v(0)+1)
			if idx<0
				idx=not idx
			end if
			dim r as integer=0
			dim j as integer=0
			dim k as integer=idx-1
			while r<m andalso j<k andalso v(j)+v(j+1)<=m
				while v(j)+v(k)>m
					k-=1
				end while
				if r<v(j)+v(k)
					r=v(j)+v(k)
				end if
				j+=1
			end while
			Console.WriteLine(r)
		next
	end sub
end module
```

F#

```fsharp:paizapoh1.fs
open System
let arg:String array=Console.ReadLine().Split(' ')
let n=int(arg.[0])
let d=int(arg.[1])
let _v:int array=Array.zeroCreate(1000001)
let v:int array=Array.zeroCreate(n)
for i in 0..n-1 do
 let x=int(Console.ReadLine())
 _v.[x]<-_v.[x]+1
let mutable x=0
for j in 0..1000000 do
 for k in 0.._v.[j]-1 do
  v.[x]<-j
  x<-x+1
for i in 0..d-1 do
 let m=int(Console.ReadLine())
 let mutable idx=Array.BinarySearch(v,m-v.[0]+1)
 if idx<0 then
  idx<-(~~~idx)
 let mutable r=0
 let mutable j=0
 let mutable k=idx-1
 while r<m && j<k && v.[j]+v.[j+1]<=m do
  while v.[j]+v.[k]>m do
   k<-k-1
  if r<v.[j]+v.[k] then
   r<-v.[j]+v.[k]
  j<-j+1
 Console.WriteLine(r)
```

D

```d:paizapoh1.d
import std.stdio;
int array_binarysearch(int needle, int[] haystack, int size){
	int high = size-1;
	int low = 0;
	int ret = size;
	while( low <= high ){
		int probe = (high + low) / 2;
		int comparison = haystack[probe]-needle;
		if( comparison <= 0 ){
			low = probe+1;
		}else{
			ret=high;
			high = probe-1;
		}
	}
	return ret;
}
char[9999999] z;
int[1000001] _v;
int[500000] v;
int get(){
	static int input_count=0;
	int r=0;
	for(;'0'<=z[input_count]&&z[input_count]<='9';)r=r*10+z[input_count++]-'0';
	input_count++;
	return r;
}
void main(){
	stdin.rawRead(z);
	int n,d,m,i,j,k,r;
	n=get(),d=get();
	for(i=0;i<n;i++)_v[get()]++;
	for(i=j=0;j<1000001;j++)for(k=0;k<_v[j];k++)v[i++]=j;
	for(i=0;i<d;i++){
		m=get();
		int idx=array_binarysearch(m-v[0],v,n);
		for(r=j=0,k=idx-1;r<m&&j<k&&v[j]+v[j+1]<=m;j++){
			for(;v[j]+v[k]>m;)k--;
			if(r<v[j]+v[k])r=v[j]+v[k];
		}
		printf("%d\n",r);
	}
}
```