void heaplify(int *arr, int len, int pos) { int left = pos * 2 + 1; int right = pos * 2 + 2; int max = 0; int temp = 0; if( left < len && arr[left] > arr[pos]) { max = left; } else { max = pos; } if( right < len && arr[right] > arr[max]) { max = right; } if( max != pos) { temp = arr[max]; arr[max]= arr[pos]; arr[pos] = temp; heaplify(arr, len, max); } }
void buildHeap(int *arr, int len) { for(int i = len / 2 - 1; i>=0 ; i--) { heaplify(arr, len, i); } }
void build_heap(){ int n = heap_size; int i; for(i = (n/2)-1;i >= 0;i--){ heaplify(i); } }
void heaplify(int i){ int l = left(i); int r = right(i); int smallest; node temp; if(l < heap_size && D[Q[l].i][Q[l].j] < D[Q[i].i][Q[i].j]){ smallest = l; }else{ smallest = i; } if(r < heap_size && D[Q[r].i][Q[r].j] < D[Q[smallest].i][Q[smallest].j]){ smallest = r; } if(smallest != i){ temp = Q[i]; Q[i] = Q[smallest]; Q[smallest] = temp; lookup[Q[i].i][Q[i].j] = i; lookup[Q[smallest].i][Q[smallest].j] = smallest; heaplify(smallest); } }
vertex extract_min(){ int j = heap_size-1; vertex temp; temp = A[0]; A[0] = A[j]; pointers[A[0].i][A[0].j] = 0; heap_size--; heaplify(0); return temp; }
node extract_min(){ int j = heap_size-1; node temp; temp = Q[0]; Q[0] = Q[j]; lookup[Q[0].i][Q[0].j] = 0; heap_size--; heaplify(0); return temp; }
void heaplify(int i) { int j=i; int l=2*i,r=2*i+1; if(l<=k&&heap[l]<heap[i]) i=l; if(r<=k&&heap[r]<heap[i]) i=r; if(i!=j) { swap(&heap[i],&heap[j]); heaplify(i); } }
int * heapSort(int *arr, int len) { buildHeap(arr, len); for( int i = len - 1; i > 0 ; i--) { int tmp = arr[i]; arr[i] = arr[0]; arr[0] = tmp; heaplify(arr, i , 0); } return arr; }
int main() { while(5==scanf("%d%d%d%d%d",&s,&w,&c,&k,&m)) { int time=9999/c+1,i; if(k>time)k=time; for(i=1; i<=k; i++) heap[i]=i*m+s; int total=0; for(i=1; i<=time; i++) { if(total<heap[1]) total=heap[1]; total+=w; heap[1]+=2*s+w; heaplify(1); } printf("%d\n",total+s); } return 0; }
void heaplify(int i){ int l = left(i); int r = right(i); int smallest; vertex temp; if(l < heap_size && D[A[l].i][A[l].j] < D[A[i].i][A[i].j]){ smallest = l; }else { smallest = i; } if(r < heap_size && D[A[r].i][A[r].j] < D[A[smallest].i][A[smallest].j]){ smallest= r; } if(smallest != i){ temp = A[i]; A[i] = A[smallest]; A[smallest] = temp; pointers[A[i].i][A[i].j] = i; pointers[A[smallest].i][A[smallest].j] = smallest; heaplify(smallest); } }