#include <stdio.h>
#include <stdlib.h>
//必要があれば,関数をいくつでも追加して良い
//copy
#define height 4
#define MAX (1<<height) //ビットシフト演算 2^height と同じ
int t[MAX+1]; //配列外アクセス防止のためのダミーで+1
int sz = 0;
void swap(int *x, int *y){
int tmp = *x;
*x = *y;
*y = tmp;
}
void initTree(int n){
int i;
for(i=0;i<MAX;i++){
t[i] = -1;
}
}
void printA(){
int i;
for(i
=1;i
<=sz
;i
++) printf("%d ",t
[i
]); }
void printT(int i){
int x = i;
while(x/2!=0){
x/=2;
}
}
int goP(int i){
if(i/2 == 0) return 0;
else return i/2;
}
int goL(int i){
if(2*i >= MAX) return 0;
else return 2*i;
}
int goR(int i){
if(2*i+1 >= MAX) return 0;
else return 2*i+1;
}
void preOrder(int i){
if(t[i] == -1) return;
printT(i);
preOrder(goL(i));
preOrder(goR(i));
}
void inOrder(int i){
if(t[i] == -1) return;
inOrder(goL(i));
printT(i);
inOrder(goR(i));
}
void postOrder(int i){
if(t[i] == -1) return;
postOrder(goL(i));
postOrder(goR(i));
printT(i);
}
void insBT(int x){
int k,i = 1;
for(k=0;k<height;k++){
if(t[i]==-1){
t[i] = x;
sz++;
return;
}
if(x < t[i]) i = goL(i);
else i = goR(i);
}
printf("Error : too high -> %d\n",x
); }
//先頭の要素を取り出す
//ダウンヒープ
int popHeap() {
if (sz == 0) {
return -1;
}
int top = t[1];
t[1] = t[sz];
t[sz] = -1;
sz--;
int i = 1;
while (1) {
int left = goL(i);
int right = goR(i);
int largest = i;
if (left <= sz && t[left] > t[largest]) largest = left;
if (right <= sz && t[right] > t[largest]) largest = right;
if (largest == i) break;
swap(&t[i], &t[largest]);
i = largest;
}
return top;
}
//末尾に要素を追加する
//アップヒープ
void pushHeap(int x) {
if (sz >= MAX - 1) {
return;
}
sz++;
t[sz] = x;
int i = sz;
while (i > 1 && t[goP(i)] < t[i]) {
swap(&t[i], &t[goP(i)]);
i = goP(i);
}
}
//copy
//solve
int solve(){
int ret;
int i, x, n, q, sum_def=0;
// 木の初期化
initTree(n);
for (i = 0; i < n; i++) {
pushHeap(x);
sum_def+=x;
}
sz = n;
int max_def;
for(i=0; i<q; i++){
max_def=popHeap();
pushHeap(max_def/2);
printf("%d atack! %d -> %d\n", i
+1, max_def
, max_def
/2); sum_def-=(max_def/2);
}
//ここにプログラムを書く
//ret に答えを入れてメイン関数に返す
//入力を受ける部分も自分で書いてください
//今日の分を含め過去の授業のプログラムが
//参考になるはずです
ret=sum_def;
return ret;
}
//solve
//メイン関数はいじらなくて良い
int main(void){
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8v5b+F6KaB44GM44GC44KM44Gw77yM6Zai5pWw44KS44GE44GP44Gk44Gn44KC6L+95Yqg44GX44Gm6Imv44GECgoKCi8vY29weQojZGVmaW5lIGhlaWdodCA0CiNkZWZpbmUgTUFYICgxPDxoZWlnaHQpICAvL+ODk+ODg+ODiOOCt+ODleODiOa8lOeulyAyXmhlaWdodCDjgajlkIzjgZgKCgogCmludCB0W01BWCsxXTsgLy/phY3liJflpJbjgqLjgq/jgrvjgrnpmLLmraLjga7jgZ/jgoHjga7jg4Djg5/jg7zjgafvvIvvvJEKaW50IHN6ID0gMDsKIAp2b2lkIHN3YXAoaW50ICp4LCBpbnQgKnkpewogICAgaW50IHRtcCA9ICp4OwogICAgKnggPSAqeTsKICAgICp5ID0gdG1wOwp9CiAKdm9pZCBpbml0VHJlZShpbnQgbil7CiAgICBpbnQgaTsKICAgIGZvcihpPTA7aTxNQVg7aSsrKXsKICAgICAgICB0W2ldID0gLTE7CiAgICB9Cn0KIAp2b2lkIHByaW50QSgpewogICAgaW50IGk7CiAgICBmb3IoaT0xO2k8PXN6O2krKykgcHJpbnRmKCIlZCAiLHRbaV0pOwogICAgcHJpbnRmKCJcbiIpOwp9CiAKdm9pZCBwcmludFQoaW50IGkpewogICAgaW50IHggPSBpOwogICAgd2hpbGUoeC8yIT0wKXsKICAgICAgICBwcmludGYoIiAgIik7CiAgICAgICAgeC89MjsKICAgIH0KICAgIHByaW50ZigiJWRcbiIsdFtpXSk7Cn0KIAppbnQgZ29QKGludCBpKXsKICAgIGlmKGkvMiA9PSAwKSByZXR1cm4gMDsKICAgIGVsc2UgcmV0dXJuIGkvMjsKfQogCmludCBnb0woaW50IGkpewogICAgaWYoMippID49IE1BWCkgcmV0dXJuIDA7CiAgICBlbHNlIHJldHVybiAyKmk7Cn0KIAppbnQgZ29SKGludCBpKXsKICAgIGlmKDIqaSsxID49IE1BWCkgcmV0dXJuIDA7CiAgICBlbHNlIHJldHVybiAyKmkrMTsKfQogCnZvaWQgcHJlT3JkZXIoaW50IGkpewogICAgaWYodFtpXSA9PSAtMSkgcmV0dXJuOwogICAgcHJpbnRUKGkpOwogICAgcHJlT3JkZXIoZ29MKGkpKTsKICAgIHByZU9yZGVyKGdvUihpKSk7Cn0KIAp2b2lkIGluT3JkZXIoaW50IGkpewogICAgaWYodFtpXSA9PSAtMSkgcmV0dXJuOwogICAgaW5PcmRlcihnb0woaSkpOwogICAgcHJpbnRUKGkpOwogICAgaW5PcmRlcihnb1IoaSkpOwp9CiAKdm9pZCBwb3N0T3JkZXIoaW50IGkpewogICAgaWYodFtpXSA9PSAtMSkgcmV0dXJuOwogICAgcG9zdE9yZGVyKGdvTChpKSk7CiAgICBwb3N0T3JkZXIoZ29SKGkpKTsKICAgIHByaW50VChpKTsKfQogCnZvaWQgaW5zQlQoaW50IHgpewogICAgaW50IGssaSA9IDE7CiAgICBmb3Ioaz0wO2s8aGVpZ2h0O2srKyl7CiAgICAgICAgaWYodFtpXT09LTEpewogICAgICAgICAgICB0W2ldID0geDsKICAgICAgICAgICAgc3orKzsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBpZih4IDwgdFtpXSkgaSA9IGdvTChpKTsKICAgICAgICBlbHNlIGkgPSBnb1IoaSk7CiAgICB9CiAgICBwcmludGYoIkVycm9yIDogdG9vIGhpZ2ggLT4gJWRcbiIseCk7Cn0KIAovL+WFiOmgreOBruimgee0oOOCkuWPluOCiuWHuuOBmQovL+ODgOOCpuODs+ODkuODvOODlwppbnQgcG9wSGVhcCgpIHsKICAgIGlmIChzeiA9PSAwKSB7CiAgICAgICAgcHJpbnRmKCJoZWFwIGlzIGVtcHR5IVxuIik7CiAgICAgICAgcmV0dXJuIC0xOwogICAgfQogICAgaW50IHRvcCA9IHRbMV07IAogICAgdFsxXSA9IHRbc3pdOyAgIAogICAgdFtzel0gPSAtMTsgICAgIAogICAgc3otLTsKCiAgICBpbnQgaSA9IDE7CiAgICB3aGlsZSAoMSkgewogICAgICAgIGludCBsZWZ0ID0gZ29MKGkpOwogICAgICAgIGludCByaWdodCA9IGdvUihpKTsKICAgICAgICBpbnQgbGFyZ2VzdCA9IGk7CgogICAgICAgIGlmIChsZWZ0IDw9IHN6ICYmIHRbbGVmdF0gPiB0W2xhcmdlc3RdKSBsYXJnZXN0ID0gbGVmdDsKICAgICAgICBpZiAocmlnaHQgPD0gc3ogJiYgdFtyaWdodF0gPiB0W2xhcmdlc3RdKSBsYXJnZXN0ID0gcmlnaHQ7CgogICAgICAgIGlmIChsYXJnZXN0ID09IGkpIGJyZWFrOwogICAgICAgIHN3YXAoJnRbaV0sICZ0W2xhcmdlc3RdKTsKICAgICAgICBpID0gbGFyZ2VzdDsKICAgIH0KICAgIHJldHVybiB0b3A7Cn0KCi8v5pyr5bC+44Gr6KaB57Sg44KS6L+95Yqg44GZ44KLCi8v44Ki44OD44OX44OS44O844OXCnZvaWQgcHVzaEhlYXAoaW50IHgpIHsKICAgIGlmIChzeiA+PSBNQVggLSAxKSB7CiAgICAgICAgcHJpbnRmKCJoZWFwIGlzIGZ1bGwhXG4iKTsKICAgICAgICByZXR1cm47CiAgICB9CiAgICBzeisrOwogICAgdFtzel0gPSB4OwogICAgaW50IGkgPSBzejsKCiAgICB3aGlsZSAoaSA+IDEgJiYgdFtnb1AoaSldIDwgdFtpXSkgewogICAgICAgIHN3YXAo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