Count "Word Order" with further optimization
TLE

You're probably getting a TLE because,

if value not in array:


Is a O(N) operation meaning it has to traverse the entire array (worst case) to check if the value exists in the array

I can understand why you felt the need to have an extra array, because of dictionaries are not ordered. 

But you can make use of the collections.OrderedDict module, to have an ordered dictionary!

Other


join!

print(*(i for i in elements.values()), end=' ')


This can be done cleaner with joining the values instead of unpacking

print(" ".join(map(str, e.values())))

Counter

Python is often described as batteries included,

Your element dictionary is the same the collections.Counter


Code

We can make an OrderedCounter class to combine these two and make get the most out of the included modules.

from collections import Counter, OrderedDict

class OrderedCounter(Counter, OrderedDict):
    pass

c = OrderedCounter(input() for _ in range(int(input())))
print(len(c))
print(" ".join(map(str, c.values())))