[Question]
  [
This is how the Kolakoski sequence (OEIS [A000002](https://oeis.org/A000002)) is defined:
> 
> The Kolakoski sequence is a sequence that contains `1` and `2`, and the `n`th element of the sequence is the length of the `n`th group of equal elements (run) in the sequence itself. The first 20 terms of the sequence and the respective lengths are:
> 
> 
> 
> ```
> 1 2 2 1 1 2 1 2 2 1 2 2 1 1 2 1 1 2 2 1
> - --- --- - - --- - --- --- - --- --- -
> 1  2   2  1 1  2  1  2   2  1  2   2  1
> 
> ```
> 
> Essentially, the lengths of the groups of equal elements of the Kolakoski sequence is the Kolakoski sequence itself.
> 
> 
> 
So far, so good, but that why should we restrict ourselves to `1` and `2`? We're not going to! Given two inputs, an array of positive integers `A` and an integer `N`, return the first `N` terms of the Kolakoski-like sequence defined by cycling through `A`. To get the grasp of it better, here is a worked example with the lengths of the newly added groups in brackets:
```
A = [2, 3, 1]
N = 25
2: [[2], 2 ]
3: [ 2 ,[2], 3 , 3 ]
1: [ 2 , 2 ,[3], 3 , 1 , 1 , 1 ]
2: [ 2 , 2 , 3 ,[3], 1 , 1 , 1 , 2 , 2 , 2 ]
3: [ 2 , 2 , 3 , 3 ,[1], 1 , 1 , 2 , 2 , 2 , 3 ]
1: [ 2 , 2 , 3 , 3 , 1 ,[1], 1 , 2 , 2 , 2 , 3 , 1 ]
2: [ 2 , 2 , 3 , 3 , 1 , 1 ,[1], 2 , 2 , 2 , 3 , 1 , 2 ]
3: [ 2 , 2 , 3 , 3 , 1 , 1 , 1 ,[2], 2 , 2 , 3 , 1 , 2 , 3 , 3 ]
1: [ 2 , 2 , 3 , 3 , 1 , 1 , 1 , 2 ,[2], 2 , 3 , 1 , 2 , 3 , 3 , 1 , 1 ]
2: [ 2 , 2 , 3 , 3 , 1 , 1 , 1 , 2 , 2 ,[2], 3 , 1 , 2 , 3 , 3 , 1 , 1 , 2 , 2 ]
3: [ 2 , 2 , 3 , 3 , 1 , 1 , 1 , 2 , 2 , 2 ,[3], 1 , 2 , 3 , 3 , 1 , 1 , 2 , 2 , 3 , 3 , 3 ]
1: [ 2 , 2 , 3 , 3 , 1 , 1 , 1 , 2 , 2 , 2 , 3 ,[1], 2 , 3 , 3 , 1 , 1 , 2 , 2 , 3 , 3 , 3 , 1 ]
2: [ 2 , 2 , 3 , 3 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 1 ,[2], 3 , 3 , 1 , 1 , 2 , 2 , 3 , 3 , 3 , 1 , 2 , 2 ]
C: [ 2 , 2 , 3 , 3 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 1 , 2 , 3 , 3 , 1 , 1 , 2 , 2 , 3 , 3 , 3 , 1 , 2 , 2 ]
```
Here is another worked example with a leading `1`:
```
A = [1, 2, 3]
N = 10
1: [[1]]
2: [ 1 ,[2], 2 ]
3: [ 1 , 2 ,[2], 3 , 3 ]
1: [ 1 , 2 , 2 ,[3], 3 , 1 , 1 , 1 ]
2: [ 1 , 2 , 2 , 3 ,[3], 1 , 1 , 1 , 2 , 2 , 2 ]
C: [ 1 , 2 , 2 , 3 , 3 , 1 , 1 , 1 , 2 , 2 ]
```
As you can see above, the final result was cut to `N = 10` elements. The `n`th element should be how long the `n`th equal-element group is, even if the element itself belongs in the group it refers to. As in the above case, the first `1` refers to the first such group which is just that `1`, and the first `2` refers to the second such group, which starts with it.
## Rules
* You may assume that `A` will never have two or more consecutive equal elements. `A` may contain an integer more than once, but the first and last elements will not be equal, and `A` will contain at least 2 elements (e.g. `[1, 2, 2, 3]`, `[2, 4, 3, 1, 2]` and `[3]` aren't going to be given). That's because if there were consecutive equal elements, the final result would've been an invalid prefix for such a sequence.
* You may assume `A` only contains positive integers (as such a sequence would be otherwise undefined).
* You may assume `N` is a non-negative integer (`N >= 0`).
* You can't return more terms than requested.
* Using any one of the [standard loopholes](https://codegolf.meta.stackexchange.com/q/1061/41024) is strictly forbidden.
* You may use any [reasonable I/O method](https://codegolf.meta.stackexchange.com/q/2447/41024).
* Your answer doesn't have to work beyond natural language limits, but [in theory your algorithm should work for arbitrarily large inputs and integers](https://codegolf.meta.stackexchange.com/a/8245/41024).
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest answer wins.
## Test cases
```
[5, 1, 2], 0 -> []
[2, 3, 1], 25 -> [2, 2, 3, 3, 1, 1, 1, 2, 2, 2, 3, 1, 2, 3, 3, 1, 1, 2, 2, 3, 3, 3, 1, 2, 2]
[1, 2, 3], 10 -> [1, 2, 2, 3, 3, 1, 1, 1, 2, 2]
[1, 2], 20 -> [1, 2, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2, 1]
[1, 3], 20 -> [1, 3, 3, 3, 1, 1, 1, 3, 3, 3, 1, 3, 1, 3, 3, 3, 1, 1, 1, 3]
[2, 3], 50 -> [2, 2, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 2, 3, 3]
[7, 4], 99 -> [7, 7, 7, 7, 7, 7, 7, 4, 4, 4, 4, 4, 4, 4, 7, 7, 7, 7, 7, 7, 7, 4, 4, 4, 4, 4, 4, 4, 7, 7, 7, 7, 7, 7, 7, 4, 4, 4, 4, 4, 4, 4, 7, 7, 7, 7, 7, 7, 7, 4, 4, 4, 4, 7, 7, 7, 7, 4, 4, 4, 4, 7, 7, 7, 7, 4, 4, 4, 4, 7, 7, 7, 7, 4, 4, 4, 4, 7, 7, 7, 7, 7, 7, 7, 4, 4, 4, 4, 4, 4, 4, 7, 7, 7, 7, 7, 7, 7, 4]
[1, 2, 3], 5 -> [1, 2, 2, 3, 3]
[2, 1, 3, 1], 2 -> [2, 2]
[1, 3, 5], 2 -> [1, 3]
[2, 3, 2, 4], 10 -> [2, 2, 3, 3, 2, 2, 2, 4, 4, 4]
```
  
[Answer]
# [Husk](https://github.com/barbuz/Husk), 8 bytes
```
Ṡωȯ↑⁰`Ṙ¢
```
Takes first the length, then the list.
[Try it online!](https://tio.run/##yygtzv6vcGhJVX5x3aOmxsyiQ9v@P9y54HznifWP2iY@atyQ8HDnjEOL/v//b8BlZMplZABCpgZclpZcQB4QGhr8jzbVMdQxiuWKNtIx1jEE0hCeoY4xRAxImuuYQMShYoZwlcY6plCdRkA1AA "Husk – Try It Online")
## Explanation
```
Ṡωȯ↑⁰`Ṙ¢  Inputs: n=9 and x=[2,1,3]
Ṡωȯ       Apply the following function to x until a fixed point is reached:
           Argument is a list, say y=[2,2,1,3,3,3]
       ¢   Cycle x: [2,1,3,2,1,3..
     `Ṙ    Replicate to lengths in y: [2,2,1,1,3,2,2,2,1,1,1,3,3,3]
   ↑⁰      Take first n elements: [2,2,1,1,3,2,2,2,1]
          Final result is [2,2,1,1,3,2,1,1,1], print implicitly.
```
[Answer]
# Pyth, 14 bytes
```
u<s*V]M*QlGGvz
```
Try it online: [Demonstration](http://pyth.herokuapp.com/?code=u%3Cs%2AV%5DM%2AQlGGvz&input=%5B1%2C%202%5D%0A20&test_suite_input=%5B5%2C%201%2C%202%5D%0A0%0A%5B%5D%0A%5B2%2C%203%2C%201%5D%0A25%0A%5B2%2C%202%2C%203%2C%203%2C%201%2C%201%2C%201%2C%202%2C%202%2C%202%2C%203%2C%201%2C%202%2C%203%2C%203%2C%201%2C%201%2C%202%2C%202%2C%203%2C%203%2C%203%2C%201%2C%202%2C%202%5D%0A%5B1%2C%202%2C%203%5D%0A10%0A%5B1%2C%202%2C%202%2C%203%2C%203%2C%201%2C%201%2C%201%2C%202%2C%202%5D%0A%5B1%2C%202%5D%0A20%0A%5B1%2C%202%2C%202%2C%201%2C%201%2C%202%2C%201%2C%202%2C%202%2C%201%2C%202%2C%202%2C%201%2C%201%2C%202%2C%201%2C%201%2C%202%2C%202%2C%201%5D%0A%5B1%2C%203%5D%0A20%0A%5B1%2C%203%2C%203%2C%203%2C%201%2C%201%2C%201%2C%203%2C%203%2C%203%2C%201%2C%203%2C%201%2C%203%2C%203%2C%203%2C%201%2C%201%2C%201%2C%203%5D%0A%5B2%2C%203%5D%0A50%0A%5B2%2C%202%2C%203%2C%203%2C%202%2C%202%2C%202%2C%203%2C%203%2C%203%2C%202%2C%202%2C%203%2C%203%2C%202%2C%202%2C%203%2C%203%2C%203%2C%202%2C%202%2C%202%2C%203%2C%203%2C%203%2C%202%2C%202%2C%203%2C%203%2C%202%2C%202%2C%202%2C%203%2C%203%2C%203%2C%202%2C%202%2C%203%2C%203%2C%202%2C%202%2C%202%2C%203%2C%203%2C%203%2C%202%2C%202%2C%202%2C%203%2C%203%5D%0A%5B7%2C%204%5D%0A99%0A%5B7%2C%207%2C%207%2C%207%2C%207%2C%207%2C%207%2C%204%2C%204%2C%204%2C%204%2C%204%2C%204%2C%204%2C%207%2C%207%2C%207%2C%207%2C%207%2C%207%2C%207%2C%204%2C%204%2C%204%2C%204%2C%204%2C%204%2C%204%2C%207%2C%207%2C%207%2C%207%2C%207%2C%207%2C%207%2C%204%2C%204%2C%204%2C%204%2C%204%2C%204%2C%204%2C%207%2C%207%2C%207%2C%207%2C%207%2C%207%2C%207%2C%204%2C%204%2C%204%2C%204%2C%207%2C%207%2C%207%2C%207%2C%204%2C%204%2C%204%2C%204%2C%207%2C%207%2C%207%2C%207%2C%204%2C%204%2C%204%2C%204%2C%207%2C%207%2C%207%2C%207%2C%204%2C%204%2C%204%2C%204%2C%207%2C%207%2C%207%2C%207%2C%207%2C%207%2C%207%2C%204%2C%204%2C%204%2C%204%2C%204%2C%204%2C%204%2C%207%2C%207%2C%207%2C%207%2C%207%2C%207%2C%207%2C%204%5D%0A%5B1%2C%202%2C%203%5D%0A5%0A%5B1%2C%202%2C%202%2C%203%2C%203%5D%0A%5B2%2C%201%2C%203%2C%201%5D%0A2%0A%5B2%2C%202%5D%0A%5B1%2C%203%2C%205%5D%0A2%0A%5B1%2C%203%5D%0A%5B2%2C%203%2C%202%2C%204%5D%0A10%0A%5B2%2C%202%2C%203%2C%203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C+7%2C+7%2C+7%2C+7%2C+4%2C+4%2C+4%2C+4%2C+4%2C+4%2C+4%2C+7%2C+7%2C+7%2C+7%2C+7%2C+7%2C+7%2C+4%2C+4%2C+4%2C+4%2C+4%2C+4%2C+4%2C+7%2C+7%2C+7%2C+7%2C+7%2C+7%2C+7%2C+4%2C+4%2C+4%2C+4%2C+7%2C+7%2C+7%2C+7%2C+4%2C+4%2C+4%2C+4%2C+7%2C+7%2C+7%2C+7%2C+4%2C+4%2C+4%2C+4%2C+7%2C+7%2C+7%2C+7%2C+4%2C+4%2C+4%2C+4%2C+7%2C+7%2C+7%2C+7%2C+7%2C+7%2C+7%2C+4%2C+4%2C+4%2C+4%2C+4%2C+4%2C+4%2C+7%2C+7%2C+7%2C+7%2C+7%2C+7%2C+7%2C+4%5D%0A%5B1%2C+2%2C+3%5D%0A5%0A%5B1%2C+2%2C+2%2C+3%2C+3%5D%0A%5B2%2C+1%2C+3%2C+1%5D%0A2%0A%5B2%2C+2%5D%0A%5B1%2C+3%2C+5%5D%0A2%0A%5B1%2C+3%5D%0A%5B2%2C+3%2C+2%2C+4%5D%0A10%0A%5B2%2C+2%2C+3%2C+3%2C+2%2C+2%2C+2%2C+4%2C+4%2C+4%5D&debug=0&input_size=3)
### Explanation:
```
u                 start with G = input array
       *QlG       repeat input array
     ]M           put every element into its own list
   *V      G      repeat every list vectorized by the counts in G
  s               flatten
 <          vz    take the first (second input line) numbers
                  and assign them to G until you reach fixed point
```
[Answer]
# Java 8, ~~151 + 19~~ ~~119~~ 115 bytes
```
a->n->{int c=0,l[]=new int[n],i=0,j;for(;i<n;i++)for(j=0;j<(c==i?a[i]:l[i])&c<n;j++)l[c++]=a[i%a.length];return l;}
```
[Try it online!](https://tio.run/##ZU/LTsMwELznK1aVQDFxrfI41XG4IXHg1GPkg3HdsMZ1Iscpqqp8e3CqAAfmsNLOzGpnrDqpddsZb/efEx67NkSwiWNDRMcOg9cRW8/uePZPTFzWDe8ONWin@h7eFHq4ZJDQRxUT/7Lcl@hjLenf/uqjaUygcBWqChoQMKl15dfVJXGgxYa6Wgpvvq4eLykmyvJDG3KOpedYFGRerNhwW@ZaCHxWNcqtS4Pc6mSxyeJqXRRSJOVGMWd8Ez8kDyYOwYPj45RKzIGXIkvuU4t7OKY6@S4G9E0tQYWmJ0u7GfPrOShuG6a6zp3zn6jy8kDhkUKaTyNZxPsNIb@3M3bnPpoja4fIuvQi5lis6Irwq2nMxukb "Java (OpenJDK 8) – Try It Online")
[Answer]
# [R](https://www.r-project.org/), ~~120~~ ~~114~~ 108 bytes
*-6 bytes thanks to plannapus*
```
function(A,N){i=inverse.rle
w=length
a=rle(A)
while(w(a$l)<N){a[[1]]=i(a)
a[[2]]=rep(A,l=w(a$l))}
i(a)[0:N]}
```
[Try it online!](https://tio.run/##TYxBDsIgFET3nMKFi/8TNIAaU1MWvUAvQFiQBiwJoQarLEzPjjRd6O69mcmk4nbtobhXHGY/Rehojx8vfXzb9LTHFCzJMth4n0diZFXokOTRV8hg9gHbujdKca2lB4Oksqic7KN@BbmNcCFrqdit10txMICgJ8qRiguSP2WbXekZadP8KrEGnGH5Ag "R – Try It Online")
Anonymous function; successively inverts the RLE, replacing the lengths `a[[1]]` with the inverted RLE, and the values `a[[2]]` with `A` replicated to a length equal to that of `a$l`.
[Answer]
# [Haskell](https://www.haskell.org/), 68 bytes
Many thanks to Laikoni and flawr for their help in debugging and golfing this answer in the PPCG Haskell chatroom, [Of Monads and Men](https://chat.stackexchange.com/rooms/66515/of-monads-and-men). Golfing suggestions welcome! [Try it online!](https://tio.run/##y0gszk7NyfmvoaxpG/NfQy9NU68kMTuVK00h0UGjyipe01ajykYl2lBPrypWU1s7JV8h00YXxI21Tq5MzklVSFRUzIQoSAOzY//nJmbmKdgqFBRl5pUoqCgYmSpHG@kY6xjG/v@XnJaTmF78Xze5oAAA "Haskell – Try It Online")
```
(.f).take
f a@(z:_)=(z<$[1..z])++do i<-[1..];cycle a!!i<$[1..f a!!i]
```
The first line is an anonymous function. The second line is the infinite list comprehension that produces our Kolakoski-like sequence.
**Explanation**
First, we define `z` as the head of `a` with `a@(z:_)`. Then, we initialize the sequence with `(z<$[1..z])`.
Then, from `1` onwards, `do i<-[1..]`, we append the following to the sequence: `cycle a!!i<$[1..f a!!i]`, which is the `i`-th member of `a` (cycled indefinitely) appended `f a!!i` times.
Finally, the anonymous function simply takes the first `n` terms of our Kolaskoski-like sequence.
[Answer]
# [Python 2](https://docs.python.org/2/), 123 bytes
```
x,y=input()
k=x[0]
t,j=[],0
if k==1:t,j=[k]+x[1]*[x[1]],2
while len(t)<y:t+=(j and t[j]or k)*[x[j%len(x)]];j+=1
print t[:y]
```
[Try it online!](https://tio.run/##TY7BbsMgEETP4Sv2UmHXHIDUikLKlyAOkUNqcIQtiyjw9S6YSO0Fzcy@2WVJYZw934b5ZiTGeIskSeuXZ2haNMmoqEaBOKk0ocjeYZKSiT2YdBcV05@qvJpw9Brtw8DD@Ca030mETjYOrv4GQTk9rzC1hXUfhYit1hfXSYaW1fqQEZH0lu@jeyYtWA/r1f@YhrFWoENYkzDRDFC@iQ4mDmYJYq/iMD7xpnoCjADXBChSnMAx@2x4j1TJc5Ado9WVQZXHt3wDfZYnAl9Zns//m/2OsL@9tZ0H1dSLvFYZ/QU "Python 2 – Try It Online")
]