Comments

Please Help. I am a beginner and I am solving this contest for practice. (sorry for necroing) in D, Can someone please explain what's wrong in this approach?:

#include <iostream>
#include <set>
#include <vector>
#define ll __int64
using namespace std;

int main()
{
    short n;
    cin >> n;
    ll a[n];
    set<ll> nos, fixed;
    vector<ll> v;
    for (short i = 0; i < n; i++)
    {
        cin >> a[i];
        fixed.insert(a[i]);
    }
    nos = fixed;
    short j = 0;
    ll s = a[j];
    v.push_back(s);
    nos.erase(s);
    for (short i = 0; i < n; i++)
    {
        if (a[i] == s)
            continue;
        if ((s % 3 == 0) && (nos.count(s / 3)))
        {
            s /= 3;
            v.push_back(s);
            nos.erase(s);
        }
        if (nos.count(s * 2))
        {
            s = s * 2;
            v.push_back(s);
            nos.erase(s);
        }
        if ((i + 1 == n) && (v.size() != n))
        {
            v.clear();
            i = -1;
            j++;
            s = a[j];
            v.push_back(s);
            nos = fixed;
            nos.erase(s);
        }
    }
    for (short i = 0; i < n; i++)
    {
        cout << v[i] << endl;
    }
    return 0;
}
Your code here...

why is this happening?