#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
using namespace std;
int bh[102];
int use_bh[102];
int sub_bh[102];
int wsub_bh[102];
int all_bh_used()
{
int i=0;
for(i=0;i<101;i++)
{
if(bh[i] > 0)return 0;
}
return 1;
}
vector <vector <int> > match2(int height, int position, vector <vector <int> > vl)
{
if(bh[height+1] > 0)
{
vl[position-1].push_back(height+1);
}
if(bh[height] > 0)
{
vl[position-1].push_back(height);
}
return vl;
}
int find(int depth, int ph, int p)
{
int i;
if(use_bh[p] > 1)
{
for(i=depth;i<=ph+1;i++)
{
if(bh[i] > 0)
{
bh[i] -= 1;
//cout<<"bh["<<i<<"]="<<bh[i]<<endl;
sub_bh[i] += 1;
return 1;
}
}
}
else
{
if(bh[p] > 0)
{
bh[p] -= 1;
sub_bh[p] += 1;
return 1;
}
}
return 0;
}
class CrossingTheRiver{
public:
CrossingTheRiver(){}
~CrossingTheRiver(){}
string isItEvenPossible(int waterWidth, int landWidth, vector<int> blockHeight, int depth);
};
string CrossingTheRiver::isItEvenPossible(int waterWidth, int landWidth, vector<int> blockHeight, int depth)
{
string st;
vector<vector <int> > vl;
int i=0, s, skip=0;
int position = 1, ph=0, f=0, land_sum=0, old_ph=0, end_ph=0, f1=0, f2=0;
int len_block = blockHeight.size();
for (int i = 0; i < waterWidth + landWidth; i++) {
vector<int> row;
vl.push_back(row);
}
//ini bh
for(i=0; i<102; i++)
{
bh[i] = 0;
use_bh[i] = 0;
}
for(i=0;i<len_block;i++)
{
bh[blockHeight[i]] += 1;
}
while(position <= waterWidth + landWidth && 0 == f)
{
skip = 0;
if(1 == all_bh_used())
{
f = 1;
break;
}
if(position <= waterWidth)
{
ph += depth;
vl = match2(ph, position, vl);
if(vl[position-1].size() > 0)
{
ph = vl[position-1].back() - depth;
bh[vl[position-1].back()] -= 1;
use_bh[vl[position-1].back()] += 1;
++position;
}
else//vl[position-1].size() == 0
{
if(position > 1)
{
--position;
s = vl[position-1].size();
bh[vl[position-1][s-1]] += 1;
use_bh[vl[position-1][s-1]] -= 1;
vl[position-1].resize(s-1);
if(s>1)
{
bh[vl[position-1].back()] -= 1;
use_bh[vl[position-1].back()] += 1;
ph = vl[position-1].back() - depth;
skip = 1;
++position;
}
while(vl[position-1].size() == 0 && 0 == skip)
{
if(position > 1)
{
--position;
s = vl[position-1].size();
bh[vl[position-1][s-1]] += 1;
use_bh[vl[position-1][s-1]] -= 1;
vl[position-1].resize(s-1);
if(s>1)
{
bh[vl[position-1].back()] -= 1;
use_bh[vl[position-1].back()] += 1;
ph = vl[position-1].back() - depth;
++position;
break;
}
}
else
{
if(vl[position-1].size() > 0)
{
s = vl[position-1].size();
bh[vl[position-1][s-1]] += 1;
use_bh[vl[position-1][s-1]] -= 1;
vl[position-1].resize(s-1);
if(s>1)
{
bh[vl[position-1].back()] -= 1;
use_bh[vl[position-1].back()] += 1;
ph = vl[position-1].back() - depth;
++position;
break;
}
}
else
{
f = 1;
break;
}
}
}
}
else//position == 1
{
if(vl[position-1].size() > 1)
{
s = vl[position-1].size();
bh[vl[position-1][s-1]] += 1;
use_bh[vl[position-1][s-1]] -= 1;
vl[position-1].resize(s-1);
if(s>1)
{
bh[vl[position-1].back()] -= 1;
if(bh[vl[position-1].back()] < 0)cout<<"bh8["<<position<<"]="<<bh[vl[position-1].back()]<<endl;
use_bh[vl[position-1].back()] += 1;
ph = vl[position-1].back() - depth;
++position;
}
}
else
{
f = 1;
break;
}
}
}
}
else//position > waterWidth
{
old_ph = ph;
land_sum = position - waterWidth - 1;
f1 = 0;
f2 = 0;
while(land_sum < landWidth)
{
if(0 == bh[ph] && 0 == bh[ph+1])
{
if(use_bh[ph] > 0 && 0 == f1)
{
if(1 == find(depth, ph, ph))
{
land_sum += 1;
use_bh[ph] -= 1;
wsub_bh[ph] += 1;
}
else f1 = 1;
}
else if(use_bh[ph+1] > 0 && 0 == f2)
{
if(1 == find(depth, ph, ph+1))
{
land_sum += 1;
use_bh[ph] -= 1;
wsub_bh[ph] += 1;
++ph;
f1 = 0;
f2 = 0;
}
else f2 = 1;
}
else
{
end_ph = ph;
break;
}
}
else
{
if(bh[ph] > 0)
{
land_sum += bh[ph];
sub_bh[ph] = bh[ph];
bh[ph] = 0;
}
if(bh[ph+1] > 0)
{
land_sum += bh[ph+1];
sub_bh[ph+1] = bh[ph+1];
bh[ph+1] = 0;
++ph;
}
}
}
for(i=depth;i<=end_ph;i++)
{
if(sub_bh[i] > 0)
{
bh[i] += sub_bh[i];
sub_bh[i] = 0;
}
if(wsub_bh[i] > 0)
{
use_bh[i] += wsub_bh[i];
wsub_bh[i] = 0;
}
}
if(land_sum >= landWidth)
{
position = waterWidth + landWidth + 1;
break;
}
else//vl[position-1].size() = 0
{
if(position > 1)
{
--position;
s = vl[position-1].size();
bh[vl[position-1][s-1]] += 1;
use_bh[vl[position-1][s-1]] -= 1;
vl[position-1].resize(s-1);
if(s>1)
{
bh[vl[position-1].back()] -= 1;
use_bh[vl[position-1].back()] += 1;
if(position > waterWidth)
{
ph = vl[position-1].back();
}
else
{
ph = vl[position-1].back() - depth;
}
++position;
skip = 1;
}
while(vl[position-1].size() == 0 && 0==skip)
{
if(position > 1)
{
--position;
s = vl[position-1].size();
bh[vl[position-1][s-1]] += 1;
use_bh[vl[position-1][s-1]] -= 1;
vl[position-1].resize(s-1);
if(s>1)
{
bh[vl[position-1].back()] -= 1;
use_bh[vl[position-1].back()] += 1;
if(position > waterWidth)
{
ph = vl[position-1].back();
}
else
{
ph = vl[position-1].back() - depth;
}
++position;
break;
}
}
else//while, position==1
{
if(vl[position-1].size() > 1)
{
s = vl[position-1].size();
bh[vl[position-1][s-1]] += 1;
use_bh[vl[position-1][s-1]] -= 1;
vl[position-1].resize(s-1);
if(s>1)
{
bh[vl[position-1].back()] -= 1;
use_bh[vl[position-1].back()] += 1;
if(position > waterWidth)
{
ph = vl[position-1].back();
}
else
{
ph = vl[position-1].back() - depth;
}
++position;
break;
}
}
else
{
f = 1;
break;
}
}
}
}
else//if position == 1
{
if(vl[position-1].size() > 1)
{
s = vl[position-1].size();
bh[vl[position-1][s-1]] += 1;
use_bh[vl[position-1][s-1]] -= 1;
vl[position-1].resize(s-1);
if(s>1)
{
bh[vl[position-1].back()] -= 1;
use_bh[vl[position-1].back()] += 1;
if(position > waterWidth)
{
ph = vl[position-1].back();
}
else
{
ph = vl[position-1].back() - depth;
}
++position;
}
}
else
{
f = 1;
break;
}
}
}
}
}
if(0 == f)
{
st = "POSSIBLE";
}
else {
if (0 == ph)st = "POSSIBLE";
else
{
st = "IMPOSSIBLE";
}
}
return st;
}
int main()
{
CrossingTheRiver cr;
/*
int myints[] = {
5, 9, 41, 7, 5, 7, 8, 3, 6, 16, 16, 81, 6, 6, 17, 5, 15, 5, 13, 5, 63, 10, 14, 5, 12, 69, 5, 6, 7, 9, 64, 14, 11, 4, 7, 3, 14, 7, 13, 96, 4, 92, 33, 97, 45, 4, 55, 7, 16, 7
};
vector <int> b(myints, myints + sizeof(myints) / sizeof(int));
cout<<cr.isItEvenPossible(13, 26, b, 2);
*/
//Possible
/*
int myints[] = {
3,4,4,5,5, 6
};
vector <int> b(myints, myints + sizeof(myints) / sizeof(int));
cout<<cr.isItEvenPossible(3, 3, b, 2);
*///Possible
/*
int myints[] = {
3,4,4,5,6, 6
};
vector <int> b(myints, myints + sizeof(myints) / sizeof(int));
cout<<cr.isItEvenPossible(3, 3, b, 2);
*/
//impossible
/*
int myints[] = {
2, 2, 60, 2, 5, 5, 2, 14, 89, 4, 11, 49, 83, 31, 61, 21, 62, 5, 3, 2, 6, 18, 2, 47, 5
};
vector <int> b(myints, myints + sizeof(myints) / sizeof(int));
cout<<cr.isItEvenPossible(11, 2, b, 2);
*///Possible
/*
int myints[] = {
19, 21, 13, 23, 17, 25, 22, 29, 14, 18, 29, 19, 20, 14, 25, 19, 27, 14, 21, 28, 15, 25, 17, 26, 25, 13, 20, 29, 13, 20, 16, 22, 20, 24, 16, 24, 13, 19, 24, 24, 14, 18, 16, 22, 21
};
vector <int> b(myints, myints + sizeof(myints) / sizeof(int));
cout<<cr.isItEvenPossible(35, 10, b, 13);
*/
//Possible
/*
int myints[] = {
2, 3, 4, 5
};
vector <int> b(myints, myints + sizeof(myints) / sizeof(int));
cout<<cr.isItEvenPossible(3, 1, b, 2);
*///imposible
int myints[] = {
14, 22, 13, 26, 28, 20, 24, 19, 13, 23, 21, 16, 27, 25, 14, 18, 27, 28, 20, 22, 16, 12, 29, 15, 12, 17, 21, 23, 13, 14, 18, 21, 18, 19, 17, 28
};
vector <int> b(myints, myints + sizeof(myints) / sizeof(int));
cout<<cr.isItEvenPossible(33, 4, b, 11);
//imposible
//getchar();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8c3RkaW8uaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgYmhbMTAyXTsKaW50IHVzZV9iaFsxMDJdOwppbnQgc3ViX2JoWzEwMl07CmludCB3c3ViX2JoWzEwMl07CgppbnQgYWxsX2JoX3VzZWQoKQp7CiAgICBpbnQgaT0wOwogICAgZm9yKGk9MDtpPDEwMTtpKyspCiAgICB7CiAgICAgICAgaWYoYmhbaV0gPiAwKXJldHVybiAwOwogICAgfQogICAgcmV0dXJuIDE7Cn0KCnZlY3RvciA8dmVjdG9yIDxpbnQ+ID4gbWF0Y2gyKGludCBoZWlnaHQsIGludCBwb3NpdGlvbiwgdmVjdG9yIDx2ZWN0b3IgPGludD4gPiAgdmwpCnsgICAKICAgIAogICAgaWYoYmhbaGVpZ2h0KzFdID4gMCkKICAgIHsKICAgICAgICB2bFtwb3NpdGlvbi0xXS5wdXNoX2JhY2soaGVpZ2h0KzEpOwogICAgfQogICAgaWYoYmhbaGVpZ2h0XSA+IDApCiAgICB7CiAgICAgICAgdmxbcG9zaXRpb24tMV0ucHVzaF9iYWNrKGhlaWdodCk7CiAgICB9CiAgICAKCXJldHVybiB2bDsKfQoKaW50IGZpbmQoaW50IGRlcHRoLCBpbnQgcGgsIGludCBwKQp7CiAgICBpbnQgaTsKICAgIGlmKHVzZV9iaFtwXSA+IDEpCiAgICB7CiAgICAgICAgZm9yKGk9ZGVwdGg7aTw9cGgrMTtpKyspCiAgICAgICAgewogICAgICAgICAgICBpZihiaFtpXSA+IDApCiAgICAgICAgICAgIHsgICAgICAgICAgICAKICAgICAgICAgICAgICAgIGJoW2ldIC09IDE7CiAgICAgICAgICAgICAgICAvL2NvdXQ8PCJiaFsiPDxpPDwiXT0iPDxiaFtpXTw8ZW5kbDsKICAgICAgICAgICAgICAgIHN1Yl9iaFtpXSArPSAxOwogICAgICAgICAgICAgICAgcmV0dXJuIDE7ICAgICAgICAgICAgCiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBlbHNlCiAgICB7CiAgICAgICAgaWYoYmhbcF0gPiAwKQogICAgICAgIHsKICAgICAgICAgICAgYmhbcF0gLT0gMTsKICAgICAgICAgICAgc3ViX2JoW3BdICs9IDE7CiAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9CgpjbGFzcyBDcm9zc2luZ1RoZVJpdmVyewoJcHVibGljOgoJQ3Jvc3NpbmdUaGVSaXZlcigpe30KCX5Dcm9zc2luZ1RoZVJpdmVyKCl7fQoJc3RyaW5nIGlzSXRFdmVuUG9zc2libGUoaW50IHdhdGVyV2lkdGgsIGludCBsYW5kV2lkdGgsIHZlY3RvcjxpbnQ+ICBibG9ja0hlaWdodCwgaW50IGRlcHRoKTsKfTsKCnN0cmluZyBDcm9zc2luZ1RoZVJpdmVyOjppc0l0RXZlblBvc3NpYmxlKGludCB3YXRlcldpZHRoLCBpbnQgbGFuZFdpZHRoLCB2ZWN0b3I8aW50PiAgYmxvY2tIZWlnaHQsIGludCBkZXB0aCkKewoJc3RyaW5nIHN0OwoJdmVjdG9yPHZlY3RvciA8aW50PiA+IHZsOwoJCglpbnQgaT0wLCBzLCBza2lwPT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LTFdLmJhY2soKV0gKz0gMTsKICAgICAgICAgICAgICAgICsrcG9zaXRpb247CgkJCX0KCQkJZWxzZS8vdmxbcG9zaXRpb24tMV0uc2l6ZSgpID09IDAKCQkJewoJCQkJaWYocG9zaXRpb24gPiAxKQoJCQkJewoJCQkJCS0tcG9zaXRpb247CiAgICAgICAgICAgICAgICAgICAgcyA9IHZsW3Bvc2l0aW9uLTFdLnNpemUoKTsKICAgICAgICAgICAgICAgICAgICBiaFt2bFtwb3NpdGlvbi0xXVtzLTFdXSArPSAxOwogICAgICAgICAgICAgICAgICAgIHVzZV9iaFt2bFtwb3NpdGlvbi0xXVtzLTFdXSAtPSAxOwogICAgICAgICAgICAgICAgICAgIHZsW3Bvc2l0aW9uLTFdLnJlc2l6ZShzLTEpOwoJCQkJCWlmKHM+MSkKCQkJCQl7CiAgICAgICAgICAgICAgICAgICAgICAgIGJoW3ZsW3Bvc2l0aW9uLTFdLmJhY2soKV0gLT0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgdXNlX2JoW3ZsW3Bvc2l0aW9uLTFdLmJhY2soKV0gKz0gMTsKCQkJCQkJcGggPSB2bFtwb3NpdGlvbi0xXS5iYWNrKCkgLSBkZXB0aDsgCgkJCQkJCXNraXAgPSAxOwogICAgICAgICAgICAgICAgICAgICAgICArK3Bvc2l0aW9uOwoJCQkJCX0KICAgICAgICAgICAgICAgICAgICAKCQkJCQl3aGlsZSh2bFtwb3NpdGlvbi0xXS5zaXplKCkgPT0gMCAmJiAwID09IHNraXApCgkJCQkJewogICAgICAgICAgICAgICAgICAgICAgICBpZihwb3NpdGlvbiA+IDEpCgkJCQkJCXsKCQkJCQkJCS0tcG9zaXRpb247CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzID0gdmxbcG9zaXRpb24tMV0uc2l6ZSgpOwoJCQkJCQkJYmhbdmxbcG9zaXRpb24tMV1bcy0xXV0gKz0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHVzZV9iaFt2bFtwb3NpdGlvbi0xXVtzLTFdXSAtPSAxOwoJCQkJCQkJdmxbcG9zaXRpb24tMV0ucmVzaXplKHMtMSk7CgkJCQkJCQlpZihzPjEpCgkJCQkJCQl7CgkJCQkJCQkJYmhbdmxbcG9zaXRpb24tMV0uYmFjaygpXSAtPSAxOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHVzZV9iaFt2bFtwb3NpdGlvbi0xXS5iYWNrKCldICs9IDE7CgkJCQkJCQkJcGggPSB2bFtwb3NpdGlvbi0xXS5iYWNrKCkgLSBkZXB0aDsgCgkJCQkJCQkJKytwb3NpdGlvbjsKCQkJCQkJCQlicmVhazsKCQkJCQkJCX0KCQkJCQkJfQoJCQkJCQllbHNlCgkJCQkJCXsKCQkJCQkJCWlmKHZsW3Bvc2l0aW9uLTFdLnNpemUoKSA+IDApCgkJCQkJCQl7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcyA9IHZsW3Bvc2l0aW9uLTFdLnNpemUoKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBiaFt2bFtwb3NpdGlvbi0xXVtzLTFdXSArPSAxOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHVzZV9iaFt2bFtwb3NpdGlvbi0xXVtzLTFdXSAtPSAxOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHZsW3Bvc2l0aW9uLTFdLnJlc2l6ZShzLTEpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmKHM+MSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJoW3ZsW3Bvc2l0aW9uLTFdLmJhY2soKV0gLT0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgdXNlX2JoW3ZsW3Bvc2l0aW9uLTFdLmJhY2soKV0gKz0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcGggPSB2bFtwb3Np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KCQkJCQl7CgkJCQkJCWYgPSAxOwoJCQkJCQlicmVhazsKCQkJCQl9CgkJCQl9CgkJCX0JCgkJfQoJCWVsc2UvL3Bvc2l0aW9uID4gd2F0ZXJXaWR0aCAKCQl7CQogICAgICAgICAgICBvbGRfcGggPSBwaDsKICAgICAgICAgICAgbGFuZF9zdW0gPSBwb3NpdGlvbiAtIHdhdGVyV2lkdGggLSAxOwogICAgICAgICAgICBmMSA9IDA7CiAgICAgICAgICAgIGYyID0gMDsKICAgICAgICAgICAgd2hpbGUobGFuZF9zdW0gPCBsYW5kV2lkdGgpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmKDAgPT0gYmhbcGhdICYmIDAgPT0gYmhbcGgrMV0pCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgaWYodXNlX2JoW3BoXSA+IDAgJiYgMCA9PSBmMSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmKDEgPT0gZmluZChkZXB0aCwgcGgsIHBoKSkKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgbGFuZF9zdW0gKz0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHVzZV9iaFtwaF0gLT0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHdzdWJfYmhbcGhdICs9IDE7CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgZWxzZSBmMSA9IDE7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGVsc2UgaWYodXNlX2JoW3BoKzFdID4gMCAmJiAwID09IGYyKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgaWYoMSA9PSBmaW5kKGRlcHRoLCBwaCwgcGgrMSkpCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGxhbmRfc3VtICs9IDE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB1c2VfYmhbcGhdIC09IDE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB3c3ViX2JoW3BoXSArPSAxOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgKytwaDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGYxID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGYyID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBlbHNlIGYyID0gMTsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgZW5kX3BoID0gcGg7CiAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBpZihiaFtwaF0gPiAwKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgbGFuZF9zdW0gKz0gYmhbcGhdOwogICAgICAgICAgICAgICAgICAgICAgICBzdWJfYmhbcGhdID0gYmhbcGhdOwogICAgICAgICAgICAgICAgICAgICAgICBiaFtwaF0gPSAwOwogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZihiaFtwaCsxXSA+IDApCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBsYW5kX3N1bSArPSBiaFtwaCsxXTsKICAgICAgICAgICAgICAgICAgICAgICAgc3ViX2JoW3BoKzFdID0gYmhbcGgrMV07CiAgICAgICAgICAgICAgICAgICAgICAgIGJoW3BoKzFdID0gMDsKICAgICAgICAgICAgICAgICAgICAgICAgKytwaDsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIGZvcihpPWRlcHRoO2k8PWVuZF9waDtpKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmKHN1Yl9iaFtpXSA+IDApCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgYmhbaV0gKz0gc3ViX2JoW2ldOwogICAgICAgICAgICAgICAgICAgIHN1Yl9iaFtpXSA9IDA7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZih3c3ViX2JoW2ldID4gMCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICB1c2VfYmhbaV0gKz0gd3N1Yl9iaFtpXTsKICAgICAgICAgICAgICAgICAgICB3c3ViX2JoW2ldID0gMDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICAKICAgICAgICAgICAgaWYobGFuZF9zdW0gPj0gbGFuZFdpZHRoKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBwb3NpdGlvbiA9IHdhdGVyV2lkdGggKyBsYW5kV2lkdGggKyAxOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KCQkJZWxzZS8vdmxbcG9zaXRpb24tMV0uc2l6ZSgpID0gMAogICAgICAgICAgICB7CiAgICAgICAgICAgICAgIAoJCQkJaWYocG9zaXRpb24gPiAxKQoJCQkJewoJCQkJCS0tcG9zaXRpb247CiAgICAgICAgICAgICAgICAgICAgCgkJCQkJcyA9IHZsW3Bvc2l0aW9uLTFdLnNpemUoKTsKCQkJCQliaFt2bFtwb3NpdGlvbi0xXVtzLTFdXSArPSAxOwogICAgICAgICAgICAgICAgICAgIHVzZV9iaFt2bFtwb3NpdGlvbi0xXVtzLTFdXSAtPSAxOwoJCQkJCXZsW3Bvc2l0aW9uLTFdLnJlc2l6ZShzLTEpOwoJCQkJCQoJCQkJCWlmKHM+MSkKCQkJCQl7CgkJCQkJCWJoW3ZsW3Bvc2l0aW9uLTFdLmJhY2soKV0gLT0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgdXNlX2JoW3ZsW3Bvc2l0aW9uLTFdLmJhY2soKV0gKz0gMTsKCQkJCQkJaWYocG9zaXRpb24gPiB3YXRlcldpZHRoKQoJCQkJCQl7CgkJCQkJCQlwaCA9IHZsW3Bvc2l0aW9uLTFdLmJhY2soKTsJCgkJCQkJCX0KCQkJCQkJZWxzZQoJCQkJCQl7CgkJCQkJCQlwaCA9IHZsW3Bvc2l0aW9uLTFdLmJhY2soKSAtIGRlcHRoOwoJCQkJCQl9CgkJCQkJCSsrcG9zaXRpb247CgkJCQkJCXNraXAgPSAxOwoJCQkJCX0KCQkJCQl3aGlsZSh2bFtwb3NpdGlvbi0xXS5zaXplKCkgPT0gMCAmJiAwPT1za2lwKQoJCQkJCXsKCQkJCQkJaWYocG9zaXRpb24gPiAxKQoJCQkJCQl7CgkJCQkJCQktLXBvc2l0aW9uOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBzID0gdmxbcG9zaXRpb24tMV0uc2l6ZSgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgYmhbdmxbcG9zaXRpb24tMV1bcy0xXV0gKz0gMTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHVzZV9iaFt2bFtwb3NpdGlvbi0xXVtzLTFdXSAtPSAxOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdmxbcG9zaXRpb24tMV0ucmVzaXplKHMtMSk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZihzPjEpCgkJCQkJCQl7CgkJCQkJCQkJYmhbdmxbcG9zaXRpb24tMV0uYmFjaygpXSAtPSAxOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHVzZV9iaFt2bFtwb3NpdGlvbi0xXS5iYWNrKCldICs9IDE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYocG9zaXRpb24gP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