"Defragmenting" a list A fragmented file is a file which is seperated into several parts on a harddrive, because it can't fit into a certain space.

On all harddrives, there are files called unmovable files, which are unmovable. To simulate real data on a harddrive, the input will also have unmovable files.

You are given several sequences of consecutive numbers, all scrambled into one list. The goal is to order each of these sequences so that they are placed in as little space as possible. The list can be as big as you need it to be. If there is no space for a sequence, then put a - instead. 

To mark the unmovable files, another list, with the same length as the data, will be given after you recieve the data. It will contain either a 0 or a 1. Every 1 within that list means that the corresponding item in the data is unmovable.

Shortest code wins.

Test Cases

The output does not have to be exactly like this, but the length needs to be the same, and all of the sequences need to be ordered.

[ 0,  0,  1,  1,  1,  1,  0,  0,  0,  0,  0]
[25, 30, 11, 12, 13, 14, 68, 69, 31, 26, 32]
Output: [25, 26, 11, 12, 13, 14, 68, 69, 30, 31, 32]
(output length: 11)

[0, 0, 0,  0, 0,  1,  1,  1, 0, 0,  0,  0,  0,  0,  0, 0]
[2, 3, 5, 14, 8, 10, 11, 12, 4, 6, 28, 30, 29, 15, 16, 7]
Output: [14, 15, 16, -, -, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 28, 29, 30]
(output length: 18)
APL (132 126 123 147 145 143) 

This was a bit harder than I thought it'd be :)

Edit: the hard drive is infinite :S. All sequences that could not fit in the original space, are now appended onto the end of the output.

F←⍬⋄{×⍴⍵:∇1↓⍵⊣{∨/P←S⍷⍨1⍴⍨⍴⍵:S[R]∘←0⊣D[R←1-⍨(⍳⍴⍵)+⌊/P/⍳⍴P]∘←⍵⋄F∘←F,⍵}⊃⍵⋄D[S/⍳⍴S]∘←'-'}G[⍒⊃∘⍴¨G←K⊂⍨1≠|-⌿K,[÷2]¯1⌽K←K[⍋K←(S←~⎕)/D←⎕]]⋄F,⍨D/⍨~⌽∧\⌽S


This is what it does to your 1st and 2nd example (the 2nd example is different, but in your example the output length is not the same as the input length and you said in a comment it was wrong, so...)


⎕:    25 30 11 12 13 14 68 69 31 26 32
⎕:    0  0  1  1  1  1  0  0  0  0  0 
25 26 11 12 13 14 30 31 32 68 69



⎕:    2 3 5 14 8 10 11 12 4 6 28 30 29 15 16 7
⎕:    0 0 0 0  0 1  1  1  0 0 0  0  0  0  0  0
14 15 16 - - 10 11 12 2 3 4 5 6 7 8 28 29 30


Explanation:


F←⍬: set F to the empty list. This will hold the overflow.
K←K[⍋K←(S←~⎕)/D←⎕]: read two lists. The first list (numbers) is stored in D. The second list (whether the data is unmovable) is inverted and then stored in S. Get all movable numbers, and sort them. Store this in K.
G←K⊂⍨1≠|-⌿K,[÷2]¯1⌽K: Subtract each element of K from the next element of K. Wherever this is not 1, we found the beginning of a new sequence. Store the list of sequences in G.
G[⍒⊃∘⍴¨G...]: sort the list of sequences by descending length. This way, we place the longest sequences first.
{×⍴⍵:...}G: as long as this list of sequences is not empty...
∇1↓⍵⊣: handle the tail of the list, after...
{...}⊃⍵: doing the following with the head:
∨/P←S⍷⍨1⍴⍨⍴⍵: find, in S, a list of ones that is as long as the current sequence (⍵).
S[R]∘←0⊣D[R←1-⍨(⍳⍴⍵)+⌊/P/⍳⍴P]∘←⍵: if there is one, store ⍵ in D at the first position that it fits, and mark these positions as unmovable in S.
⋄F∘←F,⍵: if there is no room, add it to the overflow.
⋄D[S/⍳⍴S]∘←'-': when we're at the end of the list of sequences, all positions where S is still one are places where we couldn't put anything. Mark these with -.
⋄F,⍨D/⍨~⌽∧\⌽S: then, at the end, display D, with the trailing -s removed, followed by the overflow (F) per the new rules.